Skip to content Skip to sidebar Skip to footer

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. I want to train my model but on grayscale images on InceptionV3, but it takes RGB images as input. My question

Solution 1:

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
)

Solution 2:

I think the simplest way to do is to use the color_mode ="grayscale" parameter of the TrainDataGenerator.flow_from_directory method.

Here is the keras online documentation that specifies how to do this https://keras.io/api/preprocessing/image/

Post a Comment for "How To Convert Rgb Images To Grayscale, Expand Dimensions Of That Grayscale Image To Use In Inceptionv3?"