Keras:Vgg16——“decode_predictions”错误

Pra*_*aha 5 classification python-2.7 conv-neural-network keras

我正在尝试使用 Keras 中预训练的 VGG16 模型执行图像分类任务。我按照Keras 应用程序页面中的说明编写的代码是:

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]
Run Code Online (Sandbox Code Playgroud)

这与论坛中已经提出的这个问题中显示的代码非常相似。但尽管include_top参数为True,我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!谢谢!

Mar*_*jko 2

这是因为(根据可能在此处找到的函数定义)函数decode_predictions返回一个三元组(class_name, class_description, score)。这就是为什么它声称有太多的价值需要解压。