eval.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import logging
  2. logging.basicConfig(
  3. format='%(asctime)s [%(levelname)s] - %(message)s',
  4. level=logging.INFO)
  5. import os
  6. from vi_cls.cls_pred import cls_pred
  7. from config import config
  8. class eval(cls_pred):
  9. def __init__(self, cfg, args=None):
  10. cls_pred.__init__(self, cfg, args)
  11. self.num_total = 0
  12. self.num_ok = 0
  13. self.num_ng = 0
  14. self.label_class = None
  15. self.pred_class = None
  16. #------------------------------------------------------------------------------
  17. # Predict Image Files
  18. #------------------------------------------------------------------------------
  19. def pred_file_startup(self, img_dir, names):
  20. self.num_total = len(names)
  21. self.num_ok = 0
  22. self.num_ng = 0
  23. # the parent folder name is the class_name
  24. self.label_class = os.path.split(img_dir)[1]
  25. if self.label_class == '':
  26. self.label_class = os.path.split(self.label_class)[1]
  27. def before_pred_file(self, img_file):
  28. split_num = 2
  29. # if img_file.find('100X') > 0:
  30. # split_num = 2
  31. # elif img_file.find('200X') > 0:
  32. # split_num = 2
  33. # elif img_file.find('500X') > 0:
  34. # split_num = 1
  35. # elif img_file.find('1000X') > 0:
  36. # split_num = 1
  37. self.set_arg('splits_num', split_num)
  38. def after_pred_file(self, det_dict):
  39. self.pred_class = ''
  40. if len(det_dict) > 0:
  41. dets = sorted(det_dict.items(), lambda x, y : cmp(x[1], y[1]), reverse=True)
  42. self.pred_class = dets[0][0]
  43. def get_pred_result_text(self, det_dict, im_inp):
  44. if self.pred_class is not None:
  45. pred_items = self.pred_class.rpartition('-')
  46. label_items = self.label_class.rpartition('-')
  47. if pred_items[0] == label_items[0]:
  48. self.num_ok = self.num_ok + 1
  49. return 'OK'
  50. self.num_ng = self.num_ng + 1
  51. return 'NG'
  52. def get_summary_text_lines(self):
  53. ok_percent = 0
  54. ng_percent = 0
  55. if self.num_total > 0:
  56. ok_percent = float(self.num_ok) / self.num_total
  57. ng_percent = float(self.num_ng) / self.num_total
  58. lines = []
  59. lines.append('')
  60. lines.append('')
  61. lines.append('{:>10}: {:5} ({:.2%})'.format('OK', self.num_ok, ok_percent))
  62. lines.append('{:>10}: {:5} ({:.2%})'.format('NG', self.num_ng, ng_percent))
  63. lines.append('')
  64. return lines
  65. if __name__ == '__main__':
  66. cfg = config()
  67. e = eval(cfg)
  68. split_num = 2
  69. crop_size = 224
  70. e.set_arg('gray_image', 'False')
  71. e.set_arg('resize_w', crop_size * split_num)
  72. e.set_arg('resize_h', crop_size * split_num)
  73. e.set_arg('crop_w', crop_size)
  74. e.set_arg('crop_h', crop_size)
  75. root_dir = e._args.img_dir
  76. lines = []
  77. lines.append('')
  78. lines.append('')
  79. names = os.listdir(root_dir)
  80. for name in names:
  81. img_dir = os.path.join(root_dir, name)
  82. e.set_arg('img_dir', img_dir)
  83. e.pred_file()
  84. ok_percent = 0
  85. if e.num_total > 0:
  86. ok_percent = float(e.num_ok) / e.num_total
  87. lines.append('{:>20}: {:.2%} ({}/{})'.format(name, ok_percent, e.num_ok, e.num_total))
  88. summary_file = open(os.path.join(e._args.pred_dir, 'summary.txt'), 'w')
  89. for line in lines:
  90. summary_file.write(line + '\n')
  91. logging.info(line)
  92. summary_file.close()