Skip to content

Metrics

Metric dataclass

Bases: BaseMetric

Object Detection Metric.

Attributes:

Name Type Description
type str

The metric type.

value int | float | dict

The metric value.

parameters dict[str, Any]

A dictionary containing metric parameters.

Source code in valor_lite/object_detection/metric.py
 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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
@dataclass
class Metric(BaseMetric):
    """
    Object Detection Metric.

    Attributes
    ----------
    type : str
        The metric type.
    value : int | float | dict
        The metric value.
    parameters : dict[str, Any]
        A dictionary containing metric parameters.
    """

    def __post_init__(self):
        if not isinstance(self.type, str):
            raise TypeError(
                f"Metric type should be of type 'str': {self.type}"
            )
        elif not isinstance(self.value, (int, float, dict)):
            raise TypeError(
                f"Metric value must be of type 'int', 'float' or 'dict': {self.value}"
            )
        elif not isinstance(self.parameters, dict):
            raise TypeError(
                f"Metric parameters must be of type 'dict[str, Any]': {self.parameters}"
            )
        elif not all([isinstance(k, str) for k in self.parameters.keys()]):
            raise TypeError(
                f"Metric parameter dictionary should only have keys with type 'str': {self.parameters}"
            )

    @classmethod
    def precision(
        cls,
        value: float,
        label: str,
        iou_threshold: float,
        score_threshold: float,
    ):
        """
        Precision metric for a specific class label in object detection.

        This class encapsulates a metric value for a particular class label,
        along with the associated Intersection over Union (IOU) threshold and
        confidence score threshold.

        Parameters
        ----------
        value : float
            The metric value.
        label : str
            The class label for which the metric is calculated.
        iou_threshold : float
            The IOU threshold used to determine matches between predicted and ground truth boxes.
        score_threshold : float
            The confidence score threshold above which predictions are considered.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.Precision.value,
            value=value,
            parameters={
                "label": label,
                "iou_threshold": iou_threshold,
                "score_threshold": score_threshold,
            },
        )

    @classmethod
    def recall(
        cls,
        value: float,
        label: str,
        iou_threshold: float,
        score_threshold: float,
    ):
        """
        Recall metric for a specific class label in object detection.

        This class encapsulates a metric value for a particular class label,
        along with the associated Intersection over Union (IOU) threshold and
        confidence score threshold.

        Parameters
        ----------
        value : float
            The metric value.
        label : str
            The class label for which the metric is calculated.
        iou_threshold : float
            The IOU threshold used to determine matches between predicted and ground truth boxes.
        score_threshold : float
            The confidence score threshold above which predictions are considered.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.Recall.value,
            value=value,
            parameters={
                "label": label,
                "iou_threshold": iou_threshold,
                "score_threshold": score_threshold,
            },
        )

    @classmethod
    def f1_score(
        cls,
        value: float,
        label: str,
        iou_threshold: float,
        score_threshold: float,
    ):
        """
        F1 score for a specific class label in object detection.

        This class encapsulates a metric value for a particular class label,
        along with the associated Intersection over Union (IOU) threshold and
        confidence score threshold.

        Parameters
        ----------
        value : float
            The metric value.
        label : str
            The class label for which the metric is calculated.
        iou_threshold : float
            The IOU threshold used to determine matches between predicted and ground truth boxes.
        score_threshold : float
            The confidence score threshold above which predictions are considered.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.F1.value,
            value=value,
            parameters={
                "label": label,
                "iou_threshold": iou_threshold,
                "score_threshold": score_threshold,
            },
        )

    @classmethod
    def accuracy(
        cls,
        value: float,
        iou_threshold: float,
        score_threshold: float,
    ):
        """
        Accuracy metric for the object detection task type.

        This class encapsulates a metric value at a specific Intersection
        over Union (IOU) threshold and confidence score threshold.

        Parameters
        ----------
        value : float
            The metric value.
        iou_threshold : float
            The IOU threshold used to determine matches between predicted and ground truth boxes.
        score_threshold : float
            The confidence score threshold above which predictions are considered.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.Accuracy.value,
            value=value,
            parameters={
                "iou_threshold": iou_threshold,
                "score_threshold": score_threshold,
            },
        )

    @classmethod
    def average_precision(
        cls,
        value: float,
        iou_threshold: float,
        label: str,
    ):
        """
        Average Precision (AP) metric for object detection tasks.

        The AP computation uses 101-point interpolation, which calculates the average
        precision by interpolating the precision-recall curve at 101 evenly spaced recall
        levels from 0 to 1.

        Parameters
        ----------
        value : float
            The average precision value.
        iou_threshold : float
            The IOU threshold used to compute the AP.
        label : str
            The class label for which the AP is computed.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.AP.value,
            value=value,
            parameters={
                "iou_threshold": iou_threshold,
                "label": label,
            },
        )

    @classmethod
    def mean_average_precision(
        cls,
        value: float,
        iou_threshold: float,
    ):
        """
        Mean Average Precision (mAP) metric for object detection tasks.

        The AP computation uses 101-point interpolation, which calculates the average
        precision for each class by interpolating the precision-recall curve at 101 evenly
        spaced recall levels from 0 to 1. The mAP is then calculated by averaging these
        values across all class labels.

        Parameters
        ----------
        value : float
            The mean average precision value.
        iou_threshold : float
            The IOU threshold used to compute the mAP.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.mAP.value,
            value=value,
            parameters={
                "iou_threshold": iou_threshold,
            },
        )

    @classmethod
    def average_precision_averaged_over_IOUs(
        cls,
        value: float,
        iou_thresholds: list[float],
        label: str,
    ):
        """
        Average Precision (AP) metric averaged over multiple IOU thresholds.

        The AP computation uses 101-point interpolation, which calculates the average precision
        by interpolating the precision-recall curve at 101 evenly spaced recall levels from 0 to 1
        for each IOU threshold specified in `iou_thresholds`. The final APAveragedOverIOUs value is
        obtained by averaging these AP values across all specified IOU thresholds.

        Parameters
        ----------
        value : float
            The average precision value averaged over the specified IOU thresholds.
        iou_thresholds : list[float]
            The list of IOU thresholds used to compute the AP values.
        label : str
            The class label for which the AP is computed.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.APAveragedOverIOUs.value,
            value=value,
            parameters={
                "iou_thresholds": iou_thresholds,
                "label": label,
            },
        )

    @classmethod
    def mean_average_precision_averaged_over_IOUs(
        cls,
        value: float,
        iou_thresholds: list[float],
    ):
        """
        Mean Average Precision (mAP) metric averaged over multiple IOU thresholds.

        The AP computation uses 101-point interpolation, which calculates the average precision
        by interpolating the precision-recall curve at 101 evenly spaced recall levels from 0 to 1
        for each IOU threshold specified in `iou_thresholds`. The final mAPAveragedOverIOUs value is
        obtained by averaging these AP values across all specified IOU thresholds and all class labels.

        Parameters
        ----------
        value : float
            The average precision value averaged over the specified IOU thresholds.
        iou_thresholds : list[float]
            The list of IOU thresholds used to compute the AP values.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.mAPAveragedOverIOUs.value,
            value=value,
            parameters={
                "iou_thresholds": iou_thresholds,
            },
        )

    @classmethod
    def average_recall(
        cls,
        value: float,
        score_threshold: float,
        iou_thresholds: list[float],
        label: str,
    ):
        """
        Average Recall (AR) metric for object detection tasks.

        The AR computation considers detections with confidence scores above the specified
        `score_threshold` and calculates the recall at each IOU threshold in `iou_thresholds`.
        The final AR value is the average of these recall values across all specified IOU
        thresholds.

        Parameters
        ----------
        value : float
            The average recall value averaged over the specified IOU thresholds.
        score_threshold : float
            The detection score threshold; only detections with confidence scores above this
            threshold are considered.
        iou_thresholds : list[float]
            The list of IOU thresholds used to compute the recall values.
        label : str
            The class label for which the AR is computed.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.AR.value,
            value=value,
            parameters={
                "iou_thresholds": iou_thresholds,
                "score_threshold": score_threshold,
                "label": label,
            },
        )

    @classmethod
    def mean_average_recall(
        cls,
        value: float,
        score_threshold: float,
        iou_thresholds: list[float],
    ):
        """
        Mean Average Recall (mAR) metric for object detection tasks.

        The mAR computation considers detections with confidence scores above the specified
        `score_threshold` and calculates recall at each IOU threshold in `iou_thresholds` for
        each label. The final mAR value is obtained by averaging these recall values over the
        specified IOU thresholds and then averaging across all labels.

        Parameters
        ----------
        value : float
            The mean average recall value averaged over the specified IOU thresholds.
        score_threshold : float
            The detection score threshold; only detections with confidence scores above this
            threshold are considered.
        iou_thresholds : list[float]
            The list of IOU thresholds used to compute the recall values.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.mAR.value,
            value=value,
            parameters={
                "iou_thresholds": iou_thresholds,
                "score_threshold": score_threshold,
            },
        )

    @classmethod
    def average_recall_averaged_over_scores(
        cls,
        value: float,
        score_thresholds: list[float],
        iou_thresholds: list[float],
        label: str,
    ):
        """
        Average Recall (AR) metric averaged over multiple score thresholds for a specific object class label.

        The AR computation considers detections across multiple `score_thresholds` and calculates
        recall at each IOU threshold in `iou_thresholds`. The final AR value is obtained by averaging
        the recall values over all specified score thresholds and IOU thresholds.

        Parameters
        ----------
        value : float
            The average recall value averaged over the specified score thresholds and IOU thresholds.
        score_thresholds : list[float]
            The list of detection score thresholds; detections with confidence scores above each threshold are considered.
        iou_thresholds : list[float]
            The list of IOU thresholds used to compute the recall values.
        label : str
            The class label for which the AR is computed.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.ARAveragedOverScores.value,
            value=value,
            parameters={
                "iou_thresholds": iou_thresholds,
                "score_thresholds": score_thresholds,
                "label": label,
            },
        )

    @classmethod
    def mean_average_recall_averaged_over_scores(
        cls,
        value: float,
        score_thresholds: list[float],
        iou_thresholds: list[float],
    ):
        """
        Mean Average Recall (mAR) metric averaged over multiple score thresholds and IOU thresholds.

        The mAR computation considers detections across multiple `score_thresholds`, calculates recall
        at each IOU threshold in `iou_thresholds` for each label, averages these recall values over all
        specified score thresholds and IOU thresholds, and then computes the mean across all labels to
        obtain the final mAR value.

        Parameters
        ----------
        value : float
            The mean average recall value averaged over the specified score thresholds and IOU thresholds.
        score_thresholds : list[float]
            The list of detection score thresholds; detections with confidence scores above each threshold are considered.
        iou_thresholds : list[float]
            The list of IOU thresholds used to compute the recall values.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.mARAveragedOverScores.value,
            value=value,
            parameters={
                "iou_thresholds": iou_thresholds,
                "score_thresholds": score_thresholds,
            },
        )

    @classmethod
    def precision_recall_curve(
        cls,
        precisions: list[float],
        scores: list[float],
        iou_threshold: float,
        label: str,
    ):
        """
        Interpolated precision-recall curve over 101 recall points.

        The precision values are interpolated over recalls ranging from 0.0 to 1.0 in steps of 0.01,
        resulting in 101 points. This is a byproduct of the 101-point interpolation used in calculating
        the Average Precision (AP) metric in object detection tasks.

        Parameters
        ----------
        precisions : list[float]
            Interpolated precision values corresponding to recalls at 0.0, 0.01, ..., 1.0.
        scores : list[float]
            Maximum prediction score for each point on the interpolated curve.
        iou_threshold : float
            The Intersection over Union (IOU) threshold used to determine true positives.
        label : str
            The class label associated with this precision-recall curve.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.PrecisionRecallCurve.value,
            value={
                "precisions": precisions,
                "scores": scores,
            },
            parameters={
                "iou_threshold": iou_threshold,
                "label": label,
            },
        )

    @classmethod
    def counts(
        cls,
        tp: int,
        fp: int,
        fn: int,
        label: str,
        iou_threshold: float,
        score_threshold: float,
    ):
        """
        `Counts` encapsulates the counts of true positives (`tp`), false positives (`fp`),
        and false negatives (`fn`) for object detection evaluation, along with the associated
        class label, Intersection over Union (IOU) threshold, and confidence score threshold.

        Parameters
        ----------
        tp : int
            Number of true positives.
        fp : int
            Number of false positives.
        fn : int
            Number of false negatives.
        label : str
            The class label for which the counts are calculated.
        iou_threshold : float
            The IOU threshold used to determine a match between predicted and ground truth boxes.
        score_threshold : float
            The confidence score threshold above which predictions are considered.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.Counts.value,
            value={
                "tp": tp,
                "fp": fp,
                "fn": fn,
            },
            parameters={
                "iou_threshold": iou_threshold,
                "score_threshold": score_threshold,
                "label": label,
            },
        )

    @classmethod
    def confusion_matrix(
        cls,
        confusion_matrix: dict[
            str,  # ground truth label value
            dict[
                str,  # prediction label value
                dict[
                    str,  # either `count` or `examples`
                    int
                    | list[
                        dict[
                            str,  # either `datum`, `groundtruth`, `prediction` or score
                            str  # datum uid
                            | dict[
                                str, float
                            ]  # bounding box (xmin, xmax, ymin, ymax)
                            | float,  # prediction score
                        ]
                    ],
                ],
            ],
        ],
        unmatched_predictions: dict[
            str,  # prediction label value
            dict[
                str,  # either `count` or `examples`
                int
                | list[
                    dict[
                        str,  # either `datum`, `prediction` or score
                        str  # datum uid
                        | float  # prediction score
                        | dict[
                            str, float
                        ],  # bounding box (xmin, xmax, ymin, ymax)
                    ]
                ],
            ],
        ],
        unmatched_ground_truths: dict[
            str,  # ground truth label value
            dict[
                str,  # either `count` or `examples`
                int
                | list[
                    dict[
                        str,  # either `datum` or `groundtruth`
                        str  # datum uid
                        | dict[
                            str, float
                        ],  # bounding box (xmin, xmax, ymin, ymax)
                    ]
                ],
            ],
        ],
        score_threshold: float,
        iou_threshold: float,
        maximum_number_of_examples: int,
    ):
        """
        Confusion matrix for object detection tasks.

        This class encapsulates detailed information about the model's performance, including correct
        predictions, misclassifications, unmatched_predictions (subset of false positives), and unmatched ground truths
        (subset of false negatives). It provides counts and examples for each category to facilitate in-depth analysis.

        Confusion Matrix Format:
        {
            <ground truth label>: {
                <prediction label>: {
                    'count': int,
                    'examples': [
                        {
                            'datum': str,
                            'groundtruth': dict,  # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float}
                            'prediction': dict,   # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float}
                            'score': float,
                        },
                        ...
                    ],
                },
                ...
            },
            ...
        }

        Unmatched Predictions Format:
        {
            <prediction label>: {
                'count': int,
                'examples': [
                    {
                        'datum': str,
                        'prediction': dict,  # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float}
                        'score': float,
                    },
                    ...
                ],
            },
            ...
        }

        Unmatched Ground Truths Format:
        {
            <ground truth label>: {
                'count': int,
                'examples': [
                    {
                        'datum': str,
                        'groundtruth': dict,  # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float}
                    },
                    ...
                ],
            },
            ...
        }

        Parameters
        ----------
        confusion_matrix : dict
            A nested dictionary where the first key is the ground truth label value, the second key
            is the prediction label value, and the innermost dictionary contains either a `count`
            or a list of `examples`. Each example includes the datum UID, ground truth bounding box,
            predicted bounding box, and prediction scores.
        unmatched_predictions : dict
            A dictionary where each key is a prediction label value with no corresponding ground truth
            (subset of false positives). The value is a dictionary containing either a `count` or a list of
            `examples`. Each example includes the datum UID, predicted bounding box, and prediction score.
        unmatched_ground_truths : dict
            A dictionary where each key is a ground truth label value for which the model failed to predict
            (subset of false negatives). The value is a dictionary containing either a `count` or a list of `examples`.
            Each example includes the datum UID and ground truth bounding box.
        score_threshold : float
            The confidence score threshold used to filter predictions.
        iou_threshold : float
            The Intersection over Union (IOU) threshold used to determine true positives.
        maximum_number_of_examples : int
            The maximum number of examples per element.

        Returns
        -------
        Metric
        """
        return cls(
            type=MetricType.ConfusionMatrix.value,
            value={
                "confusion_matrix": confusion_matrix,
                "unmatched_predictions": unmatched_predictions,
                "unmatched_ground_truths": unmatched_ground_truths,
            },
            parameters={
                "score_threshold": score_threshold,
                "iou_threshold": iou_threshold,
                "maximum_number_of_examples": maximum_number_of_examples,
            },
        )

