简体   繁体   English

Keras ImageDataGenerator 返回具有意外失真的图像

[英]Keras ImageDataGenerator returns images with unexpected distortion

I am experimenting with Keras' ImageDataGenerator() in order to augment my image dataset befor I train a CNN model.我正在试验 Keras 的ImageDataGenerator()以便在训练 CNN 模型之前扩充我的图像数据集。

The basic generator object that I've employed is:我使用的基本生成器对象是:

datagen = ImageDataGenerator(rotation_range = 30,
                            width_shift_range = 0.2,
                            height_shift_range = 0.2,
                            horizontal_flip = True)

I then produce some augmented data with:然后我生成一些增强数据:

batch_1 = datagen.flow(image_batch, y = labels, 
                       batch_size = len(image_batch),
                       seed = 173)

When I try to look into that batch_1 object, I find pictures like this:当我试图查看那个batch_1对象时,我发现了这样的图片:

在此处输入图像描述

As you can see, there are those parallel colored lines that I cannot explain.如您所见,有一些我无法解释的平行彩色线条。 No online tutorial on ImageDataGenerator() showed these kind of distortions.没有关于ImageDataGenerator()的在线教程显示此类失真。 Is a CNN trained properly if these images are fed in?如果输入这些图像,CNN 是否得到正确训练?

A quick search tells me that it might be because you are setting your rotation_range to 30°, ImageDataGenerator then fills in the empty space between the frame and the image with a continuation of your image's border.快速搜索告诉我,这可能是因为您将 rotation_range 设置为 30°,然后 ImageDataGenerator 会用图像边框的延续填充框架和图像之间的空白区域。

Setting the angle to a multiple of 90° might be a solution, or you can simply have a white border.将角度设置为 90° 的倍数可能是一个解决方案,或者您可以简单地使用白色边框。

此图像显示正在生成的相同类型的图像。

I faced the same issue when I used ImageDataGenerator.我在使用 ImageDataGenerator 时遇到了同样的问题。 It is not happening due to rotation_range, It actually happens when we use width_shift_range and height_shift_range.它不是由于 rotation_range 而发生的,它实际上发生在我们使用 width_shift_range 和 height_shift_range 时。 We can use different fill_mode to deal with it.我们可以使用不同的 fill_mode 来处理。 I changed my fill_mode to 'reflect' as my dataset contains leaves.我将 fill_mode 更改为“反映”,因为我的数据集包含叶子。

train_iter = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255,validation_split = 0.2,
                                 preprocessing_function = tf.keras.applications.efficientnet.preprocess_input,
                                 rotation_range = 40,
                                 zoom_range = 0.10,
                                 cval = 0.,
                                 width_shift_range=0.2,
                                 height_shift_range=0.2,
                                 shear_range = 0.2,
                                 horizontal_flip = True,
                                 vertical_flip = True,
                                 fill_mode = 'reflect')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM