5

I am trying to perform an image classification task using a pre-trained VGG16 model in Keras. The code I wrote, following the instructions in the Keras application page, is:

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np

model = VGG16(weights='imagenet', include_top=True)
img_path = './train/cat.1.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)
(inID, label) = decode_predictions(features)[0]

which is quite similar to the code shown in this question already asked in the forum. But in spite of having the include_top parameter as True, I am getting the following error:

Traceback (most recent call last):
  File "vgg16-keras-classifier.py", line 14, in <module>
    (inID, label) = decode_predictions(features)[0]
ValueError: too many values to unpack

Any help will be deeply appreciated! Thanks!

Community
  • 1
  • 1
Prabaha
  • 879
  • 2
  • 9
  • 19

1 Answers1

2

It's because (according to a function definition which might be found here) a function decode_predictions returns a triple (class_name, class_description, score). This why it claims that there are too many values to unpack.

Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
  • 1
    Thanks for the answer! Turns out, if you just use `result=decode_predictions(features)` then the output actually is within another array, like `[[(a1,b1,c1),...,(a5,b5,c5)]]`. So, to get the top result, one has to write, `decode_predictions(features)[0][0]` – Prabaha May 16 '17 at 22:50