【问题标题】:How to convert RGB images to grayscale, expand dimensions of that grayscale image to use in InceptionV3?如何将 RGB 图像转换为灰度,扩展该灰度图像的尺寸以在 InceptionV3 中使用?
【发布时间】:2021-03-18 15:31:40
【问题描述】:

我正在训练一个 Keras 模型,并且我有 RGB 格式的训练图像。我想训练我的模型,但在 InceptionV3 上使用灰度图像,但它需要 RGB 图像作为输入。我的问题是:如何先将 RGB 图像转换为灰度图像,然后进行 3 维复制?我正在使用 Keras 的 TrainDataGenerator.flow_from_directory 方法。

【问题讨论】:

    标签: python tensorflow image-processing keras data-augmentation


    【解决方案1】:

    ImageDataGenerator 中,您可以传递一个预处理函数。使用函数tf.image.rgb_to_grayscaletf.image.grayscale_to_rgb 进行转换:

    def to_grayscale_then_rgb(image):
        image = tf.image.rgb_to_grayscale(image)
        image = tf.image.grayscale_to_rgb(image)
        return image
    
    tf.keras.preprocessing.image.ImageDataGenerator(
        rescale=1/255,
        preprocessing_function=to_grayscale_then_rgb
    )
    

    【讨论】:

      【解决方案2】:

      我认为最简单的方法是使用 TrainDataGenerator.flow_from_directory 方法的 color_mode ="grayscale" 参数。

      这里是指定如何执行此操作的 keras 在线文档 https://keras.io/api/preprocessing/image/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-26
        • 2014-12-22
        • 2018-08-15
        • 1970-01-01
        • 2016-11-05
        • 2013-10-25
        • 1970-01-01
        相关资源
        最近更新 更多