问题描述
我正在使用带有TensorFlow后端的Keras训练语义分割模型.我采用ImageDataGenerator
进行图像增强,包括旋转,翻转和移位.通过遵循文档,我创建了字典maskgen_args
并将其用作实例化两个ImageDataGenerator
实例.
I'm training a semantic segmentation model using Keras with TensorFlow backend. I adopted ImageDataGenerator
to do the image augmentation, including rotation, flip and shift. By following the documentation, I created a dictionary maskgen_args
and used it as arguments to instantiate two ImageDataGenerator
instances.
maskgen_args = dict(
rotation_range=90,
validation_split=VALIDATION_SPLIT
)
image_datagen = ImageDataGenerator(**maskgen_args)
mask_datagen = ImageDataGenerator(**maskgen_args)
训练数据生成器的操作如下,通过将seed
设置为相同的值,遮罩将与图像匹配.
The training data generator is done as follows, by setting seed
to the same value, the mask will match the image.
training_data_generator = zip(
image_datagen.flow_from_directory(
data_dir,
target_size=(512, 512),
color_mode='rgb',
batch_size=BATCH_SIZE,
class_mode=None,
save_format='jpeg',
seed=GENERATE_SEED,
subset='training'
),
mask_datagen.flow_from_directory(
label_dir,
target_size=(512, 512),
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode=None,
save_format='png',
seed=GENERATE_SEED,
subset='training'
)
)
到目前为止,没有发生任何问题.但是由于我只需要为图像而不是遮罩做一些额外的预处理(例如标准化),因此我创建了另一个imagegen_args
字典,并在实例化ImageDataGenerator
时将其用作参数.
So far, there is no problem occurred. But as I need to do some extra preprocessing (eg. normalization) only for the image but not for the mask, I created another imagegen_args
dictionary and used it as the arguments when instantiating the ImageDataGenerator
.
maskgen_args = dict(
rotation_range=90,
validation_split=VALIDATION_SPLIT
)
imagegen_args = dict(
samplewise_center=True,
samplewise_std_normalization=True,
channel_shift_range=10,
brightness_range=(0.7, 1.3),
**maskgen_args
)
image_datagen = ImageDataGenerator(**imagegen_args)
mask_datagen = ImageDataGenerator(**maskgen_args)
当我检查training_data_generator
的输出时,出现了问题:似乎图像和遮罩是分别生成的:它们确实可以随机旋转,但是它们以不同的角度旋转,与以前不同.这是食物图像和食物遮罩的示例.
When I check the output of the training_data_generator
, problem occurred: seems the image and mask are generated separately: they surely have random rotation, but they are rotated in different angle, unlike before. Here is an example of a food image and the mask for the food.
我检查了image_datagen
和mask_datagen
的id
,两种情况下它们的id
是不同的.我想知道为什么在第一种情况下他们可以以相同的随机角度旋转图像和蒙版,而在第二种情况下却不能?当我确实确实需要给image_datagen
额外的参数时,我应该怎么做才能使它们的行为像第一种情况一样?
I checked the id
of image_datagen
and mask_datagen
, both cases their id
are different. I wonder why the first case they can rotate the image and mask with the same random angle, but not in the second case? What should I do to make them behave like the first case when I indeed need to give extra arguments to image_datagen
?
推荐答案
设置
channel_shift_range=10,
brightness_range=(0.7, 1.3)
这将修改此生成器的RNG,以使Image RNG和Mask RNG不再同步.
This modifies the RNG of this generator so that the Image RNG and the Mask RNG are not in sync anymore.
我建议您在此任务使用自定义序列,直到发布KP新API. (请参见 https://github. com/keras-team/governance/blob/master/rfcs/20190729-keras-preprocessing-redesign.md )
I propose you use a custom Sequence for this task until the KP new API is released. (see https://github.com/keras-team/governance/blob/master/rfcs/20190729-keras-preprocessing-redesign.md)
对于自定义序列的示例,我在这里提出一个示例: https://dref360. github.io/deterministic-da/
For an example of a custom Sequence, I propose an example here: https://dref360.github.io/deterministic-da/
这篇关于Keras`ImageDataGenerator`图像和遮罩以不同方式增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!