manasd28
manasd28

Reputation: 13

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 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.

Upvotes: 1

Views: 10363

Answers (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/

Upvotes: 3

Nicolas Gervais
Nicolas Gervais

Reputation: 36704

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
)

Upvotes: 2

Related Questions