如何将 RGB 图像转换为灰度,扩展该灰度图像的尺寸以在 InceptionV3 中使用? [英] How to convert RGB images to grayscale, expand dimensions of that grayscale image to use in InceptionV3?

查看:58
本文介绍了如何将 RGB 图像转换为灰度,扩展该灰度图像的尺寸以在 InceptionV3 中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm training a Keras model and I have training images in RGB format. I want to train my model but on grayscale images on InceptionV3, but it takes RGB images as input. My question is: How can I first convert the RGB images into grayscale, then make a copy in 3 dimensions? I'm using Keras' TrainDataGenerator.flow_from_directory method.

推荐答案

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

In ImageDataGenerator, you can pass a preprocessing function. Use the functions tf.image.rgb_to_grayscale and tf.image.grayscale_to_rgb to do the transformation:

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
)

这篇关于如何将 RGB 图像转换为灰度,扩展该灰度图像的尺寸以在 InceptionV3 中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