accuracy(value, iou_threshold, score_threshold) classmethod

Accuracy metric for the object detection task type.

This class encapsulates a metric value at a specific Intersection over Union (IOU) threshold and confidence score threshold.

Parameters:

Name Type Description Default
value float

The metric value.

required
iou_threshold float

The IOU threshold used to determine matches between predicted and ground truth boxes.

required
score_threshold float

The confidence score threshold above which predictions are considered.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def accuracy(
    cls,
    value: float,
    iou_threshold: float,
    score_threshold: float,
):
    """
    Accuracy metric for the object detection task type.

    This class encapsulates a metric value at a specific Intersection
    over Union (IOU) threshold and confidence score threshold.

    Parameters
    ----------
    value : float
        The metric value.
    iou_threshold : float
        The IOU threshold used to determine matches between predicted and ground truth boxes.
    score_threshold : float
        The confidence score threshold above which predictions are considered.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.Accuracy.value,
        value=value,
        parameters={
            "iou_threshold": iou_threshold,
            "score_threshold": score_threshold,
        },
    )

average_precision(value, iou_threshold, label) classmethod

Average Precision (AP) metric for object detection tasks.

The AP computation uses 101-point interpolation, which calculates the average precision by interpolating the precision-recall curve at 101 evenly spaced recall levels from 0 to 1.

Parameters:

Name Type Description Default
value float

The average precision value.

required
iou_threshold float

The IOU threshold used to compute the AP.

required
label str

The class label for which the AP is computed.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def average_precision(
    cls,
    value: float,
    iou_threshold: float,
    label: str,
):
    """
    Average Precision (AP) metric for object detection tasks.

    The AP computation uses 101-point interpolation, which calculates the average
    precision by interpolating the precision-recall curve at 101 evenly spaced recall
    levels from 0 to 1.

    Parameters
    ----------
    value : float
        The average precision value.
    iou_threshold : float
        The IOU threshold used to compute the AP.
    label : str
        The class label for which the AP is computed.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.AP.value,
        value=value,
        parameters={
            "iou_threshold": iou_threshold,
            "label": label,
        },
    )

