map_utils.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from __future__ import unicode_literals
  18. import os
  19. import sys
  20. import numpy as np
  21. import itertools
  22. import paddlex.utils.logging as logging
  23. __all__ = [
  24. '_draw_pr_curve', 'bbox_area', 'jaccard_overlap', 'prune_zero_padding',
  25. 'DetectionMAP'
  26. ]
  27. def _draw_pr_curve(precision,
  28. recall,
  29. iou=0.5,
  30. out_dir='pr_curve',
  31. file_name='precision_recall_curve.jpg'):
  32. if not os.path.exists(out_dir):
  33. os.makedirs(out_dir)
  34. output_path = os.path.join(out_dir, file_name)
  35. try:
  36. import matplotlib.pyplot as plt
  37. except Exception as e:
  38. logging.error('Matplotlib not found, plaese install matplotlib.'
  39. 'for example: `pip install matplotlib`.')
  40. raise e
  41. plt.cla()
  42. plt.figure('P-R Curve')
  43. plt.title('Precision/Recall Curve(IoU={})'.format(iou))
  44. plt.xlabel('Recall')
  45. plt.ylabel('Precision')
  46. plt.grid(True)
  47. plt.plot(recall, precision)
  48. plt.savefig(output_path)
  49. def bbox_area(bbox, is_bbox_normalized):
  50. """
  51. Calculate area of a bounding box
  52. """
  53. norm = 1. - float(is_bbox_normalized)
  54. width = bbox[2] - bbox[0] + norm
  55. height = bbox[3] - bbox[1] + norm
  56. return width * height
  57. def jaccard_overlap(pred, gt, is_bbox_normalized=False):
  58. """
  59. Calculate jaccard overlap ratio between two bounding box
  60. """
  61. if pred[0] >= gt[2] or pred[2] <= gt[0] or \
  62. pred[1] >= gt[3] or pred[3] <= gt[1]:
  63. return 0.
  64. inter_xmin = max(pred[0], gt[0])
  65. inter_ymin = max(pred[1], gt[1])
  66. inter_xmax = min(pred[2], gt[2])
  67. inter_ymax = min(pred[3], gt[3])
  68. inter_size = bbox_area([inter_xmin, inter_ymin, inter_xmax, inter_ymax],
  69. is_bbox_normalized)
  70. pred_size = bbox_area(pred, is_bbox_normalized)
  71. gt_size = bbox_area(gt, is_bbox_normalized)
  72. overlap = float(inter_size) / (pred_size + gt_size - inter_size)
  73. return overlap
  74. def prune_zero_padding(gt_box, gt_label, difficult=None):
  75. valid_cnt = 0
  76. for i in range(len(gt_box)):
  77. if gt_box[i, 0] == 0 and gt_box[i, 1] == 0 and \
  78. gt_box[i, 2] == 0 and gt_box[i, 3] == 0:
  79. break
  80. valid_cnt += 1
  81. return (gt_box[:valid_cnt], gt_label[:valid_cnt], difficult[:valid_cnt]
  82. if difficult is not None else None)
  83. class DetectionMAP(object):
  84. """
  85. Calculate detection mean average precision.
  86. Currently support two types: 11point and integral
  87. Args:
  88. class_num (int): The class number.
  89. overlap_thresh (float): The threshold of overlap
  90. ratio between prediction bounding box and
  91. ground truth bounding box for deciding
  92. true/false positive. Default 0.5.
  93. map_type (str): Calculation method of mean average
  94. precision, currently support '11point' and
  95. 'integral'. Default '11point'.
  96. is_bbox_normalized (bool): Whether bounding boxes
  97. is normalized to range[0, 1]. Default False.
  98. evaluate_difficult (bool): Whether to evaluate
  99. difficult bounding boxes. Default False.
  100. catid2name (dict): Mapping between category id and category name.
  101. classwise (bool): Whether per-category AP and draw
  102. P-R Curve or not.
  103. """
  104. def __init__(self,
  105. class_num,
  106. overlap_thresh=0.5,
  107. map_type='11point',
  108. is_bbox_normalized=False,
  109. evaluate_difficult=False,
  110. catid2name=None,
  111. classwise=False):
  112. self.class_num = class_num
  113. self.overlap_thresh = overlap_thresh
  114. assert map_type in ['11point', 'integral'], \
  115. "map_type currently only support '11point' "\
  116. "and 'integral'"
  117. self.map_type = map_type
  118. self.is_bbox_normalized = is_bbox_normalized
  119. self.evaluate_difficult = evaluate_difficult
  120. self.classwise = classwise
  121. self.classes = []
  122. for cname in catid2name.values():
  123. self.classes.append(cname)
  124. self.reset()
  125. def update(self, bbox, score, label, gt_box, gt_label, difficult=None):
  126. """
  127. Update metric statics from given prediction and ground
  128. truth infomations.
  129. """
  130. if difficult is None:
  131. difficult = np.zeros_like(gt_label)
  132. # record class gt count
  133. for gtl, diff in zip(gt_label, difficult):
  134. if self.evaluate_difficult or int(diff) == 0:
  135. self.class_gt_counts[int(np.array(gtl))] += 1
  136. # record class score positive
  137. visited = [False] * len(gt_label)
  138. for b, s, l in zip(bbox, score, label):
  139. xmin, ymin, xmax, ymax = b.tolist()
  140. pred = [xmin, ymin, xmax, ymax]
  141. max_idx = -1
  142. max_overlap = -1.0
  143. for i, gl in enumerate(gt_label):
  144. if int(gl) == int(l):
  145. overlap = jaccard_overlap(pred, gt_box[i],
  146. self.is_bbox_normalized)
  147. if overlap > max_overlap:
  148. max_overlap = overlap
  149. max_idx = i
  150. if max_overlap > self.overlap_thresh:
  151. if self.evaluate_difficult or \
  152. int(np.array(difficult[max_idx])) == 0:
  153. if not visited[max_idx]:
  154. self.class_score_poss[int(l)].append([s, 1.0])
  155. visited[max_idx] = True
  156. else:
  157. self.class_score_poss[int(l)].append([s, 0.0])
  158. else:
  159. self.class_score_poss[int(l)].append([s, 0.0])
  160. def reset(self):
  161. """
  162. Reset metric statics
  163. """
  164. self.class_score_poss = [[] for _ in range(self.class_num)]
  165. self.class_gt_counts = [0] * self.class_num
  166. self.mAP = None
  167. def accumulate(self):
  168. """
  169. Accumulate metric results and calculate mAP
  170. """
  171. mAP = 0.
  172. valid_cnt = 0
  173. eval_results = []
  174. for score_pos, count in zip(self.class_score_poss,
  175. self.class_gt_counts):
  176. if count == 0: continue
  177. if len(score_pos) == 0:
  178. valid_cnt += 1
  179. continue
  180. accum_tp_list, accum_fp_list = \
  181. self._get_tp_fp_accum(score_pos)
  182. precision = []
  183. recall = []
  184. for ac_tp, ac_fp in zip(accum_tp_list, accum_fp_list):
  185. precision.append(float(ac_tp) / (ac_tp + ac_fp))
  186. recall.append(float(ac_tp) / count)
  187. one_class_ap = 0.0
  188. if self.map_type == '11point':
  189. max_precisions = [0.] * 11
  190. start_idx = len(precision) - 1
  191. for j in range(10, -1, -1):
  192. for i in range(start_idx, -1, -1):
  193. if recall[i] < float(j) / 10.:
  194. start_idx = i
  195. if j > 0:
  196. max_precisions[j - 1] = max_precisions[j]
  197. break
  198. else:
  199. if max_precisions[j] < precision[i]:
  200. max_precisions[j] = precision[i]
  201. one_class_ap = sum(max_precisions) / 11.
  202. mAP += one_class_ap
  203. valid_cnt += 1
  204. elif self.map_type == 'integral':
  205. import math
  206. prev_recall = 0.
  207. for i in range(len(precision)):
  208. recall_gap = math.fabs(recall[i] - prev_recall)
  209. if recall_gap > 1e-6:
  210. one_class_ap += precision[i] * recall_gap
  211. prev_recall = recall[i]
  212. mAP += one_class_ap
  213. valid_cnt += 1
  214. else:
  215. logging.error("Unspported mAP type {}".format(self.map_type))
  216. sys.exit(1)
  217. eval_results.append({
  218. 'class': self.classes[valid_cnt - 1],
  219. 'ap': one_class_ap,
  220. 'precision': precision,
  221. 'recall': recall,
  222. })
  223. self.eval_results = eval_results
  224. self.mAP = mAP / float(valid_cnt) if valid_cnt > 0 else mAP
  225. def get_map(self):
  226. """
  227. Get mAP result
  228. """
  229. if self.mAP is None:
  230. logging.error("mAP is not calculated.")
  231. if self.classwise:
  232. # Compute per-category AP and PR curve
  233. try:
  234. from terminaltables import AsciiTable
  235. except Exception as e:
  236. logging.error(
  237. 'terminaltables not found, plaese install terminaltables. '
  238. 'for example: `pip install terminaltables`.')
  239. raise e
  240. results_per_category = []
  241. for eval_result in self.eval_results:
  242. results_per_category.append(
  243. (str(eval_result['class']),
  244. '{:0.3f}'.format(float(eval_result['ap']))))
  245. _draw_pr_curve(
  246. eval_result['precision'],
  247. eval_result['recall'],
  248. out_dir='voc_pr_curve',
  249. file_name='{}_precision_recall_curve.jpg'.format(
  250. eval_result['class']))
  251. num_columns = min(6, len(results_per_category) * 2)
  252. results_flatten = list(itertools.chain(*results_per_category))
  253. headers = ['category', 'AP'] * (num_columns // 2)
  254. results_2d = itertools.zip_longest(* [
  255. results_flatten[i::num_columns] for i in range(num_columns)
  256. ])
  257. table_data = [headers]
  258. table_data += [result for result in results_2d]
  259. table = AsciiTable(table_data)
  260. logging.info('Per-category of VOC AP: \n{}'.format(table.table))
  261. logging.info(
  262. "per-category PR curve has output to voc_pr_curve folder.")
  263. return self.mAP
  264. def _get_tp_fp_accum(self, score_pos_list):
  265. """
  266. Calculate accumulating true/false positive results from
  267. [score, pos] records
  268. """
  269. sorted_list = sorted(score_pos_list, key=lambda s: s[0], reverse=True)
  270. accum_tp = 0
  271. accum_fp = 0
  272. accum_tp_list = []
  273. accum_fp_list = []
  274. for (score, pos) in sorted_list:
  275. accum_tp += int(pos)
  276. accum_tp_list.append(accum_tp)
  277. accum_fp += 1 - int(pos)
  278. accum_fp_list.append(accum_fp)
  279. return accum_tp_list, accum_fp_list