首页 文章

Keras:ValueError:decode_predictions需要一批预测

提问于
浏览
4

我'm using keras'预先训练过的模型VGG16,跟随此链接:Keras VGG16 I 'm trying to decode the prediction output into word of what' s在图像中:

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)的数组

任何意见或建议都非常感谢 . 谢谢 .

2 回答

  • 9

    您应该将第一行更改为:

    model = VGG16(weights='imagenet', include_top=True)
    

    如果没有此行,您的模型将生成512个大小为7 x 7像素的要素图 . 这就是你的错误背后的原因 .

  • 0

    只是为了添加@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")
    

相关问题