Search code examples
pythontensorflowconv-neural-networkmobilenet

How to load grayscale image dataset to Mobile net model


I am trying to load a grayscale image dataset(fashion-mnist) to MobileNet model to predict hand written numbers but according to this tutorial only RGB images can be loaded to the model. When I try to feed fashion-mnist samples, it gives me the following error

Error when checking input: expected keras_layer_13_input to have shape (224, 224, 3) but got array with shape (224, 224, 1)

How to solve this problem ?


Solution

  • Probably pre-trained MobileNet is not suitable for this task. You have two different problems. Mobilenet is made for Imagenet images which are 224x224 images with 3 color channels, while MNIST dataset is 28x28 images with one color channel. You can repeat the color channel in RGB:

    # data.shape [70000, 224, 224, 1] -> [70000, 224, 224, 3]
    data = np.repeat(data, 3, -1)
    

    But before that, you need to resize images. For example, you can use PIL for resizing images:

    from PIL import Image
    
    data = np.array([Image.fromarray(x).resize([224,224]) for x in data])
    

    There are some small details here which you should figure out yourself. Such as dtype of the images if you have loaded from the dataset as numpy. You may need to convert numpy types to integers with np.uint8().