average_precision_averaged_over_IOUs(value, iou_thresholds, label) classmethod

Average Precision (AP) metric averaged over multiple IOU thresholds.

The AP computation uses 101-point interpolation, which calculates the average precision by interpolating the precision-recall curve at 101 evenly spaced recall levels from 0 to 1 for each IOU threshold specified in iou_thresholds. The final APAveragedOverIOUs value is obtained by averaging these AP values across all specified IOU thresholds.

Parameters:

Name Type Description Default
value float

The average precision value averaged over the specified IOU thresholds.

required
iou_thresholds list[float]

The list of IOU thresholds used to compute the AP values.

required
label str

The class label for which the AP is computed.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def average_precision_averaged_over_IOUs(
    cls,
    value: float,
    iou_thresholds: list[float],
    label: str,
):
    """
    Average Precision (AP) metric averaged over multiple IOU thresholds.

    The AP computation uses 101-point interpolation, which calculates the average precision
    by interpolating the precision-recall curve at 101 evenly spaced recall levels from 0 to 1
    for each IOU threshold specified in `iou_thresholds`. The final APAveragedOverIOUs value is
    obtained by averaging these AP values across all specified IOU thresholds.

    Parameters
    ----------
    value : float
        The average precision value averaged over the specified IOU thresholds.
    iou_thresholds : list[float]
        The list of IOU thresholds used to compute the AP values.
    label : str
        The class label for which the AP is computed.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.APAveragedOverIOUs.value,
        value=value,
        parameters={
            "iou_thresholds": iou_thresholds,
            "label": label,
        },
    )

average_recall(value, score_threshold, iou_thresholds, label) classmethod

Average Recall (AR) metric for object detection tasks.

The AR computation considers detections with confidence scores above the specified score_threshold and calculates the recall at each IOU threshold in iou_thresholds. The final AR value is the average of these recall values across all specified IOU thresholds.

Parameters:

Name Type Description Default
value float

The average recall value averaged over the specified IOU thresholds.

required
score_threshold float

The detection score threshold; only detections with confidence scores above this threshold are considered.

required
iou_thresholds list[float]

The list of IOU thresholds used to compute the recall values.

required
label str

The class label for which the AR is computed.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def average_recall(
    cls,
    value: float,
    score_threshold: float,
    iou_thresholds: list[float],
    label: str,
):
    """
    Average Recall (AR) metric for object detection tasks.

    The AR computation considers detections with confidence scores above the specified
    `score_threshold` and calculates the recall at each IOU threshold in `iou_thresholds`.
    The final AR value is the average of these recall values across all specified IOU
    thresholds.

    Parameters
    ----------
    value : float
        The average recall value averaged over the specified IOU thresholds.
    score_threshold : float
        The detection score threshold; only detections with confidence scores above this
        threshold are considered.
    iou_thresholds : list[float]
        The list of IOU thresholds used to compute the recall values.
    label : str
        The class label for which the AR is computed.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.AR.value,
        value=value,
        parameters={
            "iou_thresholds": iou_thresholds,
            "score_threshold": score_threshold,
            "label": label,
        },
    )

