【问题标题】:Data augmentation function not correct数据增强功能不正确
【发布时间】:2019-11-24 19:23:35
【问题描述】:

请注意:我是新手,所以请保持温和。

我正在尝试创建一个函数,该函数将移动 MNIST 数据集中的每个图像并将移动后的图像添加到原始数据集中,从而有效地将数据集大小翻倍。

我的代码(警告,可能是一团糟,我最终将不得不学习如何编写更优雅的函数):

def four_point(i_set):
o_set = i_set
    for i in i_set:
        copy1 = np.ndarray(shape=(28,28))
        shift(i, shift=(1,0), output=copy1)
        copy1 = copy1.reshape(1,28,28)
        o_set = np.concatenate((o_set, copy1))
    return o_set

我检查了输出的数据集,它似乎没有应用转变。任何人都可以指导我过去吗?

【问题讨论】:

    标签: python numpy machine-learning data-augmentation


    【解决方案1】:

    不要编写自己的函数来执行此操作,而是依赖更高级别的机器学习/深度学习模块提供的内置函数。

    就像在 Keras 模块中一样,有一个名为 ImageDataGenerator() 的内置函数

    此函数有两个参数用于在图像中生成偏移。一个用于水平移位,另一个用于垂直移位。这两个论点是:

    width_shift_range,
    height_shift_range
    

    每个参数都采用:浮点数、一维数组或整数。

    1)。 float: 总高度的分数,如果 = 1。

    2)。一维数组:来自数组的随机元素。

    3)。 int: 区间内像素的整数个数 (-height_shift_range, +height_shift_range)

    现在,您想扩充这些图像并将它们全部保存在同一个文件夹中,请使用以下代码:

    aug = ImageDataGenerator(width_shift_range=0.2,height_shift_range=0.2)
    
    ### Make sure to have "/" at the end of the path
    
    list_of_images=os.listdir("/path/to/the/folder/of/images/")
    
    total = 0
    #Change the value of "const" to the number of new augmented images to be created
    const= 300
    
    for i in range(const):
        curr_image = random.choice(list_of_images)
        image = load_img("/path/to/the/folder/of/images/"+curr_image)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)
        imageGen = aug.flow(image, batch_size=1, save_to_dir='/path/to/folder/to/save/images/',save_prefix="augment_image",save_format="jpg")
        for image in imageGen:
            total += 1
            if total == const:
                break
            break
    

    上面的 sn-p 将在名为“/path/to/folder/to/save/images/”的文件夹中创建 300 个新图像。然后你所要做的就是将你的原始图像粘贴到这个文件夹中。

    您可以为 ImageDataGenerator() 提供其他参数,例如亮度、缩放、垂直翻转、水平翻转等。查看documentation 以获取更多此类参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2015-07-21
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多