每次在decode_predictions中引发错误消息时,我都会在使用resnet50深度学习模型检测植物病害时发现错误
错误
期望一批预测(即形状的二维数组(样本,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])发布于 2019-03-27 00:57:32
您可以尝试使用预处理函数:
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))这可能会有所帮助:)
发布于 2019-03-27 02:15:45
decode_predictions仅适用于ImageNet (no.类的数量= 1000)。对于这38类植物,你必须根据你为每种植物分配的地面实况标签编写自己的解码预测。
发布于 2020-06-04 01:49:52
首先,您需要一个索引JSON文件并创建一个新的decode_predictions函数。例如
这个HAM10000有7个类,您需要像这样拆分到每个文件夹

然后创建一个索引JSON文件,如下所示
{
"0" : [
"AKIEC",
"akiec"
],
"1" : [
"BCC",
"bcc"
],
"2" : [
"BKL",
"bkl"
],
"3" : [
"DF",
"df"
],
"4" : [
"MEL",
"mel"
],
"5" : [
"NV",
"nv"
],
"6" : [
"VASC",
"vasc"
]}
然后试试下面的代码
def decode_predictions(preds, top=4, class_list_path='/content/SKIN_DATA/HAM10000_index.json'):
if len(preds.shape) != 2 or preds.shape[1] != 7: # your classes number
raise ValueError('`decode_predictions` expects '
'a batch of predictions '
'(i.e. a 2D array of shape (samples, 1000)). '
'Found array with shape: ' + str(preds.shape))
index_list = json.load(open(class_list_path))
results = []
for pred in preds:
top_indices = pred.argsort()[-top:][::-1]
result = [tuple(index_list[str(i)]) + (pred[i],) for i in top_indices]
result.sort(key=lambda x: x[2], reverse=True)
results.append(result)
return resultshttps://stackoverflow.com/questions/55362315
复制相似问题