-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbox_mean_ap_ar.py
343 lines (309 loc) · 13.2 KB
/
box_mean_ap_ar.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import warnings
from typing import Dict, List
import pandas as pd
import torch
from prettytable import PrettyTable
from torch import Tensor
from torchmetrics.detection import MeanAveragePrecision
from stdeval.metrics import BaseMetric, time_cost_deco
class BoxAveragePrecision(MeanAveragePrecision, BaseMetric):
def __init__(self,
box_format='xyxy',
iou_type='bbox',
extended_summary: bool = False,
classwise: Dict[int, str] = {},
print_table=True,
**kwargs):
"""Compute the Mean-Average-Precision (mAP) and Mean-Average-Recall (mAR) \
for object detection predictions(COCO).
For ease to use, we encapsulate the MeanAveragePrecision of torchmetrics,
and add some features to make it more user-friendly.
1.We've added metrics and displays for each category.
2.We've added a display of the all metric to ASCII table and DataFrame.
theoretically supports all methods in torchmetrics.detection.mean_ap.MeanAveragePrecision.
For more information, please refer to the official documentation:
https://lightning.ai/docs/torchmetrics/stable/detection/mean_average_precision.html#torchmetrics.detection.mean_ap.MeanAveragePrecision
Usage:
# For box:
preds = [
dict(
boxes=tensor([[258.0, 41.0, 606.0, 285.0],
[158.0, 41.0, 462.0, 285.0]]),
scores=tensor([0.536, 0.71]),
labels=tensor([1, 2]),
),
dict(
boxes=tensor([[254.0, 413.0, 656.0, 245.0]]),
scores=tensor([0.526]),
labels=tensor([1]),
)
]
target = [
dict(
boxes=tensor([[214.0, 41.0, 562.0, 285.0],
[158.0, 41.0, 462.0, 285.0]]),
labels=tensor([1,2]),
),
dict(
boxes=tensor([[258.0, 41.0, 606.0, 285.0]]),
labels=tensor([1]),
)
]
classwise = {0:'person', 1:'car', 2:'tea', 3:'cycle'} # lbl id 2 name
metric = BoxAveragePrecision(iou_type="bbox", class_metrics=True, classwise=classwise)
metric.update(target, preds)
metric.get()
metric.table
metric.reset()
# For mask
mask_pred = [
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
]
mask_tgt = [
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
]
preds = [
dict(
masks=tensor([mask_pred], dtype=torch.bool),
scores=tensor([0.536]),
labels=tensor([0]),
)
]
target = [
dict(
masks=tensor([mask_tgt], dtype=torch.bool),
labels=tensor([0]),
)
]
metric = BoxLevelMeanAveragePrecision(iou_type="segm")
metric.update(target, preds)
metric.get()
Args:
box_format (str, optional): Params of torchmetrics.detection.MeanAveragePrecision. \
Defaults to 'xyxy'.
iou_type (str, optional): Params of torchmetrics.detection.MeanAveragePrecision. \
Defaults to "bbox".
extended_summary (bool, optional): Params of torchmetrics.detection.MeanAveragePrecision. \
Defaults to 'False'.
classwise (Dict[int, str], optional): Methods function to this repo, controls whether use Category name, \
like classwise = {0:'person', 1:'car', 2:'tea', 3:'cycle'}, label id map to name. Defaults to {}.
print_table (bool, optional): Methods specific to this repo, controls whether an ASCII table to printed. \
Defaults to True.
**kwargs: Other keyword arguments for torchmetrics.detection.MeanAveragePrecision.
"""
self.this_extend_summary = extended_summary
self.classwise = classwise
MeanAveragePrecision.__init__(self,
iou_type=iou_type,
box_format=box_format,
extended_summary=True,
**kwargs)
BaseMetric.__init__(self, print_table=print_table)
if len(self.iou_type) == 1:
self.prefix = ''
else:
raise ValueError('not support iou_type with multiple values')
iou_thr = str(round(self.iou_thresholds[0], 2)) + ':' + str(
round(self.iou_thresholds[-1], 2))
self.name2coco = {
f'mAP@{iou_thr}':
'map',
'mAP@50':
f'{self.prefix}map_50',
'mAP@75':
f'{self.prefix}map_75',
'mAP_s':
f'{self.prefix}map_small',
'mAP_m':
f'{self.prefix}map_medium',
'mAP_l':
f'{self.prefix}map_large',
'mAR_s':
f'{self.prefix}mar_small',
'mAR_m':
f'{self.prefix}mar_medium',
'mAR_l':
f'{self.prefix}mar_large',
f'mAR_max_dets@{self.max_detection_thresholds[0]}':
f'{self.prefix}mar_{self.max_detection_thresholds[0]}',
f'mAR_max_dets@{self.max_detection_thresholds[1]}':
f'{self.prefix}mar_{self.max_detection_thresholds[1]}',
f'mAR_max_dets@{self.max_detection_thresholds[2]}':
f'{self.prefix}mar_{self.max_detection_thresholds[2]}',
}
@time_cost_deco
def update(self, labels: List[Dict[str, Tensor]],
preds: List[Dict[str, Tensor]]) -> None:
MeanAveragePrecision.update(self, preds, labels)
@time_cost_deco
def get(self) -> dict:
res = MeanAveragePrecision.compute(self)
results_per_category = self._get_per_class_info(res)
results = dict()
if not self.this_extend_summary:
del res[f'{self.prefix}precision']
del res[f'{self.prefix}recall']
del res[f'{self.prefix}scores']
del res[f'{self.prefix}ious']
results['classes'] = res.pop('classes')
results['ALL'] = res
results.update(results_per_category)
else:
results[f'{self.prefix}precision'] = res.pop(
f'{self.prefix}precision')
results[f'{self.prefix}recall'] = res.pop(f'{self.prefix}recall')
results[f'{self.prefix}scores'] = res.pop(f'{self.prefix}scores')
results[f'{self.prefix}ious'] = res.pop(f'{self.prefix}ious')
results['classes'] = res.pop('classes')
results['ALL'] = res
results.update(results_per_category)
self.results = results
if self.print_table:
table = PrettyTable()
head = ['category']
head.extend([k for k, v in self.name2coco.items()])
table.field_names = head
all_row = ['All']
all_row.extend([
f"{self.results['ALL'][v].item():.4f}"
for k, v in self.name2coco.items()
])
table.add_row(all_row)
for cls_idx in self.results['classes'].tolist():
row = [self.results[cls_idx]['name']]
row.extend([
f'{self.results[cls_idx][v].item():.4f}'
for k, v in self.name2coco.items()
])
table.add_row(row)
print(table)
return self.results
def reset(self):
self.results = dict()
MeanAveragePrecision.reset(self)
@property
def table(self):
head = ['category']
head.extend([k for k, v in self.name2coco.items()])
all_row = ['All']
data = []
all_row.extend([
f"{self.results['ALL'][v].item():.4f}"
for k, v in self.name2coco.items()
])
data.append(all_row)
for cls_idx in self.results['classes'].tolist():
row = [self.results[cls_idx]['name']]
row.extend([
f'{self.results[cls_idx][v].item():.4f}'
for k, v in self.name2coco.items()
])
data.append(row)
df = pd.DataFrame(data).T
df.index = head
return df.T
def __repr__(self) -> str:
return (
f'{self.__class__.__name__}(iou_type={self.iou_type}, '
f'box_format={self.box_format}, '
f'iou_threshold={round(self.iou_thresholds[0],2)}:{round(self.iou_thresholds[-1],2)}, '
f'rec_threshold={round(self.rec_thresholds[0],2)}:{round(self.rec_thresholds[-1],2)})'
)
def _get_per_class_info(self, results: dict):
cat_ids2name = self.classwise
precisions = results['precision']
recalls = results['recall']
classes = results['classes']
iou_thrs = torch.tensor(self.iou_thresholds)
max_dets = self.max_detection_thresholds
# assert len(cat_ids) == precisions.shape[2]
results_per_category = {}
num_iou_thr, num_cls, num_area_rng, num_max_dets = recalls.shape
num_iou_thr, num_rec_thr, num_cls, num_area_rng, num_max_dets = precisions.shape
if len(cat_ids2name) != 0:
for idx, cls_name in cat_ids2name.items():
if not sum(classes[classes == idx]):
warnings.warn(
f"Category '{cls_name}', ID = {idx} not in preds or targets, but in classwise."
f'This information can be ignored if you are sure there is not a problem.'
)
for idx, cls in enumerate(classes):
cls = cls.item()
if len(cat_ids2name) != 0:
assert cls in cat_ids2name.keys(
), f'lables not in catIds2name, {cls} not in {cat_ids2name.keys()}'
cat_name = str(cat_ids2name[cls])
else:
cat_name = str(cls)
t = dict(name=cat_name)
# area range index 0: all area ranges
# max dets index -1: typically 100 per image
precision = precisions[:, :, idx, 0, -1]
precision = precision[precision > -1]
if precision.numel():
ap = torch.mean(precision)
else:
ap = torch.tensor([-1])
t[f'{self.prefix}map'] = ap
# get .5,, .75.
iou_idx = [torch.where(iou_thrs == iou)[0] for iou in [0.5, 0.75]]
ap = []
for iou, iou_t, in zip(iou_idx, [0.5, 0.75]):
precision = precisions[iou, :, idx, 0, -1]
precision = precision[precision > -1]
if precision.numel():
ap.append(torch.mean(precision))
else:
ap.append(torch.tensor([-1]))
t[f'{self.prefix}map_50'] = ap[0]
t[f'{self.prefix}map_75'] = ap[1]
# get small, medium, large
ap = []
for area in range(1, num_area_rng): # 1,2,3; 0 is all area
precision = precisions[:, :, idx, area, -1]
precision = precision[precision > -1]
if precision.numel():
ap.append(torch.mean(precision))
else:
ap.append(torch.tensor([-1]))
t[f'{self.prefix}map_small'] = ap[0]
t[f'{self.prefix}map_medium'] = ap[1]
t[f'{self.prefix}map_large'] = ap[2]
# mAR
recall = recalls[:, idx, 0, -1]
recall = recall[recall > -1]
if recall.numel():
ar = torch.mean(recall)
else:
ar = torch.tensor([-1])
t[f'{self.prefix}mar'] = ar
# small medium large
ar = []
for area in range(1, num_area_rng): # 1,2,3; 0 is all area
recall = recalls[:, idx, area, -1]
recall = recall[recall > -1]
if recall.numel():
ar.append(torch.mean(recall))
else:
ar.append(torch.tensor([-1]))
t[f'{self.prefix}mar_small'] = ar[0]
t[f'{self.prefix}mar_medium'] = ar[1]
t[f'{self.prefix}mar_large'] = ar[2]
for jdx, max_det in enumerate(max_dets): # 1,2,3; 0 is all area
recall = recalls[:, idx, 0, jdx]
recall = recall[recall > -1]
if recall.numel():
ar = torch.mean(recall)
else:
ar = torch.tensor([-1])
t[f'{self.prefix}mar_{max_det}'] = ar
results_per_category[cls] = t
return results_per_category