Keras:ValueError:decode_predictions 需要一批预测 [英] Keras: ValueError: decode_predictions expects a batch of predictions
问题描述
我正在使用 keras 的预训练模型 VGG16,点击此链接:Keras VGG16我正在尝试将预测输出解码为图像中的内容:
I'm using keras' pre-trained model VGG16, following this link: Keras VGG16 I'm trying to decode the prediction output into word of what's in the image:
model = VGG16(weights='imagenet', include_top=False)
img_path = 'elephant.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] #ERROR HERE
完整的错误是:
ValueError: decode_predictions
需要一批预测(即形状的二维数组(样本,1000)).找到具有以下形状的数组:(1, 7,7, 512)
ValueError:
decode_predictions
expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Found array with shape: (1, 7, 7, 512)
非常感谢任何意见或建议.谢谢.
Any comments or suggestion is highly appreciated. Thank you.
推荐答案
您应该将第一行更改为:
You should change a first line to:
model = VGG16(weights='imagenet', include_top=True)
如果没有这条线,您的模型将生成 512 个大小为 7 x 7 像素的特征图.这就是您的错误背后的原因.
Without this line your model is producing a 512 feature maps with size of 7 x 7 pixels. This the reason behind your error.
这篇关于Keras:ValueError:decode_predictions 需要一批预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!