metrics.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. import os
  18. import sys
  19. import json
  20. import paddle
  21. import numpy as np
  22. import typing
  23. from .map_utils import prune_zero_padding, DetectionMAP
  24. from .coco_utils import get_infer_results, cocoapi_eval
  25. from .widerface_utils import face_eval_run
  26. from paddlex.ppdet.data.source.category import get_categories
  27. from paddlex.ppdet.utils.logger import setup_logger
  28. logger = setup_logger(__name__)
  29. __all__ = [
  30. 'Metric', 'COCOMetric', 'VOCMetric', 'WiderFaceMetric',
  31. 'get_infer_results', 'RBoxMetric', 'SNIPERCOCOMetric'
  32. ]
  33. COCO_SIGMAS = np.array([
  34. .26, .25, .25, .35, .35, .79, .79, .72, .72, .62, .62, 1.07, 1.07, .87,
  35. .87, .89, .89
  36. ]) / 10.0
  37. CROWD_SIGMAS = np.array(
  38. [.79, .79, .72, .72, .62, .62, 1.07, 1.07, .87, .87, .89, .89, .79,
  39. .79]) / 10.0
  40. class Metric(paddle.metric.Metric):
  41. def name(self):
  42. return self.__class__.__name__
  43. def reset(self):
  44. pass
  45. def accumulate(self):
  46. pass
  47. # paddle.metric.Metric defined :metch:`update`, :meth:`accumulate`
  48. # :metch:`reset`, in ppdet, we also need following 2 methods:
  49. # abstract method for logging metric results
  50. def log(self):
  51. pass
  52. # abstract method for getting metric results
  53. def get_results(self):
  54. pass
  55. class COCOMetric(Metric):
  56. def __init__(self, anno_file, **kwargs):
  57. assert os.path.isfile(anno_file), \
  58. "anno_file {} not a file".format(anno_file)
  59. self.anno_file = anno_file
  60. self.clsid2catid = kwargs.get('clsid2catid', None)
  61. if self.clsid2catid is None:
  62. self.clsid2catid, _ = get_categories('COCO', anno_file)
  63. self.classwise = kwargs.get('classwise', False)
  64. self.output_eval = kwargs.get('output_eval', None)
  65. # TODO: bias should be unified
  66. self.bias = kwargs.get('bias', 0)
  67. self.save_prediction_only = kwargs.get('save_prediction_only', False)
  68. self.iou_type = kwargs.get('IouType', 'bbox')
  69. self.reset()
  70. def reset(self):
  71. # only bbox and mask evaluation support currently
  72. self.results = {'bbox': [], 'mask': [], 'segm': [], 'keypoint': []}
  73. self.eval_results = {}
  74. def update(self, inputs, outputs):
  75. outs = {}
  76. # outputs Tensor -> numpy.ndarray
  77. for k, v in outputs.items():
  78. outs[k] = v.numpy() if isinstance(v, paddle.Tensor) else v
  79. # multi-scale inputs: all inputs have same im_id
  80. if isinstance(inputs, typing.Sequence):
  81. im_id = inputs[0]['im_id']
  82. else:
  83. im_id = inputs['im_id']
  84. outs['im_id'] = im_id.numpy() if isinstance(im_id,
  85. paddle.Tensor) else im_id
  86. infer_results = get_infer_results(
  87. outs, self.clsid2catid, bias=self.bias)
  88. self.results['bbox'] += infer_results[
  89. 'bbox'] if 'bbox' in infer_results else []
  90. self.results['mask'] += infer_results[
  91. 'mask'] if 'mask' in infer_results else []
  92. self.results['segm'] += infer_results[
  93. 'segm'] if 'segm' in infer_results else []
  94. self.results['keypoint'] += infer_results[
  95. 'keypoint'] if 'keypoint' in infer_results else []
  96. def accumulate(self):
  97. if len(self.results['bbox']) > 0:
  98. output = "bbox.json"
  99. if self.output_eval:
  100. output = os.path.join(self.output_eval, output)
  101. with open(output, 'w') as f:
  102. json.dump(self.results['bbox'], f)
  103. logger.info('The bbox result is saved to bbox.json.')
  104. if self.save_prediction_only:
  105. logger.info('The bbox result is saved to {} and do not '
  106. 'evaluate the mAP.'.format(output))
  107. else:
  108. bbox_stats = cocoapi_eval(
  109. output,
  110. 'bbox',
  111. anno_file=self.anno_file,
  112. classwise=self.classwise)
  113. self.eval_results['bbox'] = bbox_stats
  114. sys.stdout.flush()
  115. if len(self.results['mask']) > 0:
  116. output = "mask.json"
  117. if self.output_eval:
  118. output = os.path.join(self.output_eval, output)
  119. with open(output, 'w') as f:
  120. json.dump(self.results['mask'], f)
  121. logger.info('The mask result is saved to mask.json.')
  122. if self.save_prediction_only:
  123. logger.info('The mask result is saved to {} and do not '
  124. 'evaluate the mAP.'.format(output))
  125. else:
  126. seg_stats = cocoapi_eval(
  127. output,
  128. 'segm',
  129. anno_file=self.anno_file,
  130. classwise=self.classwise)
  131. self.eval_results['mask'] = seg_stats
  132. sys.stdout.flush()
  133. if len(self.results['segm']) > 0:
  134. output = "segm.json"
  135. if self.output_eval:
  136. output = os.path.join(self.output_eval, output)
  137. with open(output, 'w') as f:
  138. json.dump(self.results['segm'], f)
  139. logger.info('The segm result is saved to segm.json.')
  140. if self.save_prediction_only:
  141. logger.info('The segm result is saved to {} and do not '
  142. 'evaluate the mAP.'.format(output))
  143. else:
  144. seg_stats = cocoapi_eval(
  145. output,
  146. 'segm',
  147. anno_file=self.anno_file,
  148. classwise=self.classwise)
  149. self.eval_results['mask'] = seg_stats
  150. sys.stdout.flush()
  151. if len(self.results['keypoint']) > 0:
  152. output = "keypoint.json"
  153. if self.output_eval:
  154. output = os.path.join(self.output_eval, output)
  155. with open(output, 'w') as f:
  156. json.dump(self.results['keypoint'], f)
  157. logger.info('The keypoint result is saved to keypoint.json.')
  158. if self.save_prediction_only:
  159. logger.info('The keypoint result is saved to {} and do not '
  160. 'evaluate the mAP.'.format(output))
  161. else:
  162. style = 'keypoints'
  163. use_area = True
  164. sigmas = COCO_SIGMAS
  165. if self.iou_type == 'keypoints_crowd':
  166. style = 'keypoints_crowd'
  167. use_area = False
  168. sigmas = CROWD_SIGMAS
  169. keypoint_stats = cocoapi_eval(
  170. output,
  171. style,
  172. anno_file=self.anno_file,
  173. classwise=self.classwise,
  174. sigmas=sigmas,
  175. use_area=use_area)
  176. self.eval_results['keypoint'] = keypoint_stats
  177. sys.stdout.flush()
  178. def log(self):
  179. pass
  180. def get_results(self):
  181. return self.eval_results
  182. class VOCMetric(Metric):
  183. def __init__(self,
  184. label_list,
  185. class_num=20,
  186. overlap_thresh=0.5,
  187. map_type='11point',
  188. is_bbox_normalized=False,
  189. evaluate_difficult=False,
  190. classwise=False):
  191. assert os.path.isfile(label_list), \
  192. "label_list {} not a file".format(label_list)
  193. self.clsid2catid, self.catid2name = get_categories('VOC', label_list)
  194. self.overlap_thresh = overlap_thresh
  195. self.map_type = map_type
  196. self.evaluate_difficult = evaluate_difficult
  197. self.detection_map = DetectionMAP(
  198. class_num=class_num,
  199. overlap_thresh=overlap_thresh,
  200. map_type=map_type,
  201. is_bbox_normalized=is_bbox_normalized,
  202. evaluate_difficult=evaluate_difficult,
  203. catid2name=self.catid2name,
  204. classwise=classwise)
  205. self.reset()
  206. def reset(self):
  207. self.detection_map.reset()
  208. def update(self, inputs, outputs):
  209. bbox_np = outputs['bbox'].numpy()
  210. bboxes = bbox_np[:, 2:]
  211. scores = bbox_np[:, 1]
  212. labels = bbox_np[:, 0]
  213. bbox_lengths = outputs['bbox_num'].numpy()
  214. if bboxes.shape == (1, 1) or bboxes is None:
  215. return
  216. gt_boxes = inputs['gt_bbox']
  217. gt_labels = inputs['gt_class']
  218. difficults = inputs['difficult'] if not self.evaluate_difficult \
  219. else None
  220. scale_factor = inputs['scale_factor'].numpy(
  221. ) if 'scale_factor' in inputs else np.ones(
  222. (gt_boxes.shape[0], 2)).astype('float32')
  223. bbox_idx = 0
  224. for i in range(len(gt_boxes)):
  225. gt_box = gt_boxes[i].numpy()
  226. h, w = scale_factor[i]
  227. gt_box = gt_box / np.array([w, h, w, h])
  228. gt_label = gt_labels[i].numpy()
  229. difficult = None if difficults is None \
  230. else difficults[i].numpy()
  231. bbox_num = bbox_lengths[i]
  232. bbox = bboxes[bbox_idx:bbox_idx + bbox_num]
  233. score = scores[bbox_idx:bbox_idx + bbox_num]
  234. label = labels[bbox_idx:bbox_idx + bbox_num]
  235. gt_box, gt_label, difficult = prune_zero_padding(gt_box, gt_label,
  236. difficult)
  237. self.detection_map.update(bbox, score, label, gt_box, gt_label,
  238. difficult)
  239. bbox_idx += bbox_num
  240. def accumulate(self):
  241. logger.info("Accumulating evaluatation results...")
  242. self.detection_map.accumulate()
  243. def log(self):
  244. map_stat = 100. * self.detection_map.get_map()
  245. logger.info("mAP({:.2f}, {}) = {:.2f}%".format(
  246. self.overlap_thresh, self.map_type, map_stat))
  247. def get_results(self):
  248. return {'bbox': [self.detection_map.get_map()]}
  249. class WiderFaceMetric(Metric):
  250. def __init__(self, image_dir, anno_file, multi_scale=True):
  251. self.image_dir = image_dir
  252. self.anno_file = anno_file
  253. self.multi_scale = multi_scale
  254. self.clsid2catid, self.catid2name = get_categories('widerface')
  255. def update(self, model):
  256. face_eval_run(
  257. model,
  258. self.image_dir,
  259. self.anno_file,
  260. pred_dir='output/pred',
  261. eval_mode='widerface',
  262. multi_scale=self.multi_scale)
  263. class RBoxMetric(Metric):
  264. def __init__(self, anno_file, **kwargs):
  265. assert os.path.isfile(anno_file), \
  266. "anno_file {} not a file".format(anno_file)
  267. assert os.path.exists(anno_file), "anno_file {} not exists".format(
  268. anno_file)
  269. self.anno_file = anno_file
  270. self.gt_anno = json.load(open(self.anno_file))
  271. cats = self.gt_anno['categories']
  272. self.clsid2catid = {i: cat['id'] for i, cat in enumerate(cats)}
  273. self.catid2clsid = {cat['id']: i for i, cat in enumerate(cats)}
  274. self.catid2name = {cat['id']: cat['name'] for cat in cats}
  275. self.classwise = kwargs.get('classwise', False)
  276. self.output_eval = kwargs.get('output_eval', None)
  277. # TODO: bias should be unified
  278. self.bias = kwargs.get('bias', 0)
  279. self.save_prediction_only = kwargs.get('save_prediction_only', False)
  280. self.iou_type = kwargs.get('IouType', 'bbox')
  281. self.overlap_thresh = kwargs.get('overlap_thresh', 0.5)
  282. self.map_type = kwargs.get('map_type', '11point')
  283. self.evaluate_difficult = kwargs.get('evaluate_difficult', False)
  284. class_num = len(self.catid2name)
  285. self.detection_map = DetectionMAP(
  286. class_num=class_num,
  287. overlap_thresh=self.overlap_thresh,
  288. map_type=self.map_type,
  289. is_bbox_normalized=False,
  290. evaluate_difficult=self.evaluate_difficult,
  291. catid2name=self.catid2name,
  292. classwise=self.classwise)
  293. self.reset()
  294. def reset(self):
  295. self.result_bbox = []
  296. self.detection_map.reset()
  297. def update(self, inputs, outputs):
  298. outs = {}
  299. # outputs Tensor -> numpy.ndarray
  300. for k, v in outputs.items():
  301. outs[k] = v.numpy() if isinstance(v, paddle.Tensor) else v
  302. im_id = inputs['im_id']
  303. outs['im_id'] = im_id.numpy() if isinstance(im_id,
  304. paddle.Tensor) else im_id
  305. infer_results = get_infer_results(
  306. outs, self.clsid2catid, bias=self.bias)
  307. self.result_bbox += infer_results[
  308. 'bbox'] if 'bbox' in infer_results else []
  309. bbox = [b['bbox'] for b in self.result_bbox]
  310. score = [b['score'] for b in self.result_bbox]
  311. label = [b['category_id'] for b in self.result_bbox]
  312. label = [self.catid2clsid[e] for e in label]
  313. gt_box = [
  314. e['bbox'] for e in self.gt_anno['annotations']
  315. if e['image_id'] == outs['im_id']
  316. ]
  317. gt_label = [
  318. e['category_id'] for e in self.gt_anno['annotations']
  319. if e['image_id'] == outs['im_id']
  320. ]
  321. gt_label = [self.catid2clsid[e] for e in gt_label]
  322. self.detection_map.update(bbox, score, label, gt_box, gt_label)
  323. def accumulate(self):
  324. if len(self.result_bbox) > 0:
  325. output = "bbox.json"
  326. if self.output_eval:
  327. output = os.path.join(self.output_eval, output)
  328. with open(output, 'w') as f:
  329. json.dump(self.result_bbox, f)
  330. logger.info('The bbox result is saved to bbox.json.')
  331. if self.save_prediction_only:
  332. logger.info('The bbox result is saved to {} and do not '
  333. 'evaluate the mAP.'.format(output))
  334. else:
  335. logger.info("Accumulating evaluatation results...")
  336. self.detection_map.accumulate()
  337. def log(self):
  338. map_stat = 100. * self.detection_map.get_map()
  339. logger.info("mAP({:.2f}, {}) = {:.2f}%".format(
  340. self.overlap_thresh, self.map_type, map_stat))
  341. def get_results(self):
  342. return {'bbox': [self.detection_map.get_map()]}
  343. class SNIPERCOCOMetric(COCOMetric):
  344. def __init__(self, anno_file, **kwargs):
  345. super(SNIPERCOCOMetric, self).__init__(anno_file, **kwargs)
  346. self.dataset = kwargs["dataset"]
  347. self.chip_results = []
  348. def reset(self):
  349. # only bbox and mask evaluation support currently
  350. self.results = {'bbox': [], 'mask': [], 'segm': [], 'keypoint': []}
  351. self.eval_results = {}
  352. self.chip_results = []
  353. def update(self, inputs, outputs):
  354. outs = {}
  355. # outputs Tensor -> numpy.ndarray
  356. for k, v in outputs.items():
  357. outs[k] = v.numpy() if isinstance(v, paddle.Tensor) else v
  358. im_id = inputs['im_id']
  359. outs['im_id'] = im_id.numpy() if isinstance(im_id,
  360. paddle.Tensor) else im_id
  361. self.chip_results.append(outs)
  362. def accumulate(self):
  363. results = self.dataset.anno_cropper.aggregate_chips_detections(
  364. self.chip_results)
  365. for outs in results:
  366. infer_results = get_infer_results(
  367. outs, self.clsid2catid, bias=self.bias)
  368. self.results['bbox'] += infer_results[
  369. 'bbox'] if 'bbox' in infer_results else []
  370. super(SNIPERCOCOMetric, self).accumulate()