【问题标题】:How do i apply Data Augmentation on entire data-set我如何在整个数据集上应用数据增强
【发布时间】:2020-05-22 17:00:05
【问题描述】:

enter image description here

大家好,我是机器学习的新手,正在努力学习它。我尝试对我的数据集进行数据扩充。我从 keras 网站获得了这段代码,但这段代码一次只选择一张图像。我希望这段代码从数据集中一张一张地挑选图像并在其上应用增强技术。我对要改变什么感到困惑。如果有人帮助我,我将非常感激。

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

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')

img = load_img('data/train/cats/cat.0.jpg')  # this is a PIL image

x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)

x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)

i = 0

for batch in datagen.flow(x, batch_size=1,
                          save_to_dir='preview', save_prefix='cat', save_format='jpeg'):

    i += 1

    if i > 20:

        break  # otherwise the generator would loop indefinitely

【问题讨论】:

    标签: python data-augmentation


    【解决方案1】:

    我假设您的 data/train/cats/ 文件夹中有数据集。现在,给定上面的代码,它会读取一张图片,对这张图片进行增强,并生成 20 张不同的图片。

    现在,要扩展进程,您可以简单地使用osglob 模块从目录中获取文件列表。然后循环你的代码块。例如:

    import glob
    
    list_of_files = glob.glob('data/train/cats/*.jpg')
    for file in list_of_files:
      img = load_img(file)  # this is a PIL image
      x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
      .
      .
      .
    
    

    您可以更多地使用datagen.flow,而不是循环遍历整个代码块,也就是说,您可以传递整个数据集,而不是将单个图像作为x 传递。例如,如果n 是图像的总数,那么您的x 形状将类似于(n,3,150,150)(假设所有图像的大小相同)。

    此外,您可以更改此 n 值。即不选择总数据集长度。在这种情况下,假设n 的值是20,在第一次迭代时,您将首先读取20 图像并像(20,3,150,150) 一样传递x。然后在第二次迭代中,您阅读下一个20,依此类推。

    例如,

    import glob
    import numpy as np
    
    x = []
    list_of_files = glob.glob('data/train/cats/*.jpg')
    for file in list_of_files:
      img = load_img(file)  # this is a PIL image
      x.append(img_to_array(img))
    x = np.array(x) # feed this x to your datagen.flow
    
    # print(x.shape)
    # (n, 3, 150, 150)
    
    # (Note: n is the length of list_of_files, i.e. total dataset length)
    

    【讨论】:

    • 谢谢你,先生它的工作。先生,因为我是 python 和机器学习的新手,所以我不明白我如何将全部图像发送为“n”。能否请您通过更改上面的代码告诉我,以便我明白我会非常感谢您。
    • 您可以简单地将图像附加到列表中,然后转换为 numpy 数组。我添加了一个示例。
    • 您好,先生,希望您一切顺利。很抱歉再次打扰您。先生,您发送的代码我试过了,但我只能从中得到 11 张图片。先生,图像的形状是:(504, 200, 200, 3)。
    • ` x.append(img_to_array(img)) x = np.array(x) # 将此 x 输入你的 datagen.flow print(x.shape) i = 0 for batch in datagen.flow (x, batch_size=1, save_to_dir=r'C:\Users\Munib\Desktop\Augmented', save_prefix='ws12', save_format='jpeg'): i += 1 if i > 10: break `
    • 好的。在上述场景中,您的batch_size 和最大i 值决定了将生成的图像数量。因此,如果要增加生成图像的数量,则需要增加其中一个参数。在第一种方法中,您修复了单个图像并基于该单个图像,您尝试生成变化。在后一种方法中,您从(504, 200, 200, 3) 中随机汇集一张图像并生成变体。
    猜你喜欢
    • 1970-01-01
    • 2021-02-08
    • 2018-11-14
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2021-04-11
    • 1970-01-01
    相关资源
    最近更新 更多