【问题标题】:How to test multi class/label image classifier?如何测试多类/标签图像分类器?
【发布时间】:2021-02-07 05:18:08
【问题描述】:

我正在尝试测试我的图像分类器的准确性。 我只做过二进制分类,对深度学习还很陌生。

我真的不想做混淆矩阵。 我宁愿一张一张地对图片进行分类。

有谁知道如何用图像一一测试分类器的准确性? 使用代码 sn-p 将非常有帮助

这是我的代码

我只是做了我通常在二进制分类中所做的事情。 但是我现在卡住了如何分类

model = load_model('./model.h5')

test_image = ImageDataGenerator(
    rescale=1. / 255
)

test_generator = test_image.flow_from_directory(
    './test',
    (32, 32),
    batch_size=20,
    class_mode='categorical',
    shuffle=False
)

loss, accuracy = model.evaluate_generator(
    generator=test_generator,
    steps=1
)

labels = test_generator.class_indices

【问题讨论】:

    标签: python tensorflow keras deep-learning multilabel-classification


    【解决方案1】:

    我假设您使用的是 TF >= 2.0

    你可以试试这个:

    import numpy as np
    from keras.preprocessing import image
    
    path = 'path-to-file'
    img = image.load_img(path, target_size=(32, 32))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    
    images = np.vstack([x])
    classes = model.predict(images, batch_size=10)
    

    【讨论】:

      【解决方案2】:

      在“./test”目录中放置您要分类的图像。然后使用下面的代码打印出每张图片的预测类、每张图片的真实类以及图片的文件名。

      file_names=test_generator.filenames  # save list of test files names to be used later
      tlabels=test_generator.labels # save test labels to be used later
      class_dict=test_generator.class_indices
      # code below determines test batch size and test steps
      # so you go through the test images exactly once
      length=len(file_names) # determine number of images
      b_max=80 # set maximum batch size you will allow
      test_batch_size=sorted([int(length/n) for n in range(1,length+1) if length % n ==0 and length/n<=b_max],reverse=True)[0]
      test_steps=int(length/batch_size)
      # make predictions
      preds=model.predict(test_gen, batch_size=test_batch_size, verbose=0, steps=test_steps)
      new_dict={} 
      for key in class_dict: # set key in new_dict to value in class_dict and value in new_dict to key in class_dict
          value=class_dict[key]
          new_dict[value]=key
      print('PREDICTED CLASS     TRUE CLASS       FILENAME            ERROR  STATUS' ) # adjust spacing based on your class names
      error_list=[] # empty list to store if the prediction was correct or not
      error_file_list=[]
      for i, p in enumerate(preds):
          pred_index=np.argmax(p) # get the index that has the highest probability
          if pred_index == tlabels[i]:
              error_list.append ('No') # correct classification
          else:
              error_list.append('Yes')
              error_file_list.append(file_names[i])
          pred_class=new_dict[pred_index]  # find the predicted class based on the index
          true_class=new_dict[tlabels[i]] # use the test label to get the true class of the test file
          file=file_names[i]
          print(f'    {pred_class:10s}       {true_class:10s}    {file:25s}   {error_list[i]}')
      

      【讨论】:

      • 感谢@Gerry P. 但我从这一行“ preds=model.predict(test_generator, batch_size=20, verbose=0) 收到此错误“UnboundLocalError: local variable 'batch_outputs' referenced before assignment” , steps=test_steps)"
      • 我编辑了我的答案,因为它只会显示有错误的文件。出现错误的行是您生成的行。只需使用我提供的代码,它应该可以正常工作。
      • 我还对其进行了修改以添加一列 ERROR STATUS,如果分类错误,则为 Yes,如果分类正确,则为 No。还添加了一个列表 error_file_list,它将是错误分类的文件名列表。
      猜你喜欢
      • 2020-03-07
      • 2019-03-25
      • 1970-01-01
      • 2018-12-01
      • 2023-01-20
      • 2020-05-10
      • 2017-07-10
      • 2021-11-18
      • 2019-04-02
      相关资源
      最近更新 更多