Search code examples
pythontensorflowkerasconv-neural-networkpre-trained-model

Is it possible to feed the pretrained Inception model (tensorflow 2.0/Keras) with 2D grayscale images?


According to Keras 2.0 documentation, in relation to the input shape of the images that can be fed to the pretrained inception model:

input_shape: optional shape tuple, only to be specified if include_top is False (otherwise the input shape has to be (299, 299, 3) (with 'channels_last' data format) or (3, 299, 299) (with 'channels_first' data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 75. E.g. (150, 150, 3) would be one valid value.

However, I am dealing with grayscale image which are 2D. How I should deal with this situation?


Solution

  • You can copy the grayscale image 3 times for a pseudoRGB image

    import numpy as np
    # img=np.zeros((224,224))
    

    If your image is of shape length 2, only width and height you will first need to add an extra dimension:

    img = np.expand_dims(img,-1)
    

    The you repeat this last dimension 3 times:

    img = np.repeat(img,3,2)
    print(img.shape)
    # (224,224,3)