4

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 ?

BiBi
  • 7,418
  • 5
  • 43
  • 69
rasindu alwis
  • 43
  • 1
  • 3
  • Welcome to stackoverflow. Please, show us a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and you'll get help – gehbiszumeis Aug 05 '19 at 11:03

3 Answers3

2

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

Mehdi
  • 4,202
  • 5
  • 20
  • 36
  • The TensorFlow equivalent would be: `tf.tile(data, [1, 1, 1, 3])`. – BiBi Aug 05 '19 at 11:03
  • I am loading dataset from this code `splits, info = tfds.load('mnist', with_info=True, as_supervised=True, split = splits)` so that how to apply above solution for this – rasindu alwis Aug 05 '19 at 11:13
  • How do you resize the images? They are not 224x224 in mnist! – Mehdi Aug 05 '19 at 11:19
  • `hub.KerasLayer(CLASSIFIER_URL, input_shape=(224, 224, 3))` this is how I resize the images – rasindu alwis Aug 05 '19 at 11:23
  • but that is not going to resize MNIST images. they have 28 x 28 pixels. – Mehdi Aug 05 '19 at 11:27
  • So than what is the best way to do this? Can't we use MobileNet model to predict greyscale images ? – rasindu alwis Aug 05 '19 at 11:29
  • I tried to add more explanation. In my solution, I used numpy. Perhaps, there are other solutions in tensorflow as well. i.e. https://www.tensorflow.org/api_docs/python/tf/image/resize_images – Mehdi Aug 05 '19 at 12:01
1

Mobilenet v2 needs RGB. You might also be able to use the convert function from PIL.

Try this:

from PIL import Image
x= Image.open(input_image).resize((96,96)).convert("RGB")

documentation is here: https://pillow.readthedocs.io/en/stable/reference/Image.html

David Buck
  • 3,752
  • 35
  • 31
  • 35
uhaz
  • 19
  • 3
0

try this, x = np.stack((x,)*3, axis=-1). Please refer to this link for more details: https://github.com/malnakli/ML/blob/master/tf_serving_keras_mobilenetv2/main.ipynb

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
uhaz
  • 19
  • 3