首页 > 解决方案 > 如何在 model.predict 解码预测中使用 38 个类而不是 1000 个类

问题描述

每次使用 resnet50 深度学习模型在 decode_predictions 中引发错误消息时,我都会发现植物病害检测错误

错误

需要一批预测(即形状的二维数组(样本,1000))。找到具有形状的数组:(1, 38)"

enter code here


model = ResNet50(weights='imagenet',include_top=False,classes=38)

try:
model = load_model('/content/drive/My 
Drive/color/checkpoints/ResNet50_model_weights.h5')
print("model loaded")  
except:
print("model not loaded")

img_path = '/content/drive/My Drive/color/test/0/appleblackrot188.jpg' 
img = image.load_img(img_path, target_size=(300, 300))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
print('Predicted:', decode_predictions(preds,top=3)[0])

标签: tensorflowkerasdeep-learningtransfer-learning

解决方案


您可以尝试使用预处理功能:

import tensorflow as tf
# Using the keras wrapper on tensorflow (it must be the same using just keras).

IMAGE = [] # From image source, i did it from the camera.

toPred = tf.keras.applications.resnet50.preprocess_input(np.expand_dims(tf.keras.preprocessing.image.img_to_array(IMAGE), axis=0))

也许这可以帮助:)


推荐阅读