【发布时间】: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