0

I'm using keras' pre-trained model VGG16, following this link: Transfer learning I'm trying predict content of an image:

# example of using a pre-trained model as a classifier
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import VGG16
# load an image from file
image = load_img('dog.jpg', target_size=(224, 224))
# convert the image pixels to a numpy array
image = img_to_array(image)
# reshape data for the model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# prepare the image for the VGG model
image = preprocess_input(image)
# load the model
model = VGG16()
# predict the probability across all output classes
yhat = model.predict(image)
# convert the probabilities to class labels
label = decode_predictions(yhat)
# retrieve the most likely result, e.g. highest probability
label = label[0][0]
# print the classification
print('%s (%.2f%%)' % (label[1], label[2]*100))

Full Error Output:

ValueError: decode_predictions expects a batch of predictions (i.e. a 2D array of shape (samples, 2622)) for V1 or (samples, 8631) for V2.Found array with shape: (1, 1000)

This is link to a seemingly similar question on SO.

Any comments and suggestions highly appreciated. Thank you!

2 Answers2

0

I ran your code and it works properly. Since I do not have your image dog.jpg I used a color jpg image of an Afghan dog and the network identified it correctly as an Afghan Hound. So I suspect there is something amiss with your image. Yhat is a 1 X 1000 array as expected. Ensure you image is an rgb image.

Gerry P
  • 7,662
  • 3
  • 10
  • 20
  • thank you for your help. I was running this in Colab and had earlier tests code where in different cell i have imported for: from keras_vggface.vggface import VGGFace from keras_vggface.utils import preprocess_input from keras_vggface.utils import decode_predictions That was the reason for the error.... – Mariusz Lewandowski Dec 16 '20 at 00:17
  • 1
    I suspected something was wrong in inputting the image. If satisfied please accept answer. Polish guy here too! – Gerry P Dec 16 '20 at 01:09
  • I have realised what is going on as soon as a read your replay. Thank you Gerry P from the Polish people. – Mariusz Lewandowski Dec 20 '20 at 22:29
0

thank you for your help. I was running this in Colab and had earlier tests code where in different cell i have imported for:

from keras_vggface.vggface import VGGFace 
from keras_vggface.utils import preprocess_input 
from keras_vggface.utils import decode_predictions

That was the reason for the error.... –