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
期望进行一系列预测(即 2D形状的数组(样本,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.
推荐答案
您应将第一行更改为:
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屋!