如何将 RGB 图像转换为灰度图像,并扩展该灰度图像的尺寸以在 InceptionV3 中使用?

How to convert RGB images to grayscale, expand dimensions of that grayscale image to use in InceptionV3?

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

ImageDataGenerator, you can pass a preprocessing function. Use the functions tf.image.rgb_to_grayscale and tf.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
)

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

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