【问题标题】:How can i use 38 classes instead of 1000 in model.predict decode predictions如何在 model.predict 解码预测中使用 38 个类而不是 1000 个类
【发布时间】:2019-08-17 03:23:34
【问题描述】:

每次使用 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])

【问题讨论】:

  • 您的帖子很难理解。请检查formatting help - 我认为第一段需要a little bit formatting
  • 先生,我想预测 38 个类别之间的植物图像,但它在代码中显示最后一行代码有错误,请检查代码

标签: tensorflow keras deep-learning transfer-learning


【解决方案1】:

首先,您需要一个索引 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 results

【讨论】:

    【解决方案2】:

    decode_predictions 仅适用于 ImageNet(类数 = 1000)。对于这 38 类植物,您必须根据为每种植物分配的地面实况标签编写自己的解码预测。

    【讨论】:

      【解决方案3】:

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

      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))
      

      也许这会有所帮助:)

      【讨论】:

      • 先生,我试过这个方法,但我提出了同样的错误,请帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 2019-07-17
      • 2018-07-15
      • 1970-01-01
      • 2018-07-15
      • 2016-12-04
      • 2017-10-03
      相关资源
      最近更新 更多