average_recall_averaged_over_scores(value, score_thresholds, iou_thresholds, label) classmethod

Average Recall (AR) metric averaged over multiple score thresholds for a specific object class label.

The AR computation considers detections across multiple score_thresholds and calculates recall at each IOU threshold in iou_thresholds. The final AR value is obtained by averaging the recall values over all specified score thresholds and IOU thresholds.

Parameters:

Name Type Description Default
value float

The average recall value averaged over the specified score thresholds and IOU thresholds.

required
score_thresholds list[float]

The list of detection score thresholds; detections with confidence scores above each threshold are considered.

required
iou_thresholds list[float]

The list of IOU thresholds used to compute the recall values.

required
label str

The class label for which the AR is computed.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def average_recall_averaged_over_scores(
    cls,
    value: float,
    score_thresholds: list[float],
    iou_thresholds: list[float],
    label: str,
):
    """
    Average Recall (AR) metric averaged over multiple score thresholds for a specific object class label.

    The AR computation considers detections across multiple `score_thresholds` and calculates
    recall at each IOU threshold in `iou_thresholds`. The final AR value is obtained by averaging
    the recall values over all specified score thresholds and IOU thresholds.

    Parameters
    ----------
    value : float
        The average recall value averaged over the specified score thresholds and IOU thresholds.
    score_thresholds : list[float]
        The list of detection score thresholds; detections with confidence scores above each threshold are considered.
    iou_thresholds : list[float]
        The list of IOU thresholds used to compute the recall values.
    label : str
        The class label for which the AR is computed.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.ARAveragedOverScores.value,
        value=value,
        parameters={
            "iou_thresholds": iou_thresholds,
            "score_thresholds": score_thresholds,
            "label": label,
        },
    )

confusion_matrix(confusion_matrix, unmatched_predictions, unmatched_ground_truths, score_threshold, iou_threshold, maximum_number_of_examples) classmethod

Confusion matrix for object detection tasks.

This class encapsulates detailed information about the model's performance, including correct predictions, misclassifications, unmatched_predictions (subset of false positives), and unmatched ground truths (subset of false negatives). It provides counts and examples for each category to facilitate in-depth analysis.

Confusion Matrix Format: { : { : { 'count': int, 'examples': [ { 'datum': str, 'groundtruth': dict, # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float} 'prediction': dict, # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float} 'score': float, }, ... ], }, ... }, ... }

Unmatched Predictions Format: { : { 'count': int, 'examples': [ { 'datum': str, 'prediction': dict, # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float} 'score': float, }, ... ], }, ... }

Unmatched Ground Truths Format: { : { 'count': int, 'examples': [ { 'datum': str, 'groundtruth': dict, # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float} }, ... ], }, ... }

Parameters:

Name Type Description Default
confusion_matrix dict

A nested dictionary where the first key is the ground truth label value, the second key is the prediction label value, and the innermost dictionary contains either a count or a list of examples. Each example includes the datum UID, ground truth bounding box, predicted bounding box, and prediction scores.

required
unmatched_predictions dict

A dictionary where each key is a prediction label value with no corresponding ground truth (subset of false positives). The value is a dictionary containing either a count or a list of examples. Each example includes the datum UID, predicted bounding box, and prediction score.

required
unmatched_ground_truths dict

A dictionary where each key is a ground truth label value for which the model failed to predict (subset of false negatives). The value is a dictionary containing either a count or a list of examples. Each example includes the datum UID and ground truth bounding box.

required
score_threshold float

The confidence score threshold used to filter predictions.

required
iou_threshold float

The Intersection over Union (IOU) threshold used to determine true positives.

required
maximum_number_of_examples int

The maximum number of examples per element.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def confusion_matrix(
    cls,
    confusion_matrix: dict[
        str,  # ground truth label value
        dict[
            str,  # prediction label value
            dict[
                str,  # either `count` or `examples`
                int
                | list[
                    dict[
                        str,  # either `datum`, `groundtruth`, `prediction` or score
                        str  # datum uid
                        | dict[
                            str, float
                        ]  # bounding box (xmin, xmax, ymin, ymax)
                        | float,  # prediction score
                    ]
                ],
            ],
        ],
    ],
    unmatched_predictions: dict[
        str,  # prediction label value
        dict[
            str,  # either `count` or `examples`
            int
            | list[
                dict[
                    str,  # either `datum`, `prediction` or score
                    str  # datum uid
                    | float  # prediction score
                    | dict[
                        str, float
                    ],  # bounding box (xmin, xmax, ymin, ymax)
                ]
            ],
        ],
    ],
    unmatched_ground_truths: dict[
        str,  # ground truth label value
        dict[
            str,  # either `count` or `examples`
            int
            | list[
                dict[
                    str,  # either `datum` or `groundtruth`
                    str  # datum uid
                    | dict[
                        str, float
                    ],  # bounding box (xmin, xmax, ymin, ymax)
                ]
            ],
        ],
    ],
    score_threshold: float,
    iou_threshold: float,
    maximum_number_of_examples: int,
):
    """
    Confusion matrix for object detection tasks.

    This class encapsulates detailed information about the model's performance, including correct
    predictions, misclassifications, unmatched_predictions (subset of false positives), and unmatched ground truths
    (subset of false negatives). It provides counts and examples for each category to facilitate in-depth analysis.

    Confusion Matrix Format:
    {
        <ground truth label>: {
            <prediction label>: {
                'count': int,
                'examples': [
                    {
                        'datum': str,
                        'groundtruth': dict,  # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float}
                        'prediction': dict,   # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float}
                        'score': float,
                    },
                    ...
                ],
            },
            ...
        },
        ...
    }

    Unmatched Predictions Format:
    {
        <prediction label>: {
            'count': int,
            'examples': [
                {
                    'datum': str,
                    'prediction': dict,  # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float}
                    'score': float,
                },
                ...
            ],
        },
        ...
    }

    Unmatched Ground Truths Format:
    {
        <ground truth label>: {
            'count': int,
            'examples': [
                {
                    'datum': str,
                    'groundtruth': dict,  # {'xmin': float, 'xmax': float, 'ymin': float, 'ymax': float}
                },
                ...
            ],
        },
        ...
    }

    Parameters
    ----------
    confusion_matrix : dict
        A nested dictionary where the first key is the ground truth label value, the second key
        is the prediction label value, and the innermost dictionary contains either a `count`
        or a list of `examples`. Each example includes the datum UID, ground truth bounding box,
        predicted bounding box, and prediction scores.
    unmatched_predictions : dict
        A dictionary where each key is a prediction label value with no corresponding ground truth
        (subset of false positives). The value is a dictionary containing either a `count` or a list of
        `examples`. Each example includes the datum UID, predicted bounding box, and prediction score.
    unmatched_ground_truths : dict
        A dictionary where each key is a ground truth label value for which the model failed to predict
        (subset of false negatives). The value is a dictionary containing either a `count` or a list of `examples`.
        Each example includes the datum UID and ground truth bounding box.
    score_threshold : float
        The confidence score threshold used to filter predictions.
    iou_threshold : float
        The Intersection over Union (IOU) threshold used to determine true positives.
    maximum_number_of_examples : int
        The maximum number of examples per element.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.ConfusionMatrix.value,
        value={
            "confusion_matrix": confusion_matrix,
            "unmatched_predictions": unmatched_predictions,
            "unmatched_ground_truths": unmatched_ground_truths,
        },
        parameters={
            "score_threshold": score_threshold,
            "iou_threshold": iou_threshold,
            "maximum_number_of_examples": maximum_number_of_examples,
        },
    )

