简体   繁体   English

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

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

I'm training a Keras model and I have training images in RGB format.我正在训练 Keras model 并且我有 RGB 格式的训练图像。 I want to train my model but on grayscale images on InceptionV3, but it takes RGB images as input.我想训练我的 model 但在 InceptionV3 上的灰度图像上,但它需要 RGB 图像作为输入。 My question is: How can I first convert the RGB images into grayscale, then make a copy in 3 dimensions?我的问题是:如何先将 RGB 图像转换为灰度图像,然后进行 3 维复制? I'm using Keras' TrainDataGenerator.flow_from_directory method.我正在使用 Keras 的TrainDataGenerator.flow_from_directory方法。

In ImageDataGenerator , you can pass a preprocessing function.ImageDataGenerator中,您可以传递一个预处理 function。 Use the functions tf.image.rgb_to_grayscale and tf.image.grayscale_to_rgb to do the transformation:使用函数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
)

I think the simplest way to do is to use the color_mode ="grayscale" parameter of the TrainDataGenerator.flow_from_directory method.我认为最简单的方法是使用TrainDataGenerator.flow_from_directory方法的color_mode ="grayscale"参数。

Here is the keras online documentation that specifies how to do this https://keras.io/api/preprocessing/image/这是指定如何执行此操作的 keras 在线文档https://keras.io/api/preprocessing/image/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM