-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcuboid_metrics.py
More file actions
274 lines (245 loc) · 12.5 KB
/
cuboid_metrics.py
File metadata and controls
274 lines (245 loc) · 12.5 KB
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
import sys
from abc import abstractmethod
from typing import List, Optional, Union
from nucleus.annotation import AnnotationList, CuboidAnnotation
from nucleus.prediction import CuboidPrediction, PredictionList
from .base import Metric, ScalarResult
from .cuboid_utils import detection_iou, label_match_wrapper, recall_precision
from .filtering import ListOfAndFilters, ListOfOrAndFilters, apply_filters
from .filters import confidence_filter
class CuboidMetric(Metric):
"""Abstract class for metrics of cuboids.
The CuboidMetric class automatically filters incoming annotations and
predictions for only cuboid annotations. It also filters
predictions whose confidence is less than the provided confidence_threshold.
Finally, it provides support for enforcing matching labels. If
`enforce_label_match` is set to True, then annotations and predictions will
only be matched if they have the same label.
To create a new concrete CuboidMetric, override the `eval` function
with logic to define a metric between cuboid annotations and predictions.
"""
def __init__(
self,
enforce_label_match: bool = False,
confidence_threshold: float = 0.0,
annotation_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
prediction_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
):
"""Initializes CuboidMetric abstract object.
Args:
enforce_label_match: whether to enforce that annotation and prediction labels must match. Default False
confidence_threshold: minimum confidence threshold for predictions. Must be in [0, 1]. Default 0.0
annotation_filters: MetadataFilter predicates. Predicates are expressed in disjunctive normal form (DNF),
like [[MetadataFilter('x', '==', 0), FieldFilter('label', '==', 'pedestrian')], ...].
DNF allows arbitrary boolean logical combinations of single field predicates. The innermost structures
each describe a single field predicate. The list of inner predicates is interpreted as a conjunction
(AND), forming a more selective and multiple column predicate. Finally, the most outer list combines
these filters as a disjunction (OR).
prediction_filters: MetadataFilter predicates. Predicates are expressed in disjunctive normal form (DNF),
like [[MetadataFilter('x', '==', 0), FieldFilter('label', '==', 'pedestrian')], ...].
DNF allows arbitrary boolean logical combinations of single field predicates. The innermost structures
each describe a single field predicate. The list of inner predicates is interpreted as a conjunction
(AND), forming a more selective and multiple column predicate. Finally, the most outer list combines
these filters as a disjunction (OR).
"""
self.enforce_label_match = enforce_label_match
assert 0 <= confidence_threshold <= 1
self.confidence_threshold = confidence_threshold
self.annotation_filters = annotation_filters
self.prediction_filters = prediction_filters
@abstractmethod
def eval(
self,
annotations: List[CuboidAnnotation],
predictions: List[CuboidPrediction],
) -> ScalarResult:
# Main evaluation function that subclasses must override.
pass
def aggregate_score(self, results: List[ScalarResult]) -> ScalarResult: # type: ignore[override]
return ScalarResult.aggregate(results)
def __call__(
self, annotations: AnnotationList, predictions: PredictionList
) -> ScalarResult:
if self.confidence_threshold > 0:
predictions = confidence_filter(
predictions, self.confidence_threshold
)
cuboid_annotations: List[CuboidAnnotation] = []
cuboid_annotations.extend(annotations.cuboid_annotations)
cuboid_predictions: List[CuboidPrediction] = []
cuboid_predictions.extend(predictions.cuboid_predictions)
eval_fn = label_match_wrapper(self.eval)
cuboid_annotations = apply_filters(
cuboid_annotations, self.annotation_filters # type: ignore
)
cuboid_predictions = apply_filters(
cuboid_predictions, self.prediction_filters # type: ignore
)
result = eval_fn(
cuboid_annotations,
cuboid_predictions,
enforce_label_match=self.enforce_label_match,
)
return result
class CuboidIOU(CuboidMetric):
"""Calculates the average IOU between cuboid annotations and predictions."""
# TODO: Remove defaults once these are surfaced more cleanly to users.
def __init__(
self,
enforce_label_match: bool = True,
iou_threshold: float = 0.0,
confidence_threshold: float = 0.0,
iou_2d: bool = False,
annotation_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
prediction_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
):
"""Initializes CuboidIOU object.
Args:
enforce_label_match: whether to enforce that annotation and prediction labels must match. Defaults to True
iou_threshold: IOU threshold to consider detection as valid. Must be in [0, 1]. Default 0.0
iou_2d: whether to return the BEV 2D IOU if true, or the 3D IOU if false.
confidence_threshold: minimum confidence threshold for predictions. Must be in [0, 1]. Default 0.0
annotation_filters: MetadataFilter predicates. Predicates are expressed in disjunctive normal form (DNF), like
[[MetadataFilter('x', '=', 0), ...], ...]. DNF allows arbitrary boolean logical combinations of single field
predicates. The innermost structures each describe a single column predicate. The list of inner predicates is
interpreted as a conjunction (AND), forming a more selective and multiple column predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
prediction_filters: MetadataFilter predicates. Predicates are expressed in disjunctive normal form (DNF), like
[[MetadataFilter('x', '=', 0), ...], ...]. DNF allows arbitrary boolean logical combinations of single field
predicates. The innermost structures each describe a single column predicate. The list of inner predicates is
interpreted as a conjunction (AND), forming a more selective and multiple column predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
"""
assert (
0 <= iou_threshold <= 1
), "IoU threshold must be between 0 and 1."
self.iou_threshold = iou_threshold
self.iou_2d = iou_2d
super().__init__(
enforce_label_match=enforce_label_match,
confidence_threshold=confidence_threshold,
annotation_filters=annotation_filters,
prediction_filters=prediction_filters,
)
def eval(
self,
annotations: List[CuboidAnnotation],
predictions: List[CuboidPrediction],
) -> ScalarResult:
iou_3d_metric, iou_2d_metric = detection_iou(
predictions,
annotations,
threshold_in_overlap_ratio=self.iou_threshold,
)
weight = max(len(annotations), len(predictions))
if self.iou_2d:
avg_iou = iou_2d_metric.sum() / max(weight, sys.float_info.epsilon)
else:
avg_iou = iou_3d_metric.sum() / max(weight, sys.float_info.epsilon)
return ScalarResult(avg_iou, weight)
class CuboidPrecision(CuboidMetric):
"""Calculates the average precision between cuboid annotations and predictions."""
# TODO: Remove defaults once these are surfaced more cleanly to users.
def __init__(
self,
enforce_label_match: bool = True,
iou_threshold: float = 0.0,
confidence_threshold: float = 0.0,
annotation_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
prediction_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
):
"""Initializes CuboidIOU object.
Args:
enforce_label_match: whether to enforce that annotation and prediction labels must match. Defaults to True
iou_threshold: IOU threshold to consider detection as valid. Must be in [0, 1]. Default 0.0
confidence_threshold: minimum confidence threshold for predictions. Must be in [0, 1]. Default 0.0
annotation_filters: MetadataFilter predicates. Predicates are expressed in disjunctive normal form (DNF), like
[[MetadataFilter('x', '==', 0), ...], ...]. DNF allows arbitrary boolean logical combinations of single field
predicates. The innermost structures each describe a single column predicate. The list of inner predicates is
interpreted as a conjunction (AND), forming a more selective and multiple column predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
prediction_filters: MetadataFilter predicates. Predicates are expressed in disjunctive normal form (DNF), like
[[MetadataFilter('x', '==', 0), ...], ...]. DNF allows arbitrary boolean logical combinations of single field
predicates. The innermost structures each describe a single column predicate. The list of inner predicates is
interpreted as a conjunction (AND), forming a more selective and multiple column predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
"""
assert (
0 <= iou_threshold <= 1
), "IoU threshold must be between 0 and 1."
self.iou_threshold = iou_threshold
super().__init__(
enforce_label_match=enforce_label_match,
confidence_threshold=confidence_threshold,
annotation_filters=annotation_filters,
prediction_filters=prediction_filters,
)
def eval(
self,
annotations: List[CuboidAnnotation],
predictions: List[CuboidPrediction],
) -> ScalarResult:
stats = recall_precision(
predictions,
annotations,
threshold_in_overlap_ratio=self.iou_threshold,
)
weight = stats["tp_sum"] + stats["fp_sum"]
precision = stats["tp_sum"] / max(weight, sys.float_info.epsilon)
return ScalarResult(precision, weight)
class CuboidRecall(CuboidMetric):
"""Calculates the average recall between cuboid annotations and predictions."""
# TODO: Remove defaults once these are surfaced more cleanly to users.
def __init__(
self,
enforce_label_match: bool = True,
iou_threshold: float = 0.0,
confidence_threshold: float = 0.0,
annotation_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
prediction_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
):
"""Initializes CuboidIOU object.
Args:
enforce_label_match: whether to enforce that annotation and prediction labels must match. Defaults to True
iou_threshold: IOU threshold to consider detection as valid. Must be in [0, 1]. Default 0.0
confidence_threshold: minimum confidence threshold for predictions. Must be in [0, 1]. Default 0.0
"""
assert (
0 <= iou_threshold <= 1
), "IoU threshold must be between 0 and 1."
self.iou_threshold = iou_threshold
super().__init__(
enforce_label_match=enforce_label_match,
confidence_threshold=confidence_threshold,
annotation_filters=annotation_filters,
prediction_filters=prediction_filters,
)
def eval(
self,
annotations: List[CuboidAnnotation],
predictions: List[CuboidPrediction],
) -> ScalarResult:
stats = recall_precision(
predictions,
annotations,
threshold_in_overlap_ratio=self.iou_threshold,
)
weight = stats["tp_sum"] + stats["fn_sum"]
recall = stats["tp_sum"] / max(weight, sys.float_info.epsilon)
return ScalarResult(recall, weight)