我正在使用keras'预先训练的模型VGG16,点击此链接:Keras VGG16 我试图将预测输出解码为图像中的内容:
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)
我们非常感谢您提出任何意见或建议。谢谢。
答案 0 :(得分:12)
您应该将第一行更改为:
model = VGG16(weights='imagenet', include_top=True)
如果没有此行,您的模型将生成512个大小为7 x 7像素的要素图。这就是你的错误背后的原因。
答案 1 :(得分:1)
只需添加@MarcinMożejko的正确答案
这同样适用于其他可用型号,因此您必须始终包含前三层:
vgg19 <- application_vgg19(include_top = TRUE, weights = "imagenet")
model_resnet50 <- application_resnet50(include_top = TRUE, weights = "imagenet")
model_inception_v3 <- application_inception_v3(include_top = TRUE, weights = "imagenet")
model_xception <- application_xception(include_top = TRUE, weights = "imagenet")