【问题标题】:Keras Realtime Augmentation adding Noise and ContrastKeras 实时增强添加噪声和对比度
【发布时间】:2017-09-08 23:20:37
【问题描述】:

Keras 为实时增强提供了一个ImageDataGenerator 类,但它不包括对比度调整和添加噪声。

我们如何在训练期间应用随机噪声水平和随机对比度调整?可以将这些函数添加到数据生成中的“preprocessing_function”参数中吗?

谢谢。

【问题讨论】:

    标签: image image-processing machine-learning deep-learning keras


    【解决方案1】:

    您确实可以使用 preprocessing_function 添加噪音。

    示例脚本:

    import random
    import numpy as np
    
    def add_noise(img):
        '''Add random noise to an image'''
        VARIABILITY = 50
        deviation = VARIABILITY*random.random()
        noise = np.random.normal(0, deviation, img.shape)
        img += noise
        np.clip(img, 0., 255.)
        return img
    
    # Prepare data-augmenting data generator
    from keras.preprocessing.image import ImageDataGenerator
    datagen = ImageDataGenerator(
            rescale=1./255,
            rotation_range=40,
            width_shift_range=0.2,
            height_shift_range=0.2,
            zoom_range=0.2,
            preprocessing_function=add_noise,
        )
    
    # Load a single image as our example
    from keras.preprocessing import image
    img_path = 'cat_by_irene_mei_flickr.png'
    img = image.load_img(img_path, target_size=(299,299))
    
    # Generate distorted images
    images = [img]
    img_arr = image.img_to_array(img)
    img_arr = img_arr.reshape((1,) + img_arr.shape)
    for batch in datagen.flow(img_arr, batch_size=1):
        images.append( image.array_to_img(batch[0]) )
        if len(images) >= 4:
            break
    
    # Display
    import matplotlib.pyplot as plt
    f, xyarr = plt.subplots(2,2)
    xyarr[0,0].imshow(images[0])
    xyarr[0,1].imshow(images[1])
    xyarr[1,0].imshow(images[2])
    xyarr[1,1].imshow(images[3])
    plt.show()
    

    脚本生成的示例图片:

    【讨论】:

    • 不错的答案。请注意,实现的这种噪声只会使图像的一部分变亮,而不会变暗。另一个注意事项是您的 ImageDataGenerator 将生成具有 [0 1] 像素值的图像。我认为许多基于 CNN 的技术都期望零均值数据-因此,如果您的数据均值为 0.5,则需要将其移动-.5(在您列出的转换下,许多大型图像集合都是如此)。我似乎确实记得,如果您使用迁移学习技术(例如从预训练的初始网络等开始),有些人会期望零均值数据。
    • 还有一点需要注意:如果您使用 ImageDataGenerator 进行训练/验证拆分 (validation_split=%),我认为它可以将该 preprocessing_function 应用于验证集。这可能是您不希望发生的事情。
    • @michar,好点。但是,由于平均值为零,平均而言,它会使图像中接近黑色的部分变亮,而使图像中接近白色的部分变暗。
    • 是的 - 但我认为由于您正在使用 [0 255] 上的图像像素,因此会有一个问题,因为平均值为 127.5。考虑使用“noise = np.random.normal(127.5, deviation, img.shape)”和最后一行“return img - 127.5”。它们在 ImageDataGenerator 缩放 1/255 后,结果将在 [-. 5, .5] 均值为零。这有意义吗?
    • @michar,是的,我同意它可能应该以零为中心。我刚刚澄清了“永不变暗”,因为高光明显被噪声变暗了。无论如何,感谢 cmets。
    【解决方案2】:

    我在this blog 发现你可以做一些简单的事情:

    from keras.layers import GaussianNoise
    
    model.add(Dense(32))
    model.add(GaussianNoise(0.1))
    model.add(Activation('relu'))
    model.add(Dense(32))
    ...
    

    很遗憾,我找不到类似的方法来调整/增强对比度。但是你可以,根据这个post,用

    增加亮度
    from keras.preprocessing.image import ImageDataGenerator
    ImageDataGenerator(brightness_range=[range_min,range_max])
    

    【讨论】:

      【解决方案3】:

      来自 Keras 文档:

      preprocessing_function:每个输入都隐含的函数。该函数将在对其进行任何其他修改之前运行。该函数应采用一个参数:一张图像(等级为 3 的 Numpy 张量),并应输出具有相同形状的 Numpy 张量。

      所以,我创建了一个简单的函数,然后使用了imgaug module 中的图像增强函数。请注意,imgaug 要求图像等级为 4。

      【讨论】:

      • 是否可以让preprocessing_functiony_train 数据的影响与x_train 一样多?我正在处理每组中的图像,例如,如果我翻转 X,我希望 Y 也被翻转。
      • 请分享您创建的代码,这将对其他人非常有帮助:)
      猜你喜欢
      • 2020-10-23
      • 2018-07-15
      • 1970-01-01
      • 2019-09-12
      • 2016-06-29
      • 2020-05-09
      • 1970-01-01
      • 2017-09-02
      • 2019-10-03
      相关资源
      最近更新 更多