【问题标题】:Keras: ValueError: decode_predictions expects a batch of predictions, NEWKeras:ValueError:decode_predictions 需要一批预测,新
【发布时间】:2021-03-25 17:11:31
【问题描述】:

我正在使用 keras 的预训练模型 VGG16,点击此链接:Transfer learning 我正在尝试预测 image 的内容:

# example of using a pre-trained model as a classifier
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import VGG16
# load an image from file
image = load_img('dog.jpg', target_size=(224, 224))
# convert the image pixels to a numpy array
image = img_to_array(image)
# reshape data for the model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# prepare the image for the VGG model
image = preprocess_input(image)
# load the model
model = VGG16()
# predict the probability across all output classes
yhat = model.predict(image)
# convert the probabilities to class labels
label = decode_predictions(yhat)
# retrieve the most likely result, e.g. highest probability
label = label[0][0]
# print the classification
print('%s (%.2f%%)' % (label[1], label[2]*100))

完整的错误输出:

ValueError: decode_predictions 期望 V1 或 V2 的一批预测(即形状为 (samples, 2622) 的二维数组)或 V2.Found 数组的形状为:(1, 1000)

这是link 对 SO 的一个看似相似的问题。

高度赞赏任何 cmets 和建议。谢谢!

【问题讨论】:

    标签: python tensorflow machine-learning keras deep-learning


    【解决方案1】:

    我运行了您的代码,它工作正常。由于我没有您的图像 dog.jpg 我使用了阿富汗狗的彩色 jpg 图像,并且网络将其正确识别为阿富汗猎犬。所以我怀疑你的形象有问题。正如预期的那样,Yhat 是一个 1 X 1000 数组。确保您的图像是 rgb 图像。

    【讨论】:

    • 感谢您的帮助。我在 Colab 中运行它,并在我导入的不同单元格中进行了早期测试代码: from keras_vggface.vggface import VGGFace from keras_vggface.utils import preprocess_input from keras_vggface.utils import decode_predictions 这就是错误的原因......
    • 我怀疑输入图片有问题。如果满意请采纳答案。波兰人也在这里!
    • 一读到你的回放,我就意识到发生了什么。感谢波兰人民的 Gerry P。
    【解决方案2】:

    感谢您的帮助。我在 Colab 中运行它,并且在我导入的不同单元格中有早期的测试代码:

    from keras_vggface.vggface import VGGFace 
    from keras_vggface.utils import preprocess_input 
    from keras_vggface.utils import decode_predictions
    

    这就是错误的原因.... –

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 2016-10-21
      • 2013-01-29
      • 1970-01-01
      • 2023-03-20
      • 2019-02-14
      相关资源
      最近更新 更多