counts(tp, fp, fn, label, iou_threshold, score_threshold) classmethod

Counts encapsulates the counts of true positives (tp), false positives (fp), and false negatives (fn) for object detection evaluation, along with the associated class label, Intersection over Union (IOU) threshold, and confidence score threshold.

Parameters:

Name Type Description Default
tp int

Number of true positives.

required
fp int

Number of false positives.

required
fn int

Number of false negatives.

required
label str

The class label for which the counts are calculated.

required
iou_threshold float

The IOU threshold used to determine a match between predicted and ground truth boxes.

required
score_threshold float

The confidence score threshold above which predictions are considered.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def counts(
    cls,
    tp: int,
    fp: int,
    fn: int,
    label: str,
    iou_threshold: float,
    score_threshold: float,
):
    """
    `Counts` encapsulates the counts of true positives (`tp`), false positives (`fp`),
    and false negatives (`fn`) for object detection evaluation, along with the associated
    class label, Intersection over Union (IOU) threshold, and confidence score threshold.

    Parameters
    ----------
    tp : int
        Number of true positives.
    fp : int
        Number of false positives.
    fn : int
        Number of false negatives.
    label : str
        The class label for which the counts are calculated.
    iou_threshold : float
        The IOU threshold used to determine a match between predicted and ground truth boxes.
    score_threshold : float
        The confidence score threshold above which predictions are considered.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.Counts.value,
        value={
            "tp": tp,
            "fp": fp,
            "fn": fn,
        },
        parameters={
            "iou_threshold": iou_threshold,
            "score_threshold": score_threshold,
            "label": label,
        },
    )

f1_score(value, label, iou_threshold, score_threshold) classmethod

F1 score for a specific class label in object detection.

This class encapsulates a metric value for a particular class label, along with the associated Intersection over Union (IOU) threshold and confidence score threshold.

Parameters:

Name Type Description Default
value float

The metric value.

required
label str

The class label for which the metric is calculated.

required
iou_threshold float

The IOU threshold used to determine matches between predicted and ground truth boxes.

required
score_threshold float

The confidence score threshold above which predictions are considered.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def f1_score(
    cls,
    value: float,
    label: str,
    iou_threshold: float,
    score_threshold: float,
):
    """
    F1 score for a specific class label in object detection.

    This class encapsulates a metric value for a particular class label,
    along with the associated Intersection over Union (IOU) threshold and
    confidence score threshold.

    Parameters
    ----------
    value : float
        The metric value.
    label : str
        The class label for which the metric is calculated.
    iou_threshold : float
        The IOU threshold used to determine matches between predicted and ground truth boxes.
    score_threshold : float
        The confidence score threshold above which predictions are considered.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.F1.value,
        value=value,
        parameters={
            "label": label,
            "iou_threshold": iou_threshold,
            "score_threshold": score_threshold,
        },
    )

