Reputation: 43
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 ?
Upvotes: 4
Views: 5249
Reputation: 19
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
Upvotes: 1
Reputation: 19
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
Upvotes: 0
Reputation: 4318
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()
.
Upvotes: 2