-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsegmentation_to_poly_metrics.py
More file actions
655 lines (601 loc) · 28.5 KB
/
Copy pathsegmentation_to_poly_metrics.py
File metadata and controls
655 lines (601 loc) · 28.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import abc
import logging
from enum import Enum
from typing import List, Optional, Union
import numpy as np
from nucleus.annotation import AnnotationList, SegmentationAnnotation
from nucleus.metrics.base import MetricResult
from nucleus.metrics.filtering import (
ListOfAndFilters,
ListOfOrAndFilters,
apply_filters,
)
from nucleus.metrics.segmentation_utils import (
instance_mask_to_polys,
rasterize_polygons_to_segmentation_mask,
setup_iou_thresholds,
transform_poly_codes_to_poly_preds,
)
from nucleus.prediction import PredictionList
from .segmentation_loader import InMemoryLoader, SegmentationMaskLoader
from .segmentation_metrics import (
SegmentationIOU,
SegmentationMAP,
SegmentationPrecision,
SegmentationRecall,
)
try:
from s3fs import S3FileSystem
except (ModuleNotFoundError, OSError):
from ..package_not_installed import PackageNotInstalled
S3FileSystem = PackageNotInstalled
from .base import Metric, ScalarResult
from .polygon_metrics import (
PolygonAveragePrecision,
PolygonIOU,
PolygonPrecision,
PolygonRecall,
)
class SegToPolyMode(str, Enum):
GENERATE_GT_FROM_POLY = "gt_from_poly"
GENERATE_PRED_POLYS_FROM_MASK = "gt_from_poly"
class SegmentationMaskToPolyMetric(Metric):
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,
mode: SegToPolyMode = SegToPolyMode.GENERATE_GT_FROM_POLY,
):
"""Initializes PolygonMetric 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: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
prediction_filters: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
"""
# Since segmentation annotations are very different from everything else we can't rely on the upper filtering
super().__init__(None, None)
self._annotation_filters = annotation_filters
self._prediction_filters = prediction_filters
self.enforce_label_match = enforce_label_match
assert 0 <= confidence_threshold <= 1
self.confidence_threshold = confidence_threshold
self.loader = SegmentationMaskLoader(S3FileSystem(anon=False))
self.mode = mode
def call_metric(
self, annotations: AnnotationList, predictions: PredictionList
) -> MetricResult:
assert (
len(predictions.segmentation_predictions) <= 1
), f"Expected only one segmentation mask, got {predictions.segmentation_predictions}"
prediction = (
predictions.segmentation_predictions[0]
if predictions.segmentation_predictions
else None
)
annotations.polygon_annotations = apply_filters(
annotations.polygon_annotations, self._annotation_filters # type: ignore
)
annotations.box_annotations = apply_filters(
annotations.box_annotations, self._annotation_filters # type: ignore
)
predictions.segmentation_predictions = apply_filters(
predictions.segmentation_predictions, self._prediction_filters # type: ignore
)
if prediction:
if self.mode == SegToPolyMode.GENERATE_GT_FROM_POLY:
pred_img = self.loader.fetch(prediction.mask_url)
ann_img, segments = rasterize_polygons_to_segmentation_mask(
annotations.polygon_annotations
+ annotations.box_annotations, # type:ignore
pred_img.shape,
)
# TODO: apply Segmentation filters after?
annotations.segmentation_annotations = [
SegmentationAnnotation(
"__no_url",
annotations=segments,
reference_id=annotations.polygon_annotations[
0
].reference_id,
)
]
return self.call_segmentation_metric(
annotations,
np.asarray(ann_img),
predictions,
np.asarray(pred_img),
)
elif self.mode == SegToPolyMode.GENERATE_PRED_POLYS_FROM_MASK:
pred_img = self.loader.fetch(prediction.mask_url)
pred_value, pred_polys = instance_mask_to_polys(
np.asarray(pred_img)
) # typing: ignore
code_to_label = {
s.index: s.label for s in prediction.annotations
}
poly_predictions = transform_poly_codes_to_poly_preds(
prediction.reference_id,
pred_value,
pred_polys,
code_to_label,
)
return self.call_poly_metric(
annotations,
PredictionList(polygon_predictions=poly_predictions),
)
else:
raise RuntimeError(
f"Misonconfigured class. Got mode '{self.mode}', expected one of {list(SegToPolyMode)}"
)
else:
return ScalarResult(0, weight=0)
def call_segmentation_metric(
self,
annotations: AnnotationList,
ann_img: np.ndarray,
predictions: PredictionList,
pred_img: np.ndarray,
):
metric = self.configure_metric()
metric.loader = InMemoryLoader(
{
annotations.segmentation_annotations[0].mask_url: ann_img,
predictions.segmentation_predictions[0].mask_url: pred_img,
}
)
return metric(annotations, predictions)
def call_poly_metric(
self, annotations: AnnotationList, predictions: PredictionList
):
metric = self.configure_metric()
return metric(annotations, predictions)
def aggregate_score(self, results: List[MetricResult]) -> ScalarResult:
metric = self.configure_metric()
return metric.aggregate_score(results) # type: ignore
@abc.abstractmethod
def configure_metric(self):
pass
class SegmentationToPolyIOU(SegmentationMaskToPolyMetric):
def __init__(
self,
enforce_label_match: bool = False,
iou_threshold: float = 0.0,
confidence_threshold: float = 0.0,
annotation_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
prediction_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
mode: SegToPolyMode = SegToPolyMode.GENERATE_GT_FROM_POLY,
):
"""Initializes PolygonIOU object.
Args:
enforce_label_match: whether to enforce that annotation and prediction labels must match. Defaults to False
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: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
prediction_filters: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field 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,
confidence_threshold,
annotation_filters,
prediction_filters,
mode,
)
def configure_metric(self):
if self.mode == SegToPolyMode.GENERATE_GT_FROM_POLY:
metric = SegmentationIOU(
self.annotation_filters,
self.prediction_filters,
)
else:
metric = PolygonIOU(
self.enforce_label_match,
self.iou_threshold,
self.confidence_threshold,
self.annotation_filters,
self.prediction_filters,
)
return metric
class SegmentationToPolyPrecision(SegmentationMaskToPolyMetric):
def __init__(
self,
enforce_label_match: bool = False,
iou_threshold: float = 0.5,
confidence_threshold: float = 0.0,
annotation_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
prediction_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
mode: SegToPolyMode = SegToPolyMode.GENERATE_GT_FROM_POLY,
):
"""Initializes SegmentationToPolyPrecision object.
Args:
enforce_label_match: whether to enforce that annotation and prediction labels must match. Defaults to False
iou_threshold: IOU threshold to consider detection as valid. Must be in [0, 1]. Default 0.5
confidence_threshold: minimum confidence threshold for predictions. Must be in [0, 1]. Default 0.0
annotation_filters: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
prediction_filters: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field 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,
confidence_threshold,
annotation_filters,
prediction_filters,
mode,
)
def configure_metric(self):
if self.mode == SegToPolyMode.GENERATE_GT_FROM_POLY:
metric = SegmentationPrecision(
self.annotation_filters,
self.prediction_filters,
self.iou_threshold,
)
else:
metric = PolygonPrecision(
self.enforce_label_match,
self.iou_threshold,
self.confidence_threshold,
self.annotation_filters,
self.prediction_filters,
)
return metric
class SegmentationToPolyRecall(SegmentationMaskToPolyMetric):
"""Calculates the recall between box or polygon annotations and predictions.
::
from nucleus import BoxAnnotation, Point, PolygonPrediction
from nucleus.annotation import AnnotationList
from nucleus.prediction import PredictionList
from nucleus.metrics import PolygonRecall
box_anno = BoxAnnotation(
label="car",
x=0,
y=0,
width=10,
height=10,
reference_id="image_1",
annotation_id="image_1_car_box_1",
metadata={"vehicle_color": "red"}
)
polygon_pred = PolygonPrediction(
label="bus",
vertices=[Point(100, 100), Point(150, 200), Point(200, 100)],
reference_id="image_2",
annotation_id="image_2_bus_polygon_1",
confidence=0.8,
metadata={"vehicle_color": "yellow"}
)
annotations = AnnotationList(box_annotations=[box_anno])
predictions = PredictionList(polygon_predictions=[polygon_pred])
metric = PolygonRecall()
metric(annotations, predictions)
"""
# TODO: Remove defaults once these are surfaced more cleanly to users.
def __init__(
self,
enforce_label_match: bool = False,
iou_threshold: float = 0.5,
confidence_threshold: float = 0.0,
annotation_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
prediction_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
mode: SegToPolyMode = SegToPolyMode.GENERATE_GT_FROM_POLY,
):
"""Initializes PolygonRecall object.
Args:
enforce_label_match: whether to enforce that annotation and prediction labels must match. Defaults to False
iou_threshold: IOU threshold to consider detection as valid. Must be in [0, 1]. Default 0.5
confidence_threshold: minimum confidence threshold for predictions. Must be in [0, 1]. Default 0.0
annotation_filters: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
prediction_filters: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field 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,
confidence_threshold,
annotation_filters,
prediction_filters,
mode,
)
def configure_metric(self):
if self.mode == SegToPolyMode.GENERATE_GT_FROM_POLY:
metric = SegmentationRecall(
self.annotation_filters,
self.prediction_filters,
self.iou_threshold,
)
else:
metric = PolygonRecall(
self.enforce_label_match,
self.iou_threshold,
self.confidence_threshold,
self.annotation_filters,
self.prediction_filters,
)
return metric
class SegmentationToPolyAveragePrecision(SegmentationMaskToPolyMetric):
"""Calculates the average precision between box or polygon annotations and predictions.
::
from nucleus import BoxAnnotation, Point, PolygonPrediction
from nucleus.annotation import AnnotationList
from nucleus.prediction import PredictionList
from nucleus.metrics import PolygonAveragePrecision
box_anno = BoxAnnotation(
label="car",
x=0,
y=0,
width=10,
height=10,
reference_id="image_1",
annotation_id="image_1_car_box_1",
metadata={"vehicle_color": "red"}
)
polygon_pred = PolygonPrediction(
label="bus",
vertices=[Point(100, 100), Point(150, 200), Point(200, 100)],
reference_id="image_2",
annotation_id="image_2_bus_polygon_1",
confidence=0.8,
metadata={"vehicle_color": "yellow"}
)
annotations = AnnotationList(box_annotations=[box_anno])
predictions = PredictionList(polygon_predictions=[polygon_pred])
metric = PolygonAveragePrecision(label="car")
metric(annotations, predictions)
"""
# TODO: Remove defaults once these are surfaced more cleanly to users.
def __init__(
self,
label,
iou_threshold: float = 0.5,
annotation_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
prediction_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
mode: SegToPolyMode = SegToPolyMode.GENERATE_GT_FROM_POLY,
):
"""Initializes PolygonRecall object.
Args:
iou_threshold: IOU threshold to consider detection as valid. Must be in [0, 1]. Default 0.5
annotation_filters: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
prediction_filters: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field 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.label = label
super().__init__(
enforce_label_match=False,
confidence_threshold=0,
annotation_filters=annotation_filters,
prediction_filters=prediction_filters,
)
def configure_metric(self):
if self.mode == SegToPolyMode.GENERATE_GT_FROM_POLY:
# TODO(gunnar): Add a label filter
metric = SegmentationPrecision(
self.annotation_filters,
self.prediction_filters,
self.iou_threshold,
)
else:
metric = PolygonAveragePrecision(
self.label,
self.iou_threshold,
self.annotation_filters,
self.prediction_filters,
)
return metric
class SegmentationToPolyMAP(SegmentationMaskToPolyMetric):
"""Calculates the mean average precision between box or polygon annotations and predictions.
::
from nucleus import BoxAnnotation, Point, PolygonPrediction
from nucleus.annotation import AnnotationList
from nucleus.prediction import PredictionList
from nucleus.metrics import PolygonMAP
box_anno = BoxAnnotation(
label="car",
x=0,
y=0,
width=10,
height=10,
reference_id="image_1",
annotation_id="image_1_car_box_1",
metadata={"vehicle_color": "red"}
)
polygon_pred = PolygonPrediction(
label="bus",
vertices=[Point(100, 100), Point(150, 200), Point(200, 100)],
reference_id="image_2",
annotation_id="image_2_bus_polygon_1",
confidence=0.8,
metadata={"vehicle_color": "yellow"}
)
annotations = AnnotationList(box_annotations=[box_anno])
predictions = PredictionList(polygon_predictions=[polygon_pred])
metric = PolygonMAP()
metric(annotations, predictions)
"""
# TODO: Remove defaults once these are surfaced more cleanly to users.
def __init__(
self,
iou_threshold: float = -1,
iou_thresholds: Union[List[float], str] = "coco",
annotation_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
prediction_filters: Optional[
Union[ListOfOrAndFilters, ListOfAndFilters]
] = None,
mode: SegToPolyMode = SegToPolyMode.GENERATE_GT_FROM_POLY,
):
"""Initializes PolygonRecall object.
Args:
iou_thresholds: IOU thresholds to check AP at
annotation_filters: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
prediction_filters: Filter predicates. Allowed formats are:
ListOfAndFilters where each Filter forms a chain of AND predicates.
or
ListOfOrAndFilters where Filters are expressed in disjunctive normal form (DNF), like
[[MetadataFilter("short_haired", "==", True), FieldFilter("label", "in", ["cat", "dog"]), ...].
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 field predicate.
Finally, the most outer list combines these filters as a disjunction (OR).
"""
if iou_threshold:
logging.warning(
"Got deprecated parameter 'iou_threshold'. Ignoring it."
)
self.iou_thresholds = setup_iou_thresholds(iou_thresholds)
super().__init__(
False, 0, annotation_filters, prediction_filters, mode
)
def configure_metric(self):
if self.mode == SegToPolyMode.GENERATE_GT_FROM_POLY:
# TODO(gunnar): Add a label filter
metric = SegmentationMAP(
self.annotation_filters,
self.prediction_filters,
self.iou_thresholds,
)
else:
def patched_average_precision(annotations, predictions):
ap_per_threshold = []
labels = [p.label for p in predictions.polygon_predictions]
for threshold in self.iou_thresholds:
ap_per_label = []
for label in labels:
call_metric = PolygonAveragePrecision(
label,
iou_threshold=threshold,
annotation_filters=self.annotation_filters,
prediction_filters=self.prediction_filters,
)
result = call_metric(annotations, predictions)
ap_per_label.append(result.value) # type: ignore
ap_per_threshold = np.mean(ap_per_label)
thresholds = np.concatenate([[0], self.iou_thresholds, [1]])
steps = np.diff(thresholds)
mean_ap = (
np.array(ap_per_threshold + [ap_per_threshold[-1]]) * steps
).sum()
return ScalarResult(mean_ap)
metric = patched_average_precision
return metric