mean_average_precision(value, iou_threshold) classmethod

Mean Average Precision (mAP) metric for object detection tasks.

The AP computation uses 101-point interpolation, which calculates the average precision for each class by interpolating the precision-recall curve at 101 evenly spaced recall levels from 0 to 1. The mAP is then calculated by averaging these values across all class labels.

Parameters:

Name Type Description Default
value float

The mean average precision value.

required
iou_threshold float

The IOU threshold used to compute the mAP.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def mean_average_precision(
    cls,
    value: float,
    iou_threshold: float,
):
    """
    Mean Average Precision (mAP) metric for object detection tasks.

    The AP computation uses 101-point interpolation, which calculates the average
    precision for each class by interpolating the precision-recall curve at 101 evenly
    spaced recall levels from 0 to 1. The mAP is then calculated by averaging these
    values across all class labels.

    Parameters
    ----------
    value : float
        The mean average precision value.
    iou_threshold : float
        The IOU threshold used to compute the mAP.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.mAP.value,
        value=value,
        parameters={
            "iou_threshold": iou_threshold,
        },
    )

mean_average_precision_averaged_over_IOUs(value, iou_thresholds) classmethod

Mean Average Precision (mAP) metric averaged over multiple IOU thresholds.

The AP computation uses 101-point interpolation, which calculates the average precision by interpolating the precision-recall curve at 101 evenly spaced recall levels from 0 to 1 for each IOU threshold specified in iou_thresholds. The final mAPAveragedOverIOUs value is obtained by averaging these AP values across all specified IOU thresholds and all class labels.

Parameters:

Name Type Description Default
value float

The average precision value averaged over the specified IOU thresholds.

required
iou_thresholds list[float]

The list of IOU thresholds used to compute the AP values.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def mean_average_precision_averaged_over_IOUs(
    cls,
    value: float,
    iou_thresholds: list[float],
):
    """
    Mean Average Precision (mAP) metric averaged over multiple IOU thresholds.

    The AP computation uses 101-point interpolation, which calculates the average precision
    by interpolating the precision-recall curve at 101 evenly spaced recall levels from 0 to 1
    for each IOU threshold specified in `iou_thresholds`. The final mAPAveragedOverIOUs value is
    obtained by averaging these AP values across all specified IOU thresholds and all class labels.

    Parameters
    ----------
    value : float
        The average precision value averaged over the specified IOU thresholds.
    iou_thresholds : list[float]
        The list of IOU thresholds used to compute the AP values.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.mAPAveragedOverIOUs.value,
        value=value,
        parameters={
            "iou_thresholds": iou_thresholds,
        },
    )

