Reputation: 21
During the classification of MNIST data, I am not able to upload the custom image from myself and gives ValueError cannot reshape array of size 2352 into shape (1,28,28,1)
import numpy as np
from google.colab import files
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
uploaded = files.upload()
for fn in uploaded.keys():
path = '/content/' + fn
img = image.load_img(path, target_size =(28, 28))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
images = np.vstack([x])
print(images.shape)
images = images.reshape(1, 28, 28, 1)
print(images.shape)
classes = model.predict(images, batch_size = 10)
Capture.PNG(image/png) - 8252 bytes, last modified: 1/26/2020 - 100% done
Saving Capture.PNG to Capture (1).PNG
(1, 28, 28, 3)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-156-810149b1d6fd> in <module>()
13 images = np.vstack([x])
14 print(images.shape)
---> 15 images = images.reshape(1, 28, 28, 1)
16 print(images.shape)
17 classes = model.predict(images, batch_size = 10)
ValueError: cannot reshape array of size 2352 into shape (1,28,28,1)
Upvotes: 1
Views: 8014
Reputation: 1081
You probably are trying to predict on an RGB image, while the model requires a grayscale image. What would work is if you do
img = img[:,:,0]
right after you load the image and then do the remaining process as it is.
Upvotes: 4