0
$\begingroup$
from keras import optimizers
from keras.models import load_model
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
model = load_model('model.h5')
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.load_img('/Images/1.jpg',target_size = (img_width, img_height))
#x= scipy.misc.imread('/Images/1.jpg').shape
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,)

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

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
model = load_model('model.h5')

# Get test image ready
test_image = image.load_img('/Images/1.jpg', target_size=(img_width, img_height))
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).

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Can you explain further on batch size as 1? Why will someone do that? $\endgroup$ – Aditya May 4 '18 at 4:15
  • 1
    $\begingroup$ @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. $\endgroup$ – n1k31t4 May 4 '18 at 9:26
  • $\begingroup$ So an equivalent would be to make multiple copies of the same IMG array and feed it? $\endgroup$ – Aditya May 4 '18 at 10:54
  • $\begingroup$ 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. $\endgroup$ – n1k31t4 May 4 '18 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.