mean_average_recall(value, score_threshold, iou_thresholds) classmethod

Mean Average Recall (mAR) metric for object detection tasks.

The mAR computation considers detections with confidence scores above the specified score_threshold and calculates recall at each IOU threshold in iou_thresholds for each label. The final mAR value is obtained by averaging these recall values over the specified IOU thresholds and then averaging across all labels.

Parameters:

Name Type Description Default
value float

The mean average recall value averaged over the specified IOU thresholds.

required
score_threshold float

The detection score threshold; only detections with confidence scores above this threshold are considered.

required
iou_thresholds list[float]

The list of IOU thresholds used to compute the recall values.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def mean_average_recall(
    cls,
    value: float,
    score_threshold: float,
    iou_thresholds: list[float],
):
    """
    Mean Average Recall (mAR) metric for object detection tasks.

    The mAR computation considers detections with confidence scores above the specified
    `score_threshold` and calculates recall at each IOU threshold in `iou_thresholds` for
    each label. The final mAR value is obtained by averaging these recall values over the
    specified IOU thresholds and then averaging across all labels.

    Parameters
    ----------
    value : float
        The mean average recall value averaged over the specified IOU thresholds.
    score_threshold : float
        The detection score threshold; only detections with confidence scores above this
        threshold are considered.
    iou_thresholds : list[float]
        The list of IOU thresholds used to compute the recall values.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.mAR.value,
        value=value,
        parameters={
            "iou_thresholds": iou_thresholds,
            "score_threshold": score_threshold,
        },
    )

mean_average_recall_averaged_over_scores(value, score_thresholds, iou_thresholds) classmethod

Mean Average Recall (mAR) metric averaged over multiple score thresholds and IOU thresholds.

The mAR computation considers detections across multiple score_thresholds, calculates recall at each IOU threshold in iou_thresholds for each label, averages these recall values over all specified score thresholds and IOU thresholds, and then computes the mean across all labels to obtain the final mAR value.

Parameters:

Name Type Description Default
value float

The mean average recall value averaged over the specified score thresholds and IOU thresholds.

required
score_thresholds list[float]

The list of detection score thresholds; detections with confidence scores above each threshold are considered.

required
iou_thresholds list[float]

The list of IOU thresholds used to compute the recall values.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def mean_average_recall_averaged_over_scores(
    cls,
    value: float,
    score_thresholds: list[float],
    iou_thresholds: list[float],
):
    """
    Mean Average Recall (mAR) metric averaged over multiple score thresholds and IOU thresholds.

    The mAR computation considers detections across multiple `score_thresholds`, calculates recall
    at each IOU threshold in `iou_thresholds` for each label, averages these recall values over all
    specified score thresholds and IOU thresholds, and then computes the mean across all labels to
    obtain the final mAR value.

    Parameters
    ----------
    value : float
        The mean average recall value averaged over the specified score thresholds and IOU thresholds.
    score_thresholds : list[float]
        The list of detection score thresholds; detections with confidence scores above each threshold are considered.
    iou_thresholds : list[float]
        The list of IOU thresholds used to compute the recall values.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.mARAveragedOverScores.value,
        value=value,
        parameters={
            "iou_thresholds": iou_thresholds,
            "score_thresholds": score_thresholds,
        },
    )

