【问题标题】:How does data augmentation work in Keras?数据增强在 Keras 中是如何工作的?
【发布时间】:2017-07-21 14:34:06
【问题描述】:

我有以下代码使用列表中的数据作为输入进行数据扩充:

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img import PIL

def augment(file_images_path, dir_save):

    datagen = ImageDataGenerator(rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest')

    with open(file_images_path) as f:

       images_names = f.readlines()
       images_names = [x.strip() for x in images_names]
       for line in images_names:
           img=PIL.Image.open(line)             
           img=img.resize((28,28))                        
           x = img_to_array(img)                                                    
           x = x.reshape((1,) + x.shape)        
           # the .flow() command below generates batches of randomly transformed 
           #images and saves the results to the `dir_save` directory            
           i = 0            
           for batch in datagen.flow(x, batch_size=1, save_to_dir=dir_save, save_prefix='augmented', save_format='tif'):
                i += 1
                if i > 2:
                    break  # otherwise the generator would loop indefinitely

我是 Keras 数据增强方面的新手,我想知道 Keras 每次迭代对我的图像执行多少图像操作。例如,如果我在包含 14 个图像的列表上运行此代码,它将生成 126 个增强图像。如果我在包含 125 个图像的列表上运行它,它将生成 370 个增强图像。我的问题是:为什么?

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    如果您在 Keras 中使用数据增强,那么每次生成一些数据时,数据都会稍作修改。

    现在,一些数据增强步骤的选项数量有限(例如,您可以翻转图像,也可以不翻转),因此使用这些可能会使您拥有的图像数量翻倍。

    其他人有(实际上)无限数量的选择。例如,当您指定rotation_range=40 时,这意味着每次生成图像时,该图像都会以-40 到40 度之间随机选择的角度进行旋转。

    因此,通过您使用的数据增强,您实际上可以生成无限多不同的图像。然而,这些将是高度相关的,显然不如实际拥有无限多的图像。

    【讨论】:

    • 我还是不明白。例如,您能告诉我在我的代码中的每个 for 循环中执行了多少图像操作吗?如果我在上面运行我的代码,我不明白为什么 14 个图像会变成 126 个增强图像。你能告诉我为什么吗?
    • 在什么意义上它变成了 126 张增强图像?您手动中断,批量大小为 1。
    • 好的,那么您认为我的代码应该生成多少张图片?那是我的问题。谢谢。
    • 由于您为每个图像生成两批大小为 1 的图像,因此您应该获得两倍的图像数量。你确定你没有从代码中删除一些重要的东西吗?
    猜你喜欢
    • 2018-08-17
    • 1970-01-01
    • 2017-11-01
    • 2017-05-01
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多