metric.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # coding:utf-8
  2. import os
  3. os.environ['CUDA_VISIBLE_DEVICES']='0'
  4. import numpy as np
  5. import shutil
  6. import paddlex as pdx
  7. def cal_image_level(model, dataset_dir):
  8. file_list = os.path.join(dataset_dir, 'test_list_2.txt')
  9. threshold = 0.4
  10. # threshold = 0.5
  11. matrix = [[0,0],[0,0]]
  12. fire_to_no = []
  13. no_to_fire = []
  14. # 观察结果错误的图片
  15. fire_to_no_path = 'metric_fire_to_no'
  16. no_to_fire_path = 'metric_no_to_fire'
  17. if not os.path.exists(fire_to_no_path):
  18. os.makedirs(fire_to_no_path)
  19. else:
  20. shutil.rmtree(fire_to_no_path)
  21. os.makedirs(fire_to_no_path)
  22. if not os.path.exists(no_to_fire_path):
  23. os.makedirs(no_to_fire_path)
  24. else:
  25. shutil.rmtree(no_to_fire_path)
  26. os.makedirs(no_to_fire_path)
  27. with open(file_list, 'r') as fr:
  28. while True:
  29. line = fr.readline()
  30. if not line:
  31. break
  32. img_file, label = line.strip().split()[:2]
  33. img_file = os.path.join(dataset_dir, img_file)
  34. label = int(label)
  35. res = model.predict(img_file)
  36. keep_results = []
  37. areas = []
  38. for dt in res:
  39. cname, bbox, score = dt['category'], dt['bbox'], dt['score']
  40. if score < threshold:
  41. continue
  42. keep_results.append(dt)
  43. areas.append(bbox[2] * bbox[3])
  44. areas = np.asarray(areas)
  45. sorted_idxs = np.argsort(-areas).tolist()
  46. keep_results = [keep_results[k]
  47. for k in sorted_idxs] if keep_results else []
  48. if len(keep_results)>0:
  49. predict_label = 1
  50. else:
  51. predict_label = 0
  52. if label == 1:
  53. if label == predict_label:
  54. matrix[0][0]+=1
  55. else:
  56. matrix[1][0]+=1
  57. fire_to_no.append(img_file)
  58. name = os.path.basename(img_file)
  59. shutil.copyfile(img_file, os.path.join(fire_to_no_path, name))
  60. else:
  61. if label == predict_label:
  62. matrix[1][1]+=1
  63. else:
  64. matrix[0][1]+=1
  65. no_to_fire.append(img_file)
  66. # 绘制结果
  67. pdx.det.visualize(img_file, keep_results, threshold=threshold, save_dir=no_to_fire_path)
  68. recall = matrix[0][0] / (matrix[0][0]+matrix[1][0])
  69. error = matrix[0][1] / (matrix[0][1]+matrix[1][1])
  70. print('===matrix:',matrix)
  71. print('===recall:',recall)
  72. print('===error:',error)
  73. print('===烟火图被判定为无烟火的图片包含:',len(fire_to_no))
  74. print('===无烟火图被判定为烟火的图片包含:',len(no_to_fire))
  75. return recall, error
  76. def select_best(dataset_dir, model_dirs):
  77. max_recall = 0
  78. min_error = 100
  79. best_recall = [0,0]
  80. best_error = [0,0]
  81. for model_dir in sorted(os.listdir(model_dirs)):
  82. if 'epoch' in model_dir or 'best_model' in model_dir:
  83. model_dir = os.path.join(model_dirs, model_dir)
  84. model = pdx.load_model(model_dir)
  85. recall, error = cal_image_level(model, dataset_dir)
  86. if recall>max_recall:
  87. best_recall = [model_dir, recall, error]+best_recall
  88. max_recall = recall
  89. if error < min_error:
  90. best_error = [model_dir, recall, error]+best_error
  91. min_error = error
  92. else:
  93. continue
  94. print('==best recall:',best_recall[:-2])
  95. print('====best error:',best_error[:-2])
  96. print('====final best:',best_recall[0],best_error[0])
  97. if __name__ == '__main__':
  98. dataset_dir = 'eval_imgs'
  99. model_dirs = 'output/ppyolov2_r50vd_dcn/'
  100. select_best(dataset_dir, model_dirs)
  101. # # model_dir = 'output/ppyolov2_r50vd_dcn/best_model/'
  102. # model = pdx.load_model(model_dir)
  103. # recall, error = cal_image_level(model, dataset_dir)