precision(value, label, iou_threshold, score_threshold) classmethod

Precision metric for a specific class label in object detection.

This class encapsulates a metric value for a particular class label, along with the associated Intersection over Union (IOU) threshold and confidence score threshold.

Parameters:

Name Type Description Default
value float

The metric value.

required
label str

The class label for which the metric is calculated.

required
iou_threshold float

The IOU threshold used to determine matches between predicted and ground truth boxes.

required
score_threshold float

The confidence score threshold above which predictions are considered.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def precision(
    cls,
    value: float,
    label: str,
    iou_threshold: float,
    score_threshold: float,
):
    """
    Precision metric for a specific class label in object detection.

    This class encapsulates a metric value for a particular class label,
    along with the associated Intersection over Union (IOU) threshold and
    confidence score threshold.

    Parameters
    ----------
    value : float
        The metric value.
    label : str
        The class label for which the metric is calculated.
    iou_threshold : float
        The IOU threshold used to determine matches between predicted and ground truth boxes.
    score_threshold : float
        The confidence score threshold above which predictions are considered.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.Precision.value,
        value=value,
        parameters={
            "label": label,
            "iou_threshold": iou_threshold,
            "score_threshold": score_threshold,
        },
    )

precision_recall_curve(precisions, scores, iou_threshold, label) classmethod

Interpolated precision-recall curve over 101 recall points.

The precision values are interpolated over recalls ranging from 0.0 to 1.0 in steps of 0.01, resulting in 101 points. This is a byproduct of the 101-point interpolation used in calculating the Average Precision (AP) metric in object detection tasks.

Parameters:

Name Type Description Default
precisions list[float]

Interpolated precision values corresponding to recalls at 0.0, 0.01, ..., 1.0.

required
scores list[float]

Maximum prediction score for each point on the interpolated curve.

required
iou_threshold float

The Intersection over Union (IOU) threshold used to determine true positives.

required
label str

The class label associated with this precision-recall curve.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def precision_recall_curve(
    cls,
    precisions: list[float],
    scores: list[float],
    iou_threshold: float,
    label: str,
):
    """
    Interpolated precision-recall curve over 101 recall points.

    The precision values are interpolated over recalls ranging from 0.0 to 1.0 in steps of 0.01,
    resulting in 101 points. This is a byproduct of the 101-point interpolation used in calculating
    the Average Precision (AP) metric in object detection tasks.

    Parameters
    ----------
    precisions : list[float]
        Interpolated precision values corresponding to recalls at 0.0, 0.01, ..., 1.0.
    scores : list[float]
        Maximum prediction score for each point on the interpolated curve.
    iou_threshold : float
        The Intersection over Union (IOU) threshold used to determine true positives.
    label : str
        The class label associated with this precision-recall curve.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.PrecisionRecallCurve.value,
        value={
            "precisions": precisions,
            "scores": scores,
        },
        parameters={
            "iou_threshold": iou_threshold,
            "label": label,
        },
    )

recall(value, label, iou_threshold, score_threshold) classmethod

Recall metric for a specific class label in object detection.

This class encapsulates a metric value for a particular class label, along with the associated Intersection over Union (IOU) threshold and confidence score threshold.

Parameters:

Name Type Description Default
value float

The metric value.

required
label str

The class label for which the metric is calculated.

required
iou_threshold float

The IOU threshold used to determine matches between predicted and ground truth boxes.

required
score_threshold float

The confidence score threshold above which predictions are considered.

required

Returns:

Type Description
Metric
Source code in valor_lite/object_detection/metric.py
@classmethod
def recall(
    cls,
    value: float,
    label: str,
    iou_threshold: float,
    score_threshold: float,
):
    """
    Recall metric for a specific class label in object detection.

    This class encapsulates a metric value for a particular class label,
    along with the associated Intersection over Union (IOU) threshold and
    confidence score threshold.

    Parameters
    ----------
    value : float
        The metric value.
    label : str
        The class label for which the metric is calculated.
    iou_threshold : float
        The IOU threshold used to determine matches between predicted and ground truth boxes.
    score_threshold : float
        The confidence score threshold above which predictions are considered.

    Returns
    -------
    Metric
    """
    return cls(
        type=MetricType.Recall.value,
        value=value,
        parameters={
            "label": label,
            "iou_threshold": iou_threshold,
            "score_threshold": score_threshold,
        },
    )

References