# how to predict an image using saved model

from keras import optimizers
from keras.preprocessing import image
import numpy as np
import cv2
import scipy.misc
from keras.wrappers.scikit_learn import KerasClassifier
# dimensions of our images
img_width, img_height = 313, 220
# load the model we saved
sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy','mse'])
test_image = image.img_to_array(test_image)
print test_image
test_image = np.expand_dims(test_image, axis = 0)
test_image = test_image.reshape(img_width, img_height*3)
result = model.predict(test_image)
print result


When I ran the above code I get this error:

ValueError: Error when checking : expected dense_1_input to have shape (36,) but got array with shape (660,)

• model.predict accepts mini batches May 3 '18 at 17:07
• Try to use model.summary to see which dimensions are used for your layers. May 3 '18 at 18:42
• input_dim=36 but still don't know how to fix this problem May 3 '18 at 18:56
• @Jennifer95 - if an answer helped you, please accept it by clicking on the green tick, so we can mark the question as resolved. Thanks :) May 21 '18 at 10:56

Having problems with dimensions is very common. As others have mentioned, the method predict expects to get a batch of images. You should run model.summary() to see what the expected dimensions of the input are. The batch size itself might have been designed to be flexible during training, by specifying None in the first dimension on the model input_shape parameter.

Without knowing more of the dimensions, I cannot say exactly where your problems lies. You could include that information by editing your question.

It will likely help to have a look at the documentation for predict function of the model objects.

There is an argument: batch_size, which defaults to 32 if not fixed by the model itself, which you can see from the model.summary(). If you set this equal to 1, perhaps you will get a prediction.

Below is a modified version of your code that I would expect to return a prediction.

from keras.models import load_model
from keras.preprocessing import image
import numpy as np

# dimensions of our images    -----   are these then grayscale (black and white)?
img_width, img_height = 313, 220

# load the model we saved

test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)

test_image = test_image.reshape(img_width, img_height*3)    # Ambiguity!
# Should this instead be: test_image.reshape(img_width, img_height, 3) ??

result = model.predict(test_image, batch_size=1)
print result


Note: there is ambiguity in the dimensions where I highlight it. A colour image will have three dimensions: (height, width, 3). Black and white will have only two dimensions: (height, width).

• Can you explain further on batch size as 1? Why will someone do that? May 4 '18 at 4:15
• @Aditya - The question was how to get a prediction for a single image. If we wanted to predict on 10 images, we could make batch_size = 10. When using the predict function, we are not updating the weights of the model; no backpropagation is performed. That means the batch_size argument really just tells the model how many images we are pushing through the network. May 4 '18 at 9:26
• So an equivalent would be to make multiple copies of the same IMG array and feed it? May 4 '18 at 10:54
• Yep - that would give then an identical prediction, given identical inputs, as random behaviour is not present at test time. It is a bit of a hack though, which is why I opted to answer with the more computationally efficient method. May 4 '18 at 12:52