繁体   English   中英

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

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

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

ImageDataGenerator中,您可以传递一个预处理 function。 使用函数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
)

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

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

暂无
暂无

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

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