我试图从我的数据集中创建图像补丁。这是我的代码:
for path, subdirs, files in os.walk(root_directory):
dirname = path.split(os.path.sep)[-1]
if dirname == 'images': #Find all 'images' directories
images = os.listdir(path) #List of all image names in this subdirectory
for i, image_name in enumerate(images):
if image_name.endswith(".tif"): #Only read jpg images...
image = cv2.imread(path+"/"+image_name, 1) #Read each image as BGR
SIZE_X = (image.shape[1]//patch_size)*patch_size #Nearest size divisible by our patch size
SIZE_Y = (image.shape[0]//patch_size)*patch_size #Nearest size divisible by our patch size
image = Image.fromarray(image)
image = image.crop((0 ,0, SIZE_X, SIZE_Y)) #Crop from top left corner
#image = image.resize((SIZE_X, SIZE_Y)) #Try not to resize for semantic segmentation
image = np.array(image)
#Extract patches from each image
print("Now patchifying image:", path+"/"+image_name)
patches_img = patchify(image, (patch_size, patch_size, 3), step=patch_size) #Step=256 for 256 patches means no overlap
for i in range(patches_img.shape[0]):
for j in range(patches_img.shape[1]):
single_patch_img = patches_img[i,j,:,:]
#Use minmaxscaler instead of just dividing by 255.
single_patch_img = scaler.fit_transform(single_patch_img.reshape(-1, single_patch_img.shape[-1])).reshape(single_patch_img.shape)
#single_patch_img = (single_patch_img.astype('float32')) / 255.
single_patch_img = single_patch_img[0] #Drop the extra unecessary dimension that patchify adds.
image_dataset.append(single_patch_img)
但它显示错误“内存不足”并立即关闭。我使用 32GB 内存和 1660Ti GPU。图像为 5000X5000 像素,我的数据集大小为 12.7GB。
如果您在 Jupyter 笔记本中加载文件并将其内容存储在变量中,则只要该变量存在并且笔记本正在运行,底层 Python 进程就会保留分配给该数据的内存。如果 Python 的垃圾收集器检测到不再需要数据,它将再次释放内存(在大多数情况下)。如果删除就是这种情况,例如通过使用 del,如果变量被其他内容覆盖或者超出范围(函数末尾的局部变量)。
如果您将大文件存储在(不同的)变量中数周,数据将保留在内存中并最终填满它。在这种情况下,您实际上可能必须手动关闭笔记本或使用其他方法来删除(全局)变量。
同一类问题的完全不同原因可能是 Jupyter 中的错误。此类错误称为内存泄漏,通常发生在长时间运行的服务器进程中。即使它们在 Python 中发生的可能性较小,但 Jupyter 也有一些错误报告。在这种情况下,唯一的解决方法可能是重新启动 Jupyter 进程。在其他情况下,我建议这样做。
jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace yournotebookname.ipynb
从 anaconda 提示符终端运行此命令。