-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathd2_evaluator.py
181 lines (155 loc) · 7.35 KB
/
d2_evaluator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# ------------------------------------------------------------------------------
# Copyright (c) Facebook, Inc. and its affiliates.
# To view a copy of this license, visit
# https://github.com/facebookresearch/detectron2/blob/main/LICENSE
# ------------------------------------------------------------------------------
#
# ------------------------------------------------------------------------------
# Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# This work is made available under the Nvidia Source Code License.
# To view a copy of this license, visit
# https://github.com/NVlabs/ODISE/blob/main/LICENSE
#
# Written by Jiarui Xu
# ------------------------------------------------------------------------------
import itertools
import json
import os
from collections import OrderedDict
from detectron2.evaluation import COCOEvaluator as _COCOEvaluator
from detectron2.evaluation import COCOPanopticEvaluator as _COCOPanopticEvaluator
from detectron2.evaluation import SemSegEvaluator as _SemSegEvaluator
from detectron2.evaluation.coco_evaluation import _evaluate_predictions_on_coco
from detectron2.utils.file_io import PathManager
from tabulate import tabulate
class COCOEvaluator(_COCOEvaluator):
def __init__(self, *, dataset_name, **kwargs):
super().__init__(dataset_name=dataset_name, **kwargs)
self.dataset_name = dataset_name
def evaluate(self, img_ids=None):
"""
Args:
img_ids: a list of image IDs to evaluate on. Default to None for the whole dataset
"""
results = super().evaluate(img_ids)
prefix_results = OrderedDict()
for k, v in results.items():
prefix_results[f"{self.dataset_name}/{k}"] = v
return prefix_results
class COCOPanopticEvaluator(_COCOPanopticEvaluator):
def __init__(self, *, dataset_name, **kwargs):
super().__init__(dataset_name=dataset_name, **kwargs)
self.dataset_name = dataset_name
def evaluate(self):
results = super().evaluate()
if results is None:
return
prefix_results = OrderedDict()
for k, v in results.items():
prefix_results[f"{self.dataset_name}/{k}"] = v
return prefix_results
class SemSegEvaluator(_SemSegEvaluator):
def __init__(self, *, dataset_name, prefix="", **kwargs):
super().__init__(dataset_name=dataset_name, **kwargs)
self.dataset_name = dataset_name
if len(prefix) and not prefix.endswith("_"):
prefix += "_"
self.prefix = prefix
def evaluate(self):
results = super().evaluate()
if results is None:
return
results_per_category = []
for name in self._class_names:
# area range index 0: all area ranges
# max dets index -1: typically 100 per image
results_per_category.append((str(name), float(results["sem_seg"][f"IoU-{name}"])))
# tabulate it
N_COLS = min(6, len(results_per_category) * 2)
results_flatten = list(itertools.chain(*results_per_category))
results_2d = itertools.zip_longest(*[results_flatten[i::N_COLS] for i in range(N_COLS)])
table = tabulate(
results_2d,
tablefmt="pipe",
floatfmt=".3f",
headers=["category", "IoU"] * (N_COLS // 2),
numalign="left",
)
self._logger.info("Per-category IoU: \n" + table)
prefix_results = OrderedDict()
for k, v in results.items():
prefix_results[f"{self.dataset_name}/{self.prefix}{k}"] = v
return prefix_results
# Copied from https://github.com/facebookresearch/Mask2Former/blob/main/mask2former/evaluation/instance_evaluation.py # noqa
# modified from COCOEvaluator for instance segmentation
class InstanceSegEvaluator(COCOEvaluator):
"""
Evaluate AR for object proposals, AP for instance detection/segmentation, AP
for keypoint detection outputs using COCO's metrics.
See http://cocodataset.org/#detection-eval and
http://cocodataset.org/#keypoints-eval to understand its metrics.
The metrics range from 0 to 100 (instead of 0 to 1), where a -1 or NaN means
the metric cannot be computed (e.g. due to no predictions made).
In addition to COCO, this evaluator is able to support any bounding box detection,
instance segmentation, or keypoint detection dataset.
"""
def _eval_predictions(self, predictions, img_ids=None):
"""
Evaluate predictions. Fill self._results with the metrics of the tasks.
"""
self._logger.info("Preparing results for COCO format ...")
coco_results = list(itertools.chain(*[x["instances"] for x in predictions]))
tasks = self._tasks or self._tasks_from_predictions(coco_results)
# unmap the category ids for COCO
if hasattr(self._metadata, "thing_dataset_id_to_contiguous_id"):
dataset_id_to_contiguous_id = self._metadata.thing_dataset_id_to_contiguous_id
# all_contiguous_ids = list(dataset_id_to_contiguous_id.values())
# num_classes = len(all_contiguous_ids)
# assert min(all_contiguous_ids) == 0 and max(all_contiguous_ids) == num_classes - 1
reverse_id_mapping = {v: k for k, v in dataset_id_to_contiguous_id.items()}
for result in coco_results:
category_id = result["category_id"]
# assert category_id < num_classes, (
# f"A prediction has class={category_id}, "
# f"but the dataset only has {num_classes} classes and "
# f"predicted class id should be in [0, {num_classes - 1}]."
# )
assert category_id in reverse_id_mapping, (
f"A prediction has class={category_id}, "
f"but the dataset only has class ids in {dataset_id_to_contiguous_id}."
)
result["category_id"] = reverse_id_mapping[category_id]
if self._output_dir:
file_path = os.path.join(self._output_dir, "coco_instances_results.json")
self._logger.info("Saving results to {}".format(file_path))
with PathManager.open(file_path, "w") as f:
f.write(json.dumps(coco_results))
f.flush()
if not self._do_evaluation:
self._logger.info("Annotations are not available for evaluation.")
return
self._logger.info(
"Evaluating predictions with {} COCO API...".format(
"unofficial" if self._use_fast_impl else "official"
)
)
for task in sorted(tasks):
assert task in {"bbox", "segm", "keypoints"}, f"Got unknown task: {task}!"
coco_eval = (
_evaluate_predictions_on_coco(
self._coco_api,
coco_results,
task,
kpt_oks_sigmas=self._kpt_oks_sigmas,
use_fast_impl=self._use_fast_impl,
img_ids=img_ids,
max_dets_per_image=self._max_dets_per_image,
)
if len(coco_results) > 0
else None # cocoapi does not handle empty results very well
)
res = self._derive_coco_results(
coco_eval, task, class_names=self._metadata.get("thing_classes")
)
self._results[task] = res