使用 Keras ImageDataGenerator 更改 RGB 通道强度

发布于 2025-01-19 20:05:17 字数 2837 浏览 1 评论 0原文

在训练模型时,我一直在尝试增加大约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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2025-01-26 20:05:17

您可以将preprocessing_function添加到img_data_gen_args字典就像您为mask_data_gen_args字典一样。在功能内部,您可以根据需要更改RGB颜色强度。您需要做的就是确保该函数采用一个参数(具有等级3的numpy张量),并返回具有相同形状的numpy张量。 在这里 是链接到docs的链接。您的预处理功能看起来像:

def my_preprocessing_function(img):
    # Apply relevant transformation to image here
    return transformed_img

You can add a preprocessing_function to the img_data_gen_args dictionary like you do for the mask_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:

def my_preprocessing_function(img):
    # Apply relevant transformation to image here
    return transformed_img
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文