|
| 1 | +import copy |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from chainer import reporter |
| 5 | +import chainer.training.extensions |
| 6 | + |
| 7 | +from chainercv.evaluations import eval_instance_segmentation_coco |
| 8 | +from chainercv.utils import apply_to_iterator |
| 9 | + |
| 10 | +try: |
| 11 | + import pycocotools.coco # NOQA |
| 12 | + _available = True |
| 13 | +except ImportError: |
| 14 | + _available = False |
| 15 | + |
| 16 | + |
| 17 | +class InstanceSegmentationCOCOEvaluator(chainer.training.extensions.Evaluator): |
| 18 | + |
| 19 | + """An extension that evaluates a instance segmentation model by MS COCO metric. |
| 20 | +
|
| 21 | + This extension iterates over an iterator and evaluates the prediction |
| 22 | + results. |
| 23 | + The results consist of average precisions (APs) and average |
| 24 | + recalls (ARs) as well as the mean of each (mean average precision and mean |
| 25 | + average recall). |
| 26 | + This extension reports the following values with keys. |
| 27 | + Please note that if |
| 28 | + :obj:`label_names` is not specified, only the mAPs and mARs are reported. |
| 29 | +
|
| 30 | + The underlying dataset of the iterator is assumed to return |
| 31 | + :obj:`img, mask, label` or :obj:`img, mask, label, area, crowded`. |
| 32 | +
|
| 33 | + .. csv-table:: |
| 34 | + :header: key, description |
| 35 | +
|
| 36 | + ap/iou=0.50:0.95/area=all/max_dets=100/<label_names[l]>, \ |
| 37 | + [#coco_ins_ext_1]_ |
| 38 | + ap/iou=0.50/area=all/max_dets=100/<label_names[l]>, \ |
| 39 | + [#coco_ins_ext_1]_ |
| 40 | + ap/iou=0.75/area=all/max_dets=100/<label_names[l]>, \ |
| 41 | + [#coco_ins_ext_1]_ |
| 42 | + ap/iou=0.50:0.95/area=small/max_dets=100/<label_names[l]>, \ |
| 43 | + [#coco_ins_ext_1]_ [#coco_ins_ext_5]_ |
| 44 | + ap/iou=0.50:0.95/area=medium/max_dets=100/<label_names[l]>, \ |
| 45 | + [#coco_ins_ext_1]_ [#coco_ins_ext_5]_ |
| 46 | + ap/iou=0.50:0.95/area=large/max_dets=100/<label_names[l]>, \ |
| 47 | + [#coco_ins_ext_1]_ [#coco_ins_ext_5]_ |
| 48 | + ar/iou=0.50:0.95/area=all/max_dets=1/<label_names[l]>, \ |
| 49 | + [#coco_ins_ext_2]_ |
| 50 | + ar/iou=0.50/area=all/max_dets=10/<label_names[l]>, \ |
| 51 | + [#coco_ins_ext_2]_ |
| 52 | + ar/iou=0.75/area=all/max_dets=100/<label_names[l]>, \ |
| 53 | + [#coco_ins_ext_2]_ |
| 54 | + ar/iou=0.50:0.95/area=small/max_dets=100/<label_names[l]>, \ |
| 55 | + [#coco_ins_ext_2]_ [#coco_ins_ext_5]_ |
| 56 | + ar/iou=0.50:0.95/area=medium/max_dets=100/<label_names[l]>, \ |
| 57 | + [#coco_ins_ext_2]_ [#coco_ins_ext_5]_ |
| 58 | + ar/iou=0.50:0.95/area=large/max_dets=100/<label_names[l]>, \ |
| 59 | + [#coco_ins_ext_2]_ [#coco_ins_ext_5]_ |
| 60 | + map/iou=0.50:0.95/area=all/max_dets=100, \ |
| 61 | + [#coco_ins_ext_3]_ |
| 62 | + map/iou=0.50/area=all/max_dets=100, \ |
| 63 | + [#coco_ins_ext_3]_ |
| 64 | + map/iou=0.75/area=all/max_dets=100, \ |
| 65 | + [#coco_ins_ext_3]_ |
| 66 | + map/iou=0.50:0.95/area=small/max_dets=100, \ |
| 67 | + [#coco_ins_ext_3]_ [#coco_ins_ext_5]_ |
| 68 | + map/iou=0.50:0.95/area=medium/max_dets=100, \ |
| 69 | + [#coco_ins_ext_3]_ [#coco_ins_ext_5]_ |
| 70 | + map/iou=0.50:0.95/area=large/max_dets=100, \ |
| 71 | + [#coco_ins_ext_3]_ [#coco_ins_ext_5]_ |
| 72 | + ar/iou=0.50:0.95/area=all/max_dets=1, \ |
| 73 | + [#coco_ins_ext_4]_ |
| 74 | + ar/iou=0.50/area=all/max_dets=10, \ |
| 75 | + [#coco_ins_ext_4]_ |
| 76 | + ar/iou=0.75/area=all/max_dets=100, \ |
| 77 | + [#coco_ins_ext_4]_ |
| 78 | + ar/iou=0.50:0.95/area=small/max_dets=100, \ |
| 79 | + [#coco_ins_ext_4]_ [#coco_ins_ext_5]_ |
| 80 | + ar/iou=0.50:0.95/area=medium/max_dets=100, \ |
| 81 | + [#coco_ins_ext_4]_ [#coco_ins_ext_5]_ |
| 82 | + ar/iou=0.50:0.95/area=large/max_dets=100, \ |
| 83 | + [#coco_ins_ext_4]_ [#coco_ins_ext_5]_ |
| 84 | +
|
| 85 | + .. [#coco_ins_ext_1] Average precision for class \ |
| 86 | + :obj:`label_names[l]`, where :math:`l` is the index of the class. \ |
| 87 | + If class :math:`l` does not exist in either :obj:`pred_labels` or \ |
| 88 | + :obj:`gt_labels`, the corresponding value is set to :obj:`numpy.nan`. |
| 89 | + .. [#coco_ins_ext_2] Average recall for class \ |
| 90 | + :obj:`label_names[l]`, where :math:`l` is the index of the class. \ |
| 91 | + If class :math:`l` does not exist in either :obj:`pred_labels` or \ |
| 92 | + :obj:`gt_labels`, the corresponding value is set to :obj:`numpy.nan`. |
| 93 | + .. [#coco_ins_ext_3] The average of average precisions over classes. |
| 94 | + .. [#coco_ins_ext_4] The average of average recalls over classes. |
| 95 | + .. [#coco_ins_ext_5] Skip if :obj:`gt_areas` is :obj:`None`. |
| 96 | +
|
| 97 | + Args: |
| 98 | + iterator (chainer.Iterator): An iterator. Each sample should be |
| 99 | + following tuple :obj:`img, mask, label, area, crowded`. |
| 100 | + target (chainer.Link): A detection link. This link must have |
| 101 | + :meth:`predict` method that takes a list of images and returns |
| 102 | + :obj:`masks`, :obj:`labels` and :obj:`scores`. |
| 103 | + label_names (iterable of strings): An iterable of names of classes. |
| 104 | + If this value is specified, average precision and average |
| 105 | + recalls for each class are reported. |
| 106 | +
|
| 107 | + """ |
| 108 | + |
| 109 | + trigger = 1, 'epoch' |
| 110 | + default_name = 'validation' |
| 111 | + priority = chainer.training.PRIORITY_WRITER |
| 112 | + |
| 113 | + def __init__( |
| 114 | + self, iterator, target, |
| 115 | + label_names=None): |
| 116 | + if not _available: |
| 117 | + raise ValueError( |
| 118 | + 'Please install pycocotools \n' |
| 119 | + 'pip install -e \'git+https://github.com/pdollar/coco.git' |
| 120 | + '#egg=pycocotools&subdirectory=PythonAPI\'') |
| 121 | + super(InstanceSegmentationCOCOEvaluator, self).__init__( |
| 122 | + iterator, target) |
| 123 | + self.label_names = label_names |
| 124 | + |
| 125 | + def evaluate(self): |
| 126 | + iterator = self._iterators['main'] |
| 127 | + target = self._targets['main'] |
| 128 | + |
| 129 | + if hasattr(iterator, 'reset'): |
| 130 | + iterator.reset() |
| 131 | + it = iterator |
| 132 | + else: |
| 133 | + it = copy.copy(iterator) |
| 134 | + |
| 135 | + in_values, out_values, rest_values = apply_to_iterator( |
| 136 | + target.predict, it) |
| 137 | + # delete unused iterators explicitly |
| 138 | + del in_values |
| 139 | + |
| 140 | + pred_masks, pred_labels, pred_scores = out_values |
| 141 | + |
| 142 | + if len(rest_values) == 2: |
| 143 | + gt_masks, gt_labels = rest_values |
| 144 | + gt_areas = None |
| 145 | + gt_crowdeds = None |
| 146 | + elif len(rest_values) == 4: |
| 147 | + gt_masks, gt_labels, gt_areas, gt_crowdeds =\ |
| 148 | + rest_values |
| 149 | + else: |
| 150 | + raise ValueError('the dataset should return ' |
| 151 | + 'sets of (img, mask, label) or sets of ' |
| 152 | + '(img, mask, label, area, crowded).') |
| 153 | + |
| 154 | + result = eval_instance_segmentation_coco( |
| 155 | + pred_masks, pred_labels, pred_scores, |
| 156 | + gt_masks, gt_labels, gt_areas, gt_crowdeds) |
| 157 | + |
| 158 | + report = {} |
| 159 | + for key in result.keys(): |
| 160 | + if key.startswith('map') or key.startswith('mar'): |
| 161 | + report[key] = result[key] |
| 162 | + |
| 163 | + if self.label_names is not None: |
| 164 | + for key in result.keys(): |
| 165 | + if key.startswith('ap') or key.startswith('ar'): |
| 166 | + for l, label_name in enumerate(self.label_names): |
| 167 | + report_key = '{}/{:s}'.format(key, label_name) |
| 168 | + try: |
| 169 | + report[report_key] = result[key][l] |
| 170 | + except IndexError: |
| 171 | + report[report_key] = np.nan |
| 172 | + |
| 173 | + observation = {} |
| 174 | + with reporter.report_scope(observation): |
| 175 | + reporter.report(report, target) |
| 176 | + return observation |
0 commit comments