map_utils.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. # Copyright (c) 2020 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 paddle
  23. from ppdet.modeling.bbox_utils import poly2rbox, rbox2poly_np
  24. from ppdet.utils.logger import setup_logger
  25. logger = setup_logger(__name__)
  26. __all__ = [
  27. 'draw_pr_curve',
  28. 'bbox_area',
  29. 'jaccard_overlap',
  30. 'prune_zero_padding',
  31. 'DetectionMAP',
  32. 'ap_per_class',
  33. 'compute_ap',
  34. ]
  35. def draw_pr_curve(precision,
  36. recall,
  37. iou=0.5,
  38. out_dir='pr_curve',
  39. file_name='precision_recall_curve.jpg'):
  40. if not os.path.exists(out_dir):
  41. os.makedirs(out_dir)
  42. output_path = os.path.join(out_dir, file_name)
  43. try:
  44. import matplotlib.pyplot as plt
  45. except Exception as e:
  46. logger.error('Matplotlib not found, plaese install matplotlib.'
  47. 'for example: `pip install matplotlib`.')
  48. raise e
  49. plt.cla()
  50. plt.figure('P-R Curve')
  51. plt.title('Precision/Recall Curve(IoU={})'.format(iou))
  52. plt.xlabel('Recall')
  53. plt.ylabel('Precision')
  54. plt.grid(True)
  55. plt.plot(recall, precision)
  56. plt.savefig(output_path)
  57. def bbox_area(bbox, is_bbox_normalized):
  58. """
  59. Calculate area of a bounding box
  60. """
  61. norm = 1. - float(is_bbox_normalized)
  62. width = bbox[2] - bbox[0] + norm
  63. height = bbox[3] - bbox[1] + norm
  64. return width * height
  65. def jaccard_overlap(pred, gt, is_bbox_normalized=False):
  66. """
  67. Calculate jaccard overlap ratio between two bounding box
  68. """
  69. if pred[0] >= gt[2] or pred[2] <= gt[0] or \
  70. pred[1] >= gt[3] or pred[3] <= gt[1]:
  71. return 0.
  72. inter_xmin = max(pred[0], gt[0])
  73. inter_ymin = max(pred[1], gt[1])
  74. inter_xmax = min(pred[2], gt[2])
  75. inter_ymax = min(pred[3], gt[3])
  76. inter_size = bbox_area([inter_xmin, inter_ymin, inter_xmax, inter_ymax],
  77. is_bbox_normalized)
  78. pred_size = bbox_area(pred, is_bbox_normalized)
  79. gt_size = bbox_area(gt, is_bbox_normalized)
  80. overlap = float(inter_size) / (pred_size + gt_size - inter_size)
  81. return overlap
  82. def calc_rbox_iou(pred, gt_rbox):
  83. """
  84. calc iou between rotated bbox
  85. """
  86. # calc iou of bounding box for speedup
  87. pred = np.array(pred, np.float32).reshape(-1, 8)
  88. pred = pred.reshape(-1, 2)
  89. gt_poly = rbox2poly_np(np.array(gt_rbox).reshape(-1, 5))[0]
  90. gt_poly = gt_poly.reshape(-1, 2)
  91. pred_rect = [
  92. np.min(pred[:, 0]), np.min(pred[:, 1]), np.max(pred[:, 0]),
  93. np.max(pred[:, 1])
  94. ]
  95. gt_rect = [
  96. np.min(gt_poly[:, 0]), np.min(gt_poly[:, 1]), np.max(gt_poly[:, 0]),
  97. np.max(gt_poly[:, 1])
  98. ]
  99. iou = jaccard_overlap(pred_rect, gt_rect, False)
  100. if iou <= 0:
  101. return iou
  102. # calc rbox iou
  103. pred = pred.reshape(-1, 8)
  104. pred = np.array(pred, np.float32).reshape(-1, 8)
  105. pred_rbox = poly2rbox(pred)
  106. pred_rbox = pred_rbox.reshape(-1, 5)
  107. pred_rbox = pred_rbox.reshape(-1, 5)
  108. try:
  109. from rbox_iou_ops import rbox_iou
  110. except Exception as e:
  111. print("import custom_ops error, try install rbox_iou_ops " \
  112. "following ppdet/ext_op/README.md", e)
  113. sys.stdout.flush()
  114. sys.exit(-1)
  115. gt_rbox = np.array(gt_rbox, np.float32).reshape(-1, 5)
  116. pd_gt_rbox = paddle.to_tensor(gt_rbox, dtype='float32')
  117. pd_pred_rbox = paddle.to_tensor(pred_rbox, dtype='float32')
  118. iou = rbox_iou(pd_gt_rbox, pd_pred_rbox)
  119. iou = iou.numpy()
  120. return iou[0][0]
  121. def prune_zero_padding(gt_box, gt_label, difficult=None):
  122. valid_cnt = 0
  123. for i in range(len(gt_box)):
  124. if gt_box[i, 0] == 0 and gt_box[i, 1] == 0 and \
  125. gt_box[i, 2] == 0 and gt_box[i, 3] == 0:
  126. break
  127. valid_cnt += 1
  128. return (gt_box[:valid_cnt], gt_label[:valid_cnt], difficult[:valid_cnt]
  129. if difficult is not None else None)
  130. class DetectionMAP(object):
  131. """
  132. Calculate detection mean average precision.
  133. Currently support two types: 11point and integral
  134. Args:
  135. class_num (int): The class number.
  136. overlap_thresh (float): The threshold of overlap
  137. ratio between prediction bounding box and
  138. ground truth bounding box for deciding
  139. true/false positive. Default 0.5.
  140. map_type (str): Calculation method of mean average
  141. precision, currently support '11point' and
  142. 'integral'. Default '11point'.
  143. is_bbox_normalized (bool): Whether bounding boxes
  144. is normalized to range[0, 1]. Default False.
  145. evaluate_difficult (bool): Whether to evaluate
  146. difficult bounding boxes. Default False.
  147. catid2name (dict): Mapping between category id and category name.
  148. classwise (bool): Whether per-category AP and draw
  149. P-R Curve or not.
  150. """
  151. def __init__(self,
  152. class_num,
  153. overlap_thresh=0.5,
  154. map_type='11point',
  155. is_bbox_normalized=False,
  156. evaluate_difficult=False,
  157. catid2name=None,
  158. classwise=False):
  159. self.class_num = class_num
  160. self.overlap_thresh = overlap_thresh
  161. assert map_type in ['11point', 'integral'], \
  162. "map_type currently only support '11point' "\
  163. "and 'integral'"
  164. self.map_type = map_type
  165. self.is_bbox_normalized = is_bbox_normalized
  166. self.evaluate_difficult = evaluate_difficult
  167. self.classwise = classwise
  168. self.classes = []
  169. for cname in catid2name.values():
  170. self.classes.append(cname)
  171. self.reset()
  172. def update(self, bbox, score, label, gt_box, gt_label, difficult=None):
  173. """
  174. Update metric statics from given prediction and ground
  175. truth infomations.
  176. """
  177. if difficult is None:
  178. difficult = np.zeros_like(gt_label)
  179. # record class gt count
  180. for gtl, diff in zip(gt_label, difficult):
  181. if self.evaluate_difficult or int(diff) == 0:
  182. self.class_gt_counts[int(np.array(gtl))] += 1
  183. # record class score positive
  184. visited = [False] * len(gt_label)
  185. for b, s, l in zip(bbox, score, label):
  186. pred = b.tolist() if isinstance(b, np.ndarray) else b
  187. max_idx = -1
  188. max_overlap = -1.0
  189. for i, gl in enumerate(gt_label):
  190. if int(gl) == int(l):
  191. if len(gt_box[i]) == 5:
  192. overlap = calc_rbox_iou(pred, gt_box[i])
  193. else:
  194. overlap = jaccard_overlap(pred, gt_box[i],
  195. self.is_bbox_normalized)
  196. if overlap > max_overlap:
  197. max_overlap = overlap
  198. max_idx = i
  199. if max_overlap > self.overlap_thresh:
  200. if self.evaluate_difficult or \
  201. int(np.array(difficult[max_idx])) == 0:
  202. if not visited[max_idx]:
  203. self.class_score_poss[int(l)].append([s, 1.0])
  204. visited[max_idx] = True
  205. else:
  206. self.class_score_poss[int(l)].append([s, 0.0])
  207. else:
  208. self.class_score_poss[int(l)].append([s, 0.0])
  209. def reset(self):
  210. """
  211. Reset metric statics
  212. """
  213. self.class_score_poss = [[] for _ in range(self.class_num)]
  214. self.class_gt_counts = [0] * self.class_num
  215. self.mAP = 0.0
  216. def accumulate(self):
  217. """
  218. Accumulate metric results and calculate mAP
  219. """
  220. mAP = 0.
  221. valid_cnt = 0
  222. eval_results = []
  223. for score_pos, count in zip(self.class_score_poss,
  224. self.class_gt_counts):
  225. if count == 0: continue
  226. if len(score_pos) == 0:
  227. valid_cnt += 1
  228. continue
  229. accum_tp_list, accum_fp_list = \
  230. self._get_tp_fp_accum(score_pos)
  231. precision = []
  232. recall = []
  233. for ac_tp, ac_fp in zip(accum_tp_list, accum_fp_list):
  234. precision.append(float(ac_tp) / (ac_tp + ac_fp))
  235. recall.append(float(ac_tp) / count)
  236. one_class_ap = 0.0
  237. if self.map_type == '11point':
  238. max_precisions = [0.] * 11
  239. start_idx = len(precision) - 1
  240. for j in range(10, -1, -1):
  241. for i in range(start_idx, -1, -1):
  242. if recall[i] < float(j) / 10.:
  243. start_idx = i
  244. if j > 0:
  245. max_precisions[j - 1] = max_precisions[j]
  246. break
  247. else:
  248. if max_precisions[j] < precision[i]:
  249. max_precisions[j] = precision[i]
  250. one_class_ap = sum(max_precisions) / 11.
  251. mAP += one_class_ap
  252. valid_cnt += 1
  253. elif self.map_type == 'integral':
  254. import math
  255. prev_recall = 0.
  256. for i in range(len(precision)):
  257. recall_gap = math.fabs(recall[i] - prev_recall)
  258. if recall_gap > 1e-6:
  259. one_class_ap += precision[i] * recall_gap
  260. prev_recall = recall[i]
  261. mAP += one_class_ap
  262. valid_cnt += 1
  263. else:
  264. logger.error("Unspported mAP type {}".format(self.map_type))
  265. sys.exit(1)
  266. eval_results.append({
  267. 'class': self.classes[valid_cnt - 1],
  268. 'ap': one_class_ap,
  269. 'precision': precision,
  270. 'recall': recall,
  271. })
  272. self.eval_results = eval_results
  273. self.mAP = mAP / float(valid_cnt) if valid_cnt > 0 else mAP
  274. def get_map(self):
  275. """
  276. Get mAP result
  277. """
  278. if self.mAP is None:
  279. logger.error("mAP is not calculated.")
  280. if self.classwise:
  281. # Compute per-category AP and PR curve
  282. try:
  283. from terminaltables import AsciiTable
  284. except Exception as e:
  285. logger.error(
  286. 'terminaltables not found, plaese install terminaltables. '
  287. 'for example: `pip install terminaltables`.')
  288. raise e
  289. results_per_category = []
  290. for eval_result in self.eval_results:
  291. results_per_category.append(
  292. (str(eval_result['class']),
  293. '{:0.3f}'.format(float(eval_result['ap']))))
  294. draw_pr_curve(
  295. eval_result['precision'],
  296. eval_result['recall'],
  297. out_dir='voc_pr_curve',
  298. file_name='{}_precision_recall_curve.jpg'.format(
  299. eval_result['class']))
  300. num_columns = min(6, len(results_per_category) * 2)
  301. results_flatten = list(itertools.chain(*results_per_category))
  302. headers = ['category', 'AP'] * (num_columns // 2)
  303. results_2d = itertools.zip_longest(
  304. *[results_flatten[i::num_columns] for i in range(num_columns)])
  305. table_data = [headers]
  306. table_data += [result for result in results_2d]
  307. table = AsciiTable(table_data)
  308. logger.info('Per-category of VOC AP: \n{}'.format(table.table))
  309. logger.info(
  310. "per-category PR curve has output to voc_pr_curve folder.")
  311. return self.mAP
  312. def _get_tp_fp_accum(self, score_pos_list):
  313. """
  314. Calculate accumulating true/false positive results from
  315. [score, pos] records
  316. """
  317. sorted_list = sorted(score_pos_list, key=lambda s: s[0], reverse=True)
  318. accum_tp = 0
  319. accum_fp = 0
  320. accum_tp_list = []
  321. accum_fp_list = []
  322. for (score, pos) in sorted_list:
  323. accum_tp += int(pos)
  324. accum_tp_list.append(accum_tp)
  325. accum_fp += 1 - int(pos)
  326. accum_fp_list.append(accum_fp)
  327. return accum_tp_list, accum_fp_list
  328. def ap_per_class(tp, conf, pred_cls, target_cls):
  329. """
  330. Computes the average precision, given the recall and precision curves.
  331. Method originally from https://github.com/rafaelpadilla/Object-Detection-Metrics.
  332. Args:
  333. tp (list): True positives.
  334. conf (list): Objectness value from 0-1.
  335. pred_cls (list): Predicted object classes.
  336. target_cls (list): Target object classes.
  337. """
  338. tp, conf, pred_cls, target_cls = np.array(tp), np.array(conf), np.array(
  339. pred_cls), np.array(target_cls)
  340. # Sort by objectness
  341. i = np.argsort(-conf)
  342. tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
  343. # Find unique classes
  344. unique_classes = np.unique(np.concatenate((pred_cls, target_cls), 0))
  345. # Create Precision-Recall curve and compute AP for each class
  346. ap, p, r = [], [], []
  347. for c in unique_classes:
  348. i = pred_cls == c
  349. n_gt = sum(target_cls == c) # Number of ground truth objects
  350. n_p = sum(i) # Number of predicted objects
  351. if (n_p == 0) and (n_gt == 0):
  352. continue
  353. elif (n_p == 0) or (n_gt == 0):
  354. ap.append(0)
  355. r.append(0)
  356. p.append(0)
  357. else:
  358. # Accumulate FPs and TPs
  359. fpc = np.cumsum(1 - tp[i])
  360. tpc = np.cumsum(tp[i])
  361. # Recall
  362. recall_curve = tpc / (n_gt + 1e-16)
  363. r.append(tpc[-1] / (n_gt + 1e-16))
  364. # Precision
  365. precision_curve = tpc / (tpc + fpc)
  366. p.append(tpc[-1] / (tpc[-1] + fpc[-1]))
  367. # AP from recall-precision curve
  368. ap.append(compute_ap(recall_curve, precision_curve))
  369. return np.array(ap), unique_classes.astype('int32'), np.array(r), np.array(
  370. p)
  371. def compute_ap(recall, precision):
  372. """
  373. Computes the average precision, given the recall and precision curves.
  374. Code originally from https://github.com/rbgirshick/py-faster-rcnn.
  375. Args:
  376. recall (list): The recall curve.
  377. precision (list): The precision curve.
  378. Returns:
  379. The average precision as computed in py-faster-rcnn.
  380. """
  381. # correct AP calculation
  382. # first append sentinel values at the end
  383. mrec = np.concatenate(([0.], recall, [1.]))
  384. mpre = np.concatenate(([0.], precision, [0.]))
  385. # compute the precision envelope
  386. for i in range(mpre.size - 1, 0, -1):
  387. mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])
  388. # to calculate area under PR curve, look for points
  389. # where X axis (recall) changes value
  390. i = np.where(mrec[1:] != mrec[:-1])[0]
  391. # and sum (\Delta recall) * prec
  392. ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
  393. return ap