You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

voc.py 4.3 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. from collections import OrderedDict
  3. from mmcv.utils import print_log
  4. from mmdet.core import eval_map, eval_recalls
  5. from .builder import DATASETS
  6. from .xml_style import XMLDataset
  7. @DATASETS.register_module()
  8. class VOCDataset(XMLDataset):
  9. CLASSES = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car',
  10. 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse',
  11. 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train',
  12. 'tvmonitor')
  13. def __init__(self, **kwargs):
  14. super(VOCDataset, self).__init__(**kwargs)
  15. if 'VOC2007' in self.img_prefix:
  16. self.year = 2007
  17. elif 'VOC2012' in self.img_prefix:
  18. self.year = 2012
  19. else:
  20. raise ValueError('Cannot infer dataset year from img_prefix')
  21. def evaluate(self,
  22. results,
  23. metric='mAP',
  24. logger=None,
  25. proposal_nums=(100, 300, 1000),
  26. iou_thr=0.5,
  27. scale_ranges=None):
  28. """Evaluate in VOC protocol.
  29. Args:
  30. results (list[list | tuple]): Testing results of the dataset.
  31. metric (str | list[str]): Metrics to be evaluated. Options are
  32. 'mAP', 'recall'.
  33. logger (logging.Logger | str, optional): Logger used for printing
  34. related information during evaluation. Default: None.
  35. proposal_nums (Sequence[int]): Proposal number used for evaluating
  36. recalls, such as recall@100, recall@1000.
  37. Default: (100, 300, 1000).
  38. iou_thr (float | list[float]): IoU threshold. Default: 0.5.
  39. scale_ranges (list[tuple], optional): Scale ranges for evaluating
  40. mAP. If not specified, all bounding boxes would be included in
  41. evaluation. Default: None.
  42. Returns:
  43. dict[str, float]: AP/recall metrics.
  44. """
  45. if not isinstance(metric, str):
  46. assert len(metric) == 1
  47. metric = metric[0]
  48. allowed_metrics = ['mAP', 'recall']
  49. if metric not in allowed_metrics:
  50. raise KeyError(f'metric {metric} is not supported')
  51. annotations = [self.get_ann_info(i) for i in range(len(self))]
  52. eval_results = OrderedDict()
  53. iou_thrs = [iou_thr] if isinstance(iou_thr, float) else iou_thr
  54. if metric == 'mAP':
  55. assert isinstance(iou_thrs, list)
  56. if self.year == 2007:
  57. ds_name = 'voc07'
  58. else:
  59. ds_name = self.CLASSES
  60. mean_aps = []
  61. for iou_thr in iou_thrs:
  62. print_log(f'\n{"-" * 15}iou_thr: {iou_thr}{"-" * 15}')
  63. # Follow the official implementation,
  64. # http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCdevkit_18-May-2011.tar
  65. # we should use the legacy coordinate system in mmdet 1.x,
  66. # which means w, h should be computed as 'x2 - x1 + 1` and
  67. # `y2 - y1 + 1`
  68. mean_ap, _ = eval_map(
  69. results,
  70. annotations,
  71. scale_ranges=None,
  72. iou_thr=iou_thr,
  73. dataset=ds_name,
  74. logger=logger,
  75. use_legacy_coordinate=True)
  76. mean_aps.append(mean_ap)
  77. eval_results[f'AP{int(iou_thr * 100):02d}'] = round(mean_ap, 3)
  78. eval_results['mAP'] = sum(mean_aps) / len(mean_aps)
  79. elif metric == 'recall':
  80. gt_bboxes = [ann['bboxes'] for ann in annotations]
  81. recalls = eval_recalls(
  82. gt_bboxes,
  83. results,
  84. proposal_nums,
  85. iou_thrs,
  86. logger=logger,
  87. use_legacy_coordinate=True)
  88. for i, num in enumerate(proposal_nums):
  89. for j, iou_thr in enumerate(iou_thrs):
  90. eval_results[f'recall@{num}@{iou_thr}'] = recalls[i, j]
  91. if recalls.shape[1] > 1:
  92. ar = recalls.mean(axis=1)
  93. for i, num in enumerate(proposal_nums):
  94. eval_results[f'AR@{num}'] = ar[i]
  95. return eval_results

No Description

Contributors (2)