1

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
manasd28
  • 13
  • 1
  • 4

2 Answers2

2

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
)
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
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/