在深度学习中预处理大而稀疏的图像
这是一个关于在深度学习中预处理大型和稀疏图像的更好方法的普遍问题.
It's a general question about better ways to preprocess large-sized and sparse images in deep learning.
大"是指尺寸太大(例如1024 * 1024 *个通道),无法直接馈入深度学习管道,因此不容易装入可用的GPU内存中.
By large, I mean the size is too big(e.g. 1024*1024*channels) for directly feeding into the deep learning pipeline, and won't easily fit in the available GPU memories.
稀疏"是指检测功能没有均匀分布,因此将图像切成较小的部分(例如64 * 64)可能无法达到将其定位在大图像中的目的.例如,通过拥有99张土地图片和1张房屋图片,将无法在农田中找到一间农舍.
By sparse, I mean the feature for detection is not evenly distributed so that cutting images into smaller pieces(e.g. 64*64) may defeat the purpose of locating it in large images. For example, it won't help to locate a farmhouse in the farmland by having 99 pictures of land and 1 picture of the house.
我当前的解决方案是使用 PIL
将原件切成小块并惩罚假阴性(即,在前面的示例中,农舍被标识为土地).
My current solution is using PIL
to cut originals into smaller pieces and penalize False Negative(i.e. Farmhouse identified as land in the previous example).
我想知道是否有更好的解决方案和管道来处理此类图像数据.
I wonder if there are better solutions and pipelines to deal with such image data.
您可以编写一个自定义数据生成器,以将数据提供给模型.您无需将所有图像一起加载.您可以在训练期间一次加载单个图像或N(批量大小)个图像.使用所有图像文件名和注释数据创建文本/CSV.
You can write a custom data generator to feed data to your model. You don't need to load all the images together. You can load a single image or N(batch size) number of images at a time during training. Create a text/CSV with all the image file names and annotation data.
def datagenerator(batch_size):
df_x = load_your_image_file_paths
df_y = load_your_annotation_data
i = 0 #pointing to data index
while True: #keeping it alive until it finishes
x = []
y = []
for j in range(batch_size):
img = Image.open(df_x[i])
anno = df_y[i]
final_img = preprocess_as_you_want(img)
final_anno = preprocess_your_annotation_if_needed(anno)
x.append(final_img)
y.append(final_anno)
i += 1
'''
df_x should be circular. this function will be called so many times depending
on number of epochs. make sure i never get larger than the maximum index
'''
if i >= len(df_x):
i = 0 #starting from the beginning
yield np.array(x),np.array(y)
您的发电机现在可以使用了.让我们训练您的模型.
Your generator is now ready to use. Let's train your model.
model.fit_generator(datagenerator(your_prefered_batch_size),
steps_per_epoch=total_data/batch_size, ...other_params)
您可以对验证数据执行相同的操作.
You can do the same for validation data.