使用 Keras ImageDataGenerator 更改 RGB 通道强度
在训练模型时,我一直在尝试增加大约360张图像。 除了这些扩展
img_data_gen_args = dict(rotation_range=90,
width_shift_range=0.3,
height_shift_range=0.3,
shear_range=0.5,
zoom_range=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode='reflect')
mask_data_gen_args = dict(rotation_range=90,
width_shift_range=0.3,
height_shift_range=0.3,
shear_range=0.5,
zoom_range=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode='reflect',
preprocessing_function = lambda x: np.where(x>0, 1, 0).astype(x.dtype))
image_data_generator = ImageDataGenerator(**img_data_gen_args)
image_generator = image_data_generator.flow_from_directory(train_images_path,
seed=seed,
batch_size=batch_size,
class_mode=None,target_size=(IMG_HEIGHT,IMG_WIDTH)) #Very important to set this otherwise it returns multiple numpy arrays
mask_data_generator = ImageDataGenerator(**mask_data_gen_args)
mask_generator = mask_data_generator.flow_from_directory(train_masks_path,
seed=seed,
batch_size=batch_size,
color_mode = 'grayscale',
class_mode=None,target_size=(IMG_HEIGHT,IMG_WIDTH))
valid_img_generator = image_data_generator.flow_from_directory(val_images_path,
seed=seed,
batch_size=batch_size,
class_mode=None,target_size=(IMG_HEIGHT,IMG_WIDTH))
valid_mask_generator = mask_data_generator.flow_from_directory(val_masks_path,
seed=seed,
batch_size=batch_size,
color_mode = 'grayscale', #Read masks in grayscale
class_mode=None,target_size=(IMG_HEIGHT,IMG_WIDTH))
train_generator = zip(image_generator, mask_generator)
val_generator = zip(valid_img_generator, valid_mask_generator)
之外,我想使用该功能来更改RGB颜色强度以使模型在弱光条件下更强大。除了上面的代码中所做的增强功能外,任何人都可以向Imagedatagenerator类提供技巧如何从Imagedatagenerator类中实现Apply_transform函数。
I have been trying to augment my around 360 images while training a model. This is how the code looks like
img_data_gen_args = dict(rotation_range=90,
width_shift_range=0.3,
height_shift_range=0.3,
shear_range=0.5,
zoom_range=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode='reflect')
mask_data_gen_args = dict(rotation_range=90,
width_shift_range=0.3,
height_shift_range=0.3,
shear_range=0.5,
zoom_range=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode='reflect',
preprocessing_function = lambda x: np.where(x>0, 1, 0).astype(x.dtype))
image_data_generator = ImageDataGenerator(**img_data_gen_args)
image_generator = image_data_generator.flow_from_directory(train_images_path,
seed=seed,
batch_size=batch_size,
class_mode=None,target_size=(IMG_HEIGHT,IMG_WIDTH)) #Very important to set this otherwise it returns multiple numpy arrays
mask_data_generator = ImageDataGenerator(**mask_data_gen_args)
mask_generator = mask_data_generator.flow_from_directory(train_masks_path,
seed=seed,
batch_size=batch_size,
color_mode = 'grayscale',
class_mode=None,target_size=(IMG_HEIGHT,IMG_WIDTH))
valid_img_generator = image_data_generator.flow_from_directory(val_images_path,
seed=seed,
batch_size=batch_size,
class_mode=None,target_size=(IMG_HEIGHT,IMG_WIDTH))
valid_mask_generator = mask_data_generator.flow_from_directory(val_masks_path,
seed=seed,
batch_size=batch_size,
color_mode = 'grayscale', #Read masks in grayscale
class_mode=None,target_size=(IMG_HEIGHT,IMG_WIDTH))
train_generator = zip(image_generator, mask_generator)
val_generator = zip(valid_img_generator, valid_mask_generator)
Apart from these augmentations I would like to use the functionality to change the RGB color intensities to make the model robust in low light conditions as well. Could anybody give a tip how to implement apply_transform function from the ImageDataGenerator class, in addition to what augmentations done in the code above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
preprocessing_function
添加到img_data_gen_args
字典就像您为mask_data_gen_args
字典一样。在功能内部,您可以根据需要更改RGB颜色强度。您需要做的就是确保该函数采用一个参数(具有等级3的numpy张量),并返回具有相同形状的numpy张量。 在这里 是链接到docs的链接。您的预处理功能看起来像:You can add a
preprocessing_function
to theimg_data_gen_args
dictionary like you do for themask_data_gen_args
dictionary. Inside the function, you can change the RGB color intensities as you like. All you need to do is to make sure that the function takes one argument (Numpy tensor with rank 3), and returns a Numpy tensor with the same shape. Here is the link to the docs. Your preprocessing function would look like so: