Skip to content

Legacy Metrics

Starting with 0.23.0, a new metrics module is being introduced to supervision. Metrics here are part of the legacy evaluation API and will be deprecated in the future.

Install the metrics extra before using this page's APIs:

pip install "supervision[metrics]"

supervision.metrics.detection.ConfusionMatrix dataclass

Confusion matrix for object detection tasks.

Attributes:

Name Type Description
matrix NDArray[int32]

An 2D np.ndarray of shape (len(classes) + 1, len(classes) + 1) containing the number of TP, FP, FN and TN for each class.

classes list[str]

Model class names.

conf_threshold float

Detection confidence threshold between 0 and 1. Detections with lower confidence will be excluded from the matrix.

iou_threshold float

Detection IoU threshold between 0 and 1. Detections with lower IoU will be classified as FP.

metric_target MetricTarget

The type of detection data used for IoU computation. Informational metadata set by from_detections and from_tensors. Excluded from __eq__ comparisons — two ConfusionMatrix instances with identical matrix, classes, conf_threshold, and iou_threshold compare as equal regardless of metric_target.

Source code in src/supervision/metrics/detection.py
 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
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
@dataclass
class ConfusionMatrix:
    """
    Confusion matrix for object detection tasks.

    Attributes:
        matrix: An 2D `np.ndarray` of shape `(len(classes) + 1, len(classes) + 1)`
            containing the number of `TP`, `FP`, `FN` and `TN` for each class.
        classes: Model class names.
        conf_threshold: Detection confidence threshold between `0` and `1`.
            Detections with lower confidence will be excluded from the matrix.
        iou_threshold: Detection IoU threshold between `0` and `1`.
            Detections with lower IoU will be classified as `FP`.
        metric_target: The type of detection data used for IoU computation.
            Informational metadata set by `from_detections` and `from_tensors`.
            Excluded from `__eq__` comparisons — two `ConfusionMatrix` instances
            with identical `matrix`, `classes`, `conf_threshold`, and
            `iou_threshold` compare as equal regardless of `metric_target`.
    """

    matrix: npt.NDArray[np.int32]
    classes: list[str]
    conf_threshold: float
    iou_threshold: float
    metric_target: MetricTarget = MetricTarget.BOXES

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, ConfusionMatrix):
            return NotImplemented
        return (
            np.array_equal(self.matrix, other.matrix)
            and self.classes == other.classes
            and self.conf_threshold == other.conf_threshold
            and self.iou_threshold == other.iou_threshold
        )

    __hash__ = None  # type: ignore[assignment]

    @classmethod
    def from_detections(
        cls,
        predictions: list[Detections],
        targets: list[Detections],
        classes: list[str],
        conf_threshold: float = 0.3,
        iou_threshold: float = 0.5,
        metric_target: MetricTarget = MetricTarget.BOXES,
    ) -> ConfusionMatrix:
        """
        Calculate confusion matrix based on predicted and ground-truth detections.

        Args:
            targets: Detections objects from ground-truth.
            predictions: Detections objects predicted by the model.
            classes: Model class names.
            conf_threshold: Detection confidence threshold between `0` and `1`.
                Detections with lower confidence will be excluded.
            iou_threshold: Detection IoU threshold between `0` and `1`.
                Detections with lower IoU will be classified as `FP`.
            metric_target: The type of detection data to use.
                Supports `MetricTarget.BOXES` (default) and
                `MetricTarget.ORIENTED_BOUNDING_BOXES`. When using
                `MetricTarget.ORIENTED_BOUNDING_BOXES`, each `Detections`
                object must include OBB coordinates in
                `detections.data[ORIENTED_BOX_COORDINATES]` as a float32
                array of shape `(N, 8)` (flat) or `(N, 4, 2)` (as stored by
                `from_ultralytics`); both are normalised to `(N, 8)` internally.
                `MetricTarget.MASKS` is not supported.

        Returns:
            New instance of ConfusionMatrix.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> import supervision as sv
            >>> targets = [
            ...     sv.Detections(
            ...         xyxy=np.array([[0, 0, 10, 10], [50, 50, 60, 60]]),
            ...         class_id=np.array([0, 0])
            ...     )
            ... ]
            >>> predictions = [
            ...     sv.Detections(
            ...         xyxy=np.array([[0, 0, 10, 10], [100, 100, 110, 110]]),
            ...         class_id=np.array([0, 0]),
            ...         confidence=np.array([0.9, 0.8])
            ...     )
            ... ]
            >>> confusion_matrix = sv.ConfusionMatrix.from_detections(
            ...     predictions=predictions,
            ...     targets=targets,
            ...     classes=['person']
            ... )
            >>> confusion_matrix.matrix
            array([[1, 1],
                   [1, 0]], dtype=int32)

            ```
        """
        prediction_tensors = []
        target_tensors = []
        for prediction, target in zip(predictions, targets):
            prediction_tensors.append(
                detections_to_tensor(
                    prediction, with_confidence=True, metric_target=metric_target
                )
            )
            target_tensors.append(
                detections_to_tensor(
                    target, with_confidence=False, metric_target=metric_target
                )
            )
        return cls.from_tensors(
            predictions=prediction_tensors,
            targets=target_tensors,
            classes=classes,
            conf_threshold=conf_threshold,
            iou_threshold=iou_threshold,
            metric_target=metric_target,
        )

    @classmethod
    def from_tensors(
        cls,
        predictions: list[npt.NDArray[np.float32]],
        targets: list[npt.NDArray[np.float32]],
        classes: list[str],
        conf_threshold: float = 0.3,
        iou_threshold: float = 0.5,
        metric_target: MetricTarget = MetricTarget.BOXES,
    ) -> ConfusionMatrix:
        """
        Calculate confusion matrix based on predicted and ground-truth detections.

        Args:
            predictions: Each element of the list describes a single
                image and has `shape = (M, 6)` or `shape = (M, 10)` depending on
                `metric_target`.
                If `MetricTarget.BOXES`, each row is in
                `(x_min, y_min, x_max, y_max, class, conf)` format.
                If `MetricTarget.ORIENTED_BOUNDING_BOXES`, each row is in
                `(x1, y1, x2, y2, x3, y3, x4, y4, class, conf)` format.
            targets: Each element of the list describes a single
                image and has `shape = (N, 5)` or `shape = (N, 9)` depending on
                `metric_target`.
                If `MetricTarget.BOXES`, each row is in
                `(x_min, y_min, x_max, y_max, class)` format.
                If `MetricTarget.ORIENTED_BOUNDING_BOXES`, each row is in
                `(x1, y1, x2, y2, x3, y3, x4, y4, class)` format.
            classes: Model class names.
            conf_threshold: Detection confidence threshold between `0` and `1`.
                Detections with lower confidence will be excluded.
            iou_threshold: Detection iou threshold between `0` and `1`.
                Detections with lower iou will be classified as `FP`.
            metric_target: The type of detection data to use.
                Determines expected tensor shapes (see Args above for column
                layouts). `MetricTarget.MASKS` is not supported.

        Returns:
            New instance of ConfusionMatrix.

        Examples:
            ```pycon
            >>> import supervision as sv
            >>> import numpy as np
            >>> targets = [
            ...     np.array([
            ...         [0.0, 0.0, 3.0, 3.0, 0],
            ...         [2.0, 2.0, 5.0, 5.0, 0],
            ...         [6.0, 1.0, 8.0, 3.0, 1],
            ...     ])
            ... ]
            >>> predictions = [
            ...     np.array([
            ...         [0.0, 0.0, 3.0, 3.0, 0, 0.9],
            ...         [0.1, 0.1, 3.0, 3.0, 0, 0.9],
            ...         [6.0, 1.0, 8.0, 3.0, 1, 0.8],
            ...     ])
            ... ]
            >>> confusion_matrix = sv.ConfusionMatrix.from_tensors(
            ...     predictions=predictions,
            ...     targets=targets,
            ...     classes=['person', 'dog']
            ... )
            >>> confusion_matrix.matrix
            array([[1, 0, 1],
                   [0, 1, 0],
                   [1, 0, 0]], dtype=int32)

            ```
        """
        _assert_supported_target(metric_target)
        _validate_input_tensors(predictions, targets, metric_target=metric_target)

        num_classes = len(classes)
        matrix: npt.NDArray[np.int32] = np.zeros(
            (num_classes + 1, num_classes + 1), dtype=np.int32
        )
        for true_batch, detection_batch in zip(targets, predictions):
            matrix += cls.evaluate_detection_batch(
                predictions=detection_batch,
                targets=true_batch,
                num_classes=num_classes,
                conf_threshold=conf_threshold,
                iou_threshold=iou_threshold,
                metric_target=metric_target,
            )
        return cls(
            matrix=matrix,
            classes=classes,
            conf_threshold=conf_threshold,
            iou_threshold=iou_threshold,
            metric_target=metric_target,
        )

    @staticmethod
    def evaluate_detection_batch(
        predictions: npt.NDArray[np.float32],
        targets: npt.NDArray[np.float32],
        num_classes: int,
        conf_threshold: float,
        iou_threshold: float,
        metric_target: MetricTarget = MetricTarget.BOXES,
    ) -> npt.NDArray[np.int32]:
        """
        Calculate confusion matrix for a batch of detections for a single image.

        Args:
            predictions: Batch prediction. Describes a single image and
                has `shape = (M, 6)` or `shape = (M, 10)` depending on
                `metric_target`.
                If `MetricTarget.BOXES`, each row is in
                `(x_min, y_min, x_max, y_max, class, conf)` format.
                If `MetricTarget.ORIENTED_BOUNDING_BOXES`, each row is in
                `(x1, y1, x2, y2, x3, y3, x4, y4, class, conf)` format.
            targets: Batch target labels. Describes a single image and
                has `shape = (N, 5)` or `shape = (N, 9)` depending on
                `metric_target`.
                If `MetricTarget.BOXES`, each row is in
                `(x_min, y_min, x_max, y_max, class)` format.
                If `MetricTarget.ORIENTED_BOUNDING_BOXES`, each row is in
                `(x1, y1, x2, y2, x3, y3, x4, y4, class)` format.
            num_classes: Number of classes.
            conf_threshold: Detection confidence threshold between `0` and `1`.
                Detections with lower confidence will be excluded.
            iou_threshold: Detection iou threshold between `0` and `1`.
                Detections with lower iou will be classified as `FP`.
            metric_target: The type of detection data to use.
                Determines IoU function (`box_iou_batch` vs
                `oriented_box_iou_batch`) and coordinate column count.
                `MetricTarget.MASKS` is not supported.

        Returns:
            Confusion matrix based on a single image.
        """
        _assert_supported_target(metric_target)

        expected_pred_cols = (
            10 if metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES else 6
        )
        expected_target_cols = (
            9 if metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES else 5
        )
        if predictions.ndim != 2 or predictions.shape[1] != expected_pred_cols:
            raise ValueError(
                f"Predictions must have shape (M, {expected_pred_cols}). "
                f"Got {predictions.shape} instead."
            )
        if targets.ndim != 2 or targets.shape[1] != expected_target_cols:
            raise ValueError(
                f"Targets must have shape (N, {expected_target_cols}). "
                f"Got {targets.shape} instead."
            )

        result_matrix: npt.NDArray[np.int32] = np.zeros(
            (num_classes + 1, num_classes + 1), dtype=np.int32
        )

        # Filter predictions by confidence threshold
        coords_dim = 8 if metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES else 4
        class_id_idx = coords_dim
        conf_idx = coords_dim + 1

        confidence = predictions[:, conf_idx]
        detection_batch_filtered = predictions[confidence >= conf_threshold]

        if len(detection_batch_filtered) == 0:
            true_classes = _validated_class_ids(
                targets[:, class_id_idx], num_classes, "Target"
            )
            for gt_class in true_classes:
                result_matrix[gt_class, num_classes] += 1
            return result_matrix

        if len(targets) == 0:
            detection_classes = _validated_class_ids(
                detection_batch_filtered[:, class_id_idx], num_classes, "Prediction"
            )
            for det_class in detection_classes:
                result_matrix[num_classes, det_class] += 1
            return result_matrix

        true_classes = _validated_class_ids(
            targets[:, class_id_idx], num_classes, "Target"
        )
        detection_classes = _validated_class_ids(
            detection_batch_filtered[:, class_id_idx], num_classes, "Prediction"
        )
        true_boxes = targets[:, :coords_dim]
        detection_boxes = detection_batch_filtered[:, :coords_dim]

        # Calculate IoU matrix
        if metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES:
            iou_batch = oriented_box_iou_batch(
                boxes_true=true_boxes, boxes_detection=detection_boxes
            )
        else:
            iou_batch = box_iou_batch(
                boxes_true=true_boxes, boxes_detection=detection_boxes
            )

        # Find all valid matches (IoU > threshold, regardless of class)
        # Use vectorized operations to avoid nested Python loops
        iou_mask = iou_batch > iou_threshold
        gt_indices, det_indices = np.nonzero(iou_mask)

        # If no pairs exceed the IoU threshold, skip matching
        if gt_indices.size == 0:
            valid_matches = []
        else:
            ious = iou_batch[gt_indices, det_indices]
            gt_match_classes = true_classes[gt_indices]
            det_match_classes = detection_classes[det_indices]
            class_matches = gt_match_classes == det_match_classes

            # Sort matches by class match first (True before False),
            # then by IoU descending.
            # np.lexsort sorts by the last key first, in ascending order.
            # We use ~class_matches so that True becomes 0
            # and False becomes 1 (True first),
            # and -ious so that larger IoUs come first.
            sort_indices = np.lexsort((-ious, ~class_matches))

            # Build list of matches in the same format as before:
            # (gt_idx, det_idx, iou, class_match)
            valid_matches = [
                (
                    int(gt_indices[idx]),
                    int(det_indices[idx]),
                    float(ious[idx]),
                    bool(class_matches[idx]),
                )
                for idx in sort_indices
            ]
        # Greedily assign matches, ensuring each GT
        # and detection is matched at most once
        matched_gt_idx = set()
        matched_det_idx = set()

        for gt_idx, det_idx, iou, class_match in valid_matches:
            if gt_idx not in matched_gt_idx and det_idx not in matched_det_idx:
                # Valid spatial match - record the class prediction
                gt_class = true_classes[gt_idx]
                det_class = detection_classes[det_idx]

                # This handles both correct classification (TP) and misclassification
                result_matrix[gt_class, det_class] += 1
                matched_gt_idx.add(gt_idx)
                matched_det_idx.add(det_idx)

        # Count unmatched ground truth as FN
        for gt_idx, gt_class in enumerate(true_classes):
            if gt_idx not in matched_gt_idx:
                result_matrix[gt_class, num_classes] += 1

        # Count unmatched detections as FP
        for det_idx, det_class in enumerate(detection_classes):
            if det_idx not in matched_det_idx:
                result_matrix[num_classes, det_class] += 1

        return result_matrix

    @staticmethod
    def _drop_extra_matches(
        matches: npt.NDArray[np.float32],
    ) -> npt.NDArray[np.float32]:
        """
        Deduplicate matches. If there are multiple matches for the same true or
        predicted box, only the one with the highest IoU is kept.
        """
        if matches.shape[0] > 0:
            matches = matches[matches[:, 2].argsort()[::-1]]
            matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
            matches = matches[matches[:, 2].argsort()[::-1]]
            matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
        result: npt.NDArray[np.float32] = matches
        return result

    @classmethod
    def benchmark(
        cls,
        dataset: DetectionDataset,
        callback: Callable[[npt.NDArray[np.uint8]], Detections],
        conf_threshold: float = 0.3,
        iou_threshold: float = 0.5,
        metric_target: MetricTarget = MetricTarget.BOXES,
        *,
        save_directory_path: str | Path | None = None,
    ) -> ConfusionMatrix:
        """
        Calculate confusion matrix from dataset and callback function.

        Args:
            dataset: Object detection dataset used for evaluation.
            callback: Function that takes an image as input and returns a
                Detections object.
            conf_threshold: Detection confidence threshold between `0` and `1`.
                Detections with lower confidence will be excluded.
            iou_threshold: Detection IoU threshold between `0` and `1`.
                Detections with lower IoU will be classified as `FP`.
            save_directory_path: Optional directory where per-image validation
                result grids are saved using the original image filenames. Images
                are written directly to this directory (no subdirectory is added).
                When ``None`` (default), no images are saved.
            metric_target: The type of detection data to use.
                Supports `MetricTarget.BOXES` and
                `MetricTarget.ORIENTED_BOUNDING_BOXES`. Passed through to
                `from_detections`. `MetricTarget.MASKS` is not supported.

        Returns:
            New instance of ConfusionMatrix.

        Example:
            ```python
            import supervision as sv
            from ultralytics import YOLO

            dataset = sv.DetectionDataset.from_yolo(...)

            model = YOLO(...)
            def callback(image: np.ndarray) -> sv.Detections:
                result = model(image)[0]
                return sv.Detections.from_ultralytics(result)

            confusion_matrix = sv.ConfusionMatrix.benchmark(
                dataset = dataset,
                callback = callback
            )

            print(confusion_matrix.matrix)
            # np.array([
            #     [0., 0., 0., 0.],
            #     [0., 1., 0., 1.],
            #     [0., 1., 1., 0.],
            #     [1., 1., 0., 0.]
            # ])
            ```
        """
        if save_directory_path is not None:
            save_directory = Path(save_directory_path)
            save_directory.mkdir(parents=True, exist_ok=True)

        predictions, targets = [], []
        for index, (image_name, image, annotation) in enumerate(dataset):
            predictions_batch = callback(image)
            predictions.append(predictions_batch)
            targets.append(annotation)

            if save_directory_path is not None:
                if isinstance(image_name, Path):
                    image_filename = image_name.name
                elif isinstance(image_name, str):
                    image_filename = Path(image_name).name
                else:
                    image_filename = f"image_{index:06d}.jpg"

                if Path(image_filename).suffix == "":
                    image_filename = f"{image_filename}.jpg"

                save_path = save_directory / image_filename
                if save_path.exists():
                    warnings.warn(
                        f"Validation image '{image_filename}' already exists at "
                        f"'{save_path}' and will be overwritten.",
                        UserWarning,
                        stacklevel=2,
                    )
                _save_detection_validation_visualization(
                    scene=image,
                    predictions=predictions_batch,
                    targets=annotation,
                    save_path=save_path,
                    conf_threshold=conf_threshold,
                    iou_threshold=iou_threshold,
                    class_names=dataset.classes,
                    metric_target=metric_target,
                )
        return cls.from_detections(
            predictions=predictions,
            targets=targets,
            classes=dataset.classes,
            conf_threshold=conf_threshold,
            iou_threshold=iou_threshold,
            metric_target=metric_target,
        )

    def plot(
        self,
        save_path: str | None = None,
        title: str | None = None,
        classes: list[str] | None = None,
        normalize: bool = False,
        fig_size: tuple[int, int] = (12, 10),
    ) -> Figure:
        """
        Create confusion matrix plot and save it at selected location.

        Args:
            save_path: Path to save the plot. If not provided,
                plot will be displayed.
            title: Title of the plot.
            classes: List of classes to be displayed on the plot.
                If not provided, all classes will be displayed.
            normalize: If True, normalize the confusion matrix.
            fig_size: Size of the plot.

        Returns:
            Confusion matrix plot.
        """
        from matplotlib import pyplot as plt

        # Cast to float so that the NaN masking below never hits an integer
        # matrix (assigning NaN into an int array raises ValueError).
        array = self.matrix.astype(np.float64)

        if normalize:
            eps = 1e-8
            array = array / (array.sum(0).reshape(1, -1) + eps)

        array[array < 0.005] = np.nan

        fig, ax = plt.subplots(figsize=fig_size, tight_layout=True, facecolor="white")

        class_names = classes if classes is not None else self.classes
        use_labels_for_ticks = class_names is not None and (0 < len(class_names) < 99)
        if use_labels_for_ticks:
            x_tick_labels = [*class_names, "FN"]
            y_tick_labels = [*class_names, "FP"]
            num_ticks = len(x_tick_labels)
        else:
            x_tick_labels = None
            y_tick_labels = None
            num_ticks = len(array)
        im = ax.imshow(array, cmap="Blues")

        cbar = ax.figure.colorbar(im, ax=ax)
        cbar.mappable.set_clim(vmin=0, vmax=float(np.nanmax(array)))

        if x_tick_labels is None:
            tick_interval = 2
        else:
            tick_interval = 1
        ax.set_xticks(np.arange(0, num_ticks, tick_interval), labels=x_tick_labels)
        ax.set_yticks(np.arange(0, num_ticks, tick_interval), labels=y_tick_labels)

        plt.setp(ax.get_xticklabels(), rotation=90, ha="right", rotation_mode="default")

        labelsize = 10 if num_ticks < 50 else 8
        ax.tick_params(axis="both", which="both", labelsize=labelsize)

        if num_ticks < 30:
            for i in range(array.shape[0]):
                for j in range(array.shape[1]):
                    n_preds = array[i, j]
                    if not np.isnan(n_preds):
                        ax.text(
                            j,
                            i,
                            f"{n_preds:.2f}" if normalize else f"{n_preds:.0f}",
                            ha="center",
                            va="center",
                            color="black"
                            if n_preds < 0.5 * np.nanmax(array)
                            else "white",
                        )

        if title:
            ax.set_title(title, fontsize=20)

        ax.set_xlabel("Predicted")
        ax.set_ylabel("True")
        ax.set_facecolor("white")
        if save_path:
            fig.savefig(
                save_path, dpi=250, facecolor=fig.get_facecolor(), transparent=True
            )
        return fig

Methods:

benchmark(dataset: DetectionDataset, callback: Callable[[npt.NDArray[np.uint8]], Detections], conf_threshold: float = 0.3, iou_threshold: float = 0.5, metric_target: MetricTarget = MetricTarget.BOXES, *, save_directory_path: str | Path | None = None) -> ConfusionMatrix classmethod

Calculate confusion matrix from dataset and callback function.

Parameters:

Name Type Description Default
dataset
DetectionDataset

Object detection dataset used for evaluation.

required
callback
Callable[[NDArray[uint8]], Detections]

Function that takes an image as input and returns a Detections object.

required
conf_threshold
float

Detection confidence threshold between 0 and 1. Detections with lower confidence will be excluded.

0.3
iou_threshold
float

Detection IoU threshold between 0 and 1. Detections with lower IoU will be classified as FP.

0.5
save_directory_path
str | Path | None

Optional directory where per-image validation result grids are saved using the original image filenames. Images are written directly to this directory (no subdirectory is added). When None (default), no images are saved.

None
metric_target
MetricTarget

The type of detection data to use. Supports MetricTarget.BOXES and MetricTarget.ORIENTED_BOUNDING_BOXES. Passed through to from_detections. MetricTarget.MASKS is not supported.

BOXES

Returns:

Type Description
ConfusionMatrix

New instance of ConfusionMatrix.

Example
import supervision as sv
from ultralytics import YOLO

dataset = sv.DetectionDataset.from_yolo(...)

model = YOLO(...)
def callback(image: np.ndarray) -> sv.Detections:
    result = model(image)[0]
    return sv.Detections.from_ultralytics(result)

confusion_matrix = sv.ConfusionMatrix.benchmark(
    dataset = dataset,
    callback = callback
)

print(confusion_matrix.matrix)
# np.array([
#     [0., 0., 0., 0.],
#     [0., 1., 0., 1.],
#     [0., 1., 1., 0.],
#     [1., 1., 0., 0.]
# ])
Source code in src/supervision/metrics/detection.py
@classmethod
def benchmark(
    cls,
    dataset: DetectionDataset,
    callback: Callable[[npt.NDArray[np.uint8]], Detections],
    conf_threshold: float = 0.3,
    iou_threshold: float = 0.5,
    metric_target: MetricTarget = MetricTarget.BOXES,
    *,
    save_directory_path: str | Path | None = None,
) -> ConfusionMatrix:
    """
    Calculate confusion matrix from dataset and callback function.

    Args:
        dataset: Object detection dataset used for evaluation.
        callback: Function that takes an image as input and returns a
            Detections object.
        conf_threshold: Detection confidence threshold between `0` and `1`.
            Detections with lower confidence will be excluded.
        iou_threshold: Detection IoU threshold between `0` and `1`.
            Detections with lower IoU will be classified as `FP`.
        save_directory_path: Optional directory where per-image validation
            result grids are saved using the original image filenames. Images
            are written directly to this directory (no subdirectory is added).
            When ``None`` (default), no images are saved.
        metric_target: The type of detection data to use.
            Supports `MetricTarget.BOXES` and
            `MetricTarget.ORIENTED_BOUNDING_BOXES`. Passed through to
            `from_detections`. `MetricTarget.MASKS` is not supported.

    Returns:
        New instance of ConfusionMatrix.

    Example:
        ```python
        import supervision as sv
        from ultralytics import YOLO

        dataset = sv.DetectionDataset.from_yolo(...)

        model = YOLO(...)
        def callback(image: np.ndarray) -> sv.Detections:
            result = model(image)[0]
            return sv.Detections.from_ultralytics(result)

        confusion_matrix = sv.ConfusionMatrix.benchmark(
            dataset = dataset,
            callback = callback
        )

        print(confusion_matrix.matrix)
        # np.array([
        #     [0., 0., 0., 0.],
        #     [0., 1., 0., 1.],
        #     [0., 1., 1., 0.],
        #     [1., 1., 0., 0.]
        # ])
        ```
    """
    if save_directory_path is not None:
        save_directory = Path(save_directory_path)
        save_directory.mkdir(parents=True, exist_ok=True)

    predictions, targets = [], []
    for index, (image_name, image, annotation) in enumerate(dataset):
        predictions_batch = callback(image)
        predictions.append(predictions_batch)
        targets.append(annotation)

        if save_directory_path is not None:
            if isinstance(image_name, Path):
                image_filename = image_name.name
            elif isinstance(image_name, str):
                image_filename = Path(image_name).name
            else:
                image_filename = f"image_{index:06d}.jpg"

            if Path(image_filename).suffix == "":
                image_filename = f"{image_filename}.jpg"

            save_path = save_directory / image_filename
            if save_path.exists():
                warnings.warn(
                    f"Validation image '{image_filename}' already exists at "
                    f"'{save_path}' and will be overwritten.",
                    UserWarning,
                    stacklevel=2,
                )
            _save_detection_validation_visualization(
                scene=image,
                predictions=predictions_batch,
                targets=annotation,
                save_path=save_path,
                conf_threshold=conf_threshold,
                iou_threshold=iou_threshold,
                class_names=dataset.classes,
                metric_target=metric_target,
            )
    return cls.from_detections(
        predictions=predictions,
        targets=targets,
        classes=dataset.classes,
        conf_threshold=conf_threshold,
        iou_threshold=iou_threshold,
        metric_target=metric_target,
    )

evaluate_detection_batch(predictions: npt.NDArray[np.float32], targets: npt.NDArray[np.float32], num_classes: int, conf_threshold: float, iou_threshold: float, metric_target: MetricTarget = MetricTarget.BOXES) -> npt.NDArray[np.int32] staticmethod

Calculate confusion matrix for a batch of detections for a single image.

Parameters:

Name Type Description Default
predictions
NDArray[float32]

Batch prediction. Describes a single image and has shape = (M, 6) or shape = (M, 10) depending on metric_target. If MetricTarget.BOXES, each row is in (x_min, y_min, x_max, y_max, class, conf) format. If MetricTarget.ORIENTED_BOUNDING_BOXES, each row is in (x1, y1, x2, y2, x3, y3, x4, y4, class, conf) format.

required
targets
NDArray[float32]

Batch target labels. Describes a single image and has shape = (N, 5) or shape = (N, 9) depending on metric_target. If MetricTarget.BOXES, each row is in (x_min, y_min, x_max, y_max, class) format. If MetricTarget.ORIENTED_BOUNDING_BOXES, each row is in (x1, y1, x2, y2, x3, y3, x4, y4, class) format.

required
num_classes
int

Number of classes.

required
conf_threshold
float

Detection confidence threshold between 0 and 1. Detections with lower confidence will be excluded.

required
iou_threshold
float

Detection iou threshold between 0 and 1. Detections with lower iou will be classified as FP.

required
metric_target
MetricTarget

The type of detection data to use. Determines IoU function (box_iou_batch vs oriented_box_iou_batch) and coordinate column count. MetricTarget.MASKS is not supported.

BOXES

Returns:

Type Description
NDArray[int32]

Confusion matrix based on a single image.

Source code in src/supervision/metrics/detection.py
@staticmethod
def evaluate_detection_batch(
    predictions: npt.NDArray[np.float32],
    targets: npt.NDArray[np.float32],
    num_classes: int,
    conf_threshold: float,
    iou_threshold: float,
    metric_target: MetricTarget = MetricTarget.BOXES,
) -> npt.NDArray[np.int32]:
    """
    Calculate confusion matrix for a batch of detections for a single image.

    Args:
        predictions: Batch prediction. Describes a single image and
            has `shape = (M, 6)` or `shape = (M, 10)` depending on
            `metric_target`.
            If `MetricTarget.BOXES`, each row is in
            `(x_min, y_min, x_max, y_max, class, conf)` format.
            If `MetricTarget.ORIENTED_BOUNDING_BOXES`, each row is in
            `(x1, y1, x2, y2, x3, y3, x4, y4, class, conf)` format.
        targets: Batch target labels. Describes a single image and
            has `shape = (N, 5)` or `shape = (N, 9)` depending on
            `metric_target`.
            If `MetricTarget.BOXES`, each row is in
            `(x_min, y_min, x_max, y_max, class)` format.
            If `MetricTarget.ORIENTED_BOUNDING_BOXES`, each row is in
            `(x1, y1, x2, y2, x3, y3, x4, y4, class)` format.
        num_classes: Number of classes.
        conf_threshold: Detection confidence threshold between `0` and `1`.
            Detections with lower confidence will be excluded.
        iou_threshold: Detection iou threshold between `0` and `1`.
            Detections with lower iou will be classified as `FP`.
        metric_target: The type of detection data to use.
            Determines IoU function (`box_iou_batch` vs
            `oriented_box_iou_batch`) and coordinate column count.
            `MetricTarget.MASKS` is not supported.

    Returns:
        Confusion matrix based on a single image.
    """
    _assert_supported_target(metric_target)

    expected_pred_cols = (
        10 if metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES else 6
    )
    expected_target_cols = (
        9 if metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES else 5
    )
    if predictions.ndim != 2 or predictions.shape[1] != expected_pred_cols:
        raise ValueError(
            f"Predictions must have shape (M, {expected_pred_cols}). "
            f"Got {predictions.shape} instead."
        )
    if targets.ndim != 2 or targets.shape[1] != expected_target_cols:
        raise ValueError(
            f"Targets must have shape (N, {expected_target_cols}). "
            f"Got {targets.shape} instead."
        )

    result_matrix: npt.NDArray[np.int32] = np.zeros(
        (num_classes + 1, num_classes + 1), dtype=np.int32
    )

    # Filter predictions by confidence threshold
    coords_dim = 8 if metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES else 4
    class_id_idx = coords_dim
    conf_idx = coords_dim + 1

    confidence = predictions[:, conf_idx]
    detection_batch_filtered = predictions[confidence >= conf_threshold]

    if len(detection_batch_filtered) == 0:
        true_classes = _validated_class_ids(
            targets[:, class_id_idx], num_classes, "Target"
        )
        for gt_class in true_classes:
            result_matrix[gt_class, num_classes] += 1
        return result_matrix

    if len(targets) == 0:
        detection_classes = _validated_class_ids(
            detection_batch_filtered[:, class_id_idx], num_classes, "Prediction"
        )
        for det_class in detection_classes:
            result_matrix[num_classes, det_class] += 1
        return result_matrix

    true_classes = _validated_class_ids(
        targets[:, class_id_idx], num_classes, "Target"
    )
    detection_classes = _validated_class_ids(
        detection_batch_filtered[:, class_id_idx], num_classes, "Prediction"
    )
    true_boxes = targets[:, :coords_dim]
    detection_boxes = detection_batch_filtered[:, :coords_dim]

    # Calculate IoU matrix
    if metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES:
        iou_batch = oriented_box_iou_batch(
            boxes_true=true_boxes, boxes_detection=detection_boxes
        )
    else:
        iou_batch = box_iou_batch(
            boxes_true=true_boxes, boxes_detection=detection_boxes
        )

    # Find all valid matches (IoU > threshold, regardless of class)
    # Use vectorized operations to avoid nested Python loops
    iou_mask = iou_batch > iou_threshold
    gt_indices, det_indices = np.nonzero(iou_mask)

    # If no pairs exceed the IoU threshold, skip matching
    if gt_indices.size == 0:
        valid_matches = []
    else:
        ious = iou_batch[gt_indices, det_indices]
        gt_match_classes = true_classes[gt_indices]
        det_match_classes = detection_classes[det_indices]
        class_matches = gt_match_classes == det_match_classes

        # Sort matches by class match first (True before False),
        # then by IoU descending.
        # np.lexsort sorts by the last key first, in ascending order.
        # We use ~class_matches so that True becomes 0
        # and False becomes 1 (True first),
        # and -ious so that larger IoUs come first.
        sort_indices = np.lexsort((-ious, ~class_matches))

        # Build list of matches in the same format as before:
        # (gt_idx, det_idx, iou, class_match)
        valid_matches = [
            (
                int(gt_indices[idx]),
                int(det_indices[idx]),
                float(ious[idx]),
                bool(class_matches[idx]),
            )
            for idx in sort_indices
        ]
    # Greedily assign matches, ensuring each GT
    # and detection is matched at most once
    matched_gt_idx = set()
    matched_det_idx = set()

    for gt_idx, det_idx, iou, class_match in valid_matches:
        if gt_idx not in matched_gt_idx and det_idx not in matched_det_idx:
            # Valid spatial match - record the class prediction
            gt_class = true_classes[gt_idx]
            det_class = detection_classes[det_idx]

            # This handles both correct classification (TP) and misclassification
            result_matrix[gt_class, det_class] += 1
            matched_gt_idx.add(gt_idx)
            matched_det_idx.add(det_idx)

    # Count unmatched ground truth as FN
    for gt_idx, gt_class in enumerate(true_classes):
        if gt_idx not in matched_gt_idx:
            result_matrix[gt_class, num_classes] += 1

    # Count unmatched detections as FP
    for det_idx, det_class in enumerate(detection_classes):
        if det_idx not in matched_det_idx:
            result_matrix[num_classes, det_class] += 1

    return result_matrix

from_detections(predictions: list[Detections], targets: list[Detections], classes: list[str], conf_threshold: float = 0.3, iou_threshold: float = 0.5, metric_target: MetricTarget = MetricTarget.BOXES) -> ConfusionMatrix classmethod

Calculate confusion matrix based on predicted and ground-truth detections.

Parameters:

Name Type Description Default
targets
list[Detections]

Detections objects from ground-truth.

required
predictions
list[Detections]

Detections objects predicted by the model.

required
classes
list[str]

Model class names.

required
conf_threshold
float

Detection confidence threshold between 0 and 1. Detections with lower confidence will be excluded.

0.3
iou_threshold
float

Detection IoU threshold between 0 and 1. Detections with lower IoU will be classified as FP.

0.5
metric_target
MetricTarget

The type of detection data to use. Supports MetricTarget.BOXES (default) and MetricTarget.ORIENTED_BOUNDING_BOXES. When using MetricTarget.ORIENTED_BOUNDING_BOXES, each Detections object must include OBB coordinates in detections.data[ORIENTED_BOX_COORDINATES] as a float32 array of shape (N, 8) (flat) or (N, 4, 2) (as stored by from_ultralytics); both are normalised to (N, 8) internally. MetricTarget.MASKS is not supported.

BOXES

Returns:

Type Description
ConfusionMatrix

New instance of ConfusionMatrix.

Examples:

>>> import numpy as np
>>> import supervision as sv
>>> targets = [
...     sv.Detections(
...         xyxy=np.array([[0, 0, 10, 10], [50, 50, 60, 60]]),
...         class_id=np.array([0, 0])
...     )
... ]
>>> predictions = [
...     sv.Detections(
...         xyxy=np.array([[0, 0, 10, 10], [100, 100, 110, 110]]),
...         class_id=np.array([0, 0]),
...         confidence=np.array([0.9, 0.8])
...     )
... ]
>>> confusion_matrix = sv.ConfusionMatrix.from_detections(
...     predictions=predictions,
...     targets=targets,
...     classes=['person']
... )
>>> confusion_matrix.matrix
array([[1, 1],
       [1, 0]], dtype=int32)
Source code in src/supervision/metrics/detection.py
@classmethod
def from_detections(
    cls,
    predictions: list[Detections],
    targets: list[Detections],
    classes: list[str],
    conf_threshold: float = 0.3,
    iou_threshold: float = 0.5,
    metric_target: MetricTarget = MetricTarget.BOXES,
) -> ConfusionMatrix:
    """
    Calculate confusion matrix based on predicted and ground-truth detections.

    Args:
        targets: Detections objects from ground-truth.
        predictions: Detections objects predicted by the model.
        classes: Model class names.
        conf_threshold: Detection confidence threshold between `0` and `1`.
            Detections with lower confidence will be excluded.
        iou_threshold: Detection IoU threshold between `0` and `1`.
            Detections with lower IoU will be classified as `FP`.
        metric_target: The type of detection data to use.
            Supports `MetricTarget.BOXES` (default) and
            `MetricTarget.ORIENTED_BOUNDING_BOXES`. When using
            `MetricTarget.ORIENTED_BOUNDING_BOXES`, each `Detections`
            object must include OBB coordinates in
            `detections.data[ORIENTED_BOX_COORDINATES]` as a float32
            array of shape `(N, 8)` (flat) or `(N, 4, 2)` (as stored by
            `from_ultralytics`); both are normalised to `(N, 8)` internally.
            `MetricTarget.MASKS` is not supported.

    Returns:
        New instance of ConfusionMatrix.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> targets = [
        ...     sv.Detections(
        ...         xyxy=np.array([[0, 0, 10, 10], [50, 50, 60, 60]]),
        ...         class_id=np.array([0, 0])
        ...     )
        ... ]
        >>> predictions = [
        ...     sv.Detections(
        ...         xyxy=np.array([[0, 0, 10, 10], [100, 100, 110, 110]]),
        ...         class_id=np.array([0, 0]),
        ...         confidence=np.array([0.9, 0.8])
        ...     )
        ... ]
        >>> confusion_matrix = sv.ConfusionMatrix.from_detections(
        ...     predictions=predictions,
        ...     targets=targets,
        ...     classes=['person']
        ... )
        >>> confusion_matrix.matrix
        array([[1, 1],
               [1, 0]], dtype=int32)

        ```
    """
    prediction_tensors = []
    target_tensors = []
    for prediction, target in zip(predictions, targets):
        prediction_tensors.append(
            detections_to_tensor(
                prediction, with_confidence=True, metric_target=metric_target
            )
        )
        target_tensors.append(
            detections_to_tensor(
                target, with_confidence=False, metric_target=metric_target
            )
        )
    return cls.from_tensors(
        predictions=prediction_tensors,
        targets=target_tensors,
        classes=classes,
        conf_threshold=conf_threshold,
        iou_threshold=iou_threshold,
        metric_target=metric_target,
    )

from_tensors(predictions: list[npt.NDArray[np.float32]], targets: list[npt.NDArray[np.float32]], classes: list[str], conf_threshold: float = 0.3, iou_threshold: float = 0.5, metric_target: MetricTarget = MetricTarget.BOXES) -> ConfusionMatrix classmethod

Calculate confusion matrix based on predicted and ground-truth detections.

Parameters:

Name Type Description Default
predictions
list[NDArray[float32]]

Each element of the list describes a single image and has shape = (M, 6) or shape = (M, 10) depending on metric_target. If MetricTarget.BOXES, each row is in (x_min, y_min, x_max, y_max, class, conf) format. If MetricTarget.ORIENTED_BOUNDING_BOXES, each row is in (x1, y1, x2, y2, x3, y3, x4, y4, class, conf) format.

required
targets
list[NDArray[float32]]

Each element of the list describes a single image and has shape = (N, 5) or shape = (N, 9) depending on metric_target. If MetricTarget.BOXES, each row is in (x_min, y_min, x_max, y_max, class) format. If MetricTarget.ORIENTED_BOUNDING_BOXES, each row is in (x1, y1, x2, y2, x3, y3, x4, y4, class) format.

required
classes
list[str]

Model class names.

required
conf_threshold
float

Detection confidence threshold between 0 and 1. Detections with lower confidence will be excluded.

0.3
iou_threshold
float

Detection iou threshold between 0 and 1. Detections with lower iou will be classified as FP.

0.5
metric_target
MetricTarget

The type of detection data to use. Determines expected tensor shapes (see Args above for column layouts). MetricTarget.MASKS is not supported.

BOXES

Returns:

Type Description
ConfusionMatrix

New instance of ConfusionMatrix.

Examples:

>>> import supervision as sv
>>> import numpy as np
>>> targets = [
...     np.array([
...         [0.0, 0.0, 3.0, 3.0, 0],
...         [2.0, 2.0, 5.0, 5.0, 0],
...         [6.0, 1.0, 8.0, 3.0, 1],
...     ])
... ]
>>> predictions = [
...     np.array([
...         [0.0, 0.0, 3.0, 3.0, 0, 0.9],
...         [0.1, 0.1, 3.0, 3.0, 0, 0.9],
...         [6.0, 1.0, 8.0, 3.0, 1, 0.8],
...     ])
... ]
>>> confusion_matrix = sv.ConfusionMatrix.from_tensors(
...     predictions=predictions,
...     targets=targets,
...     classes=['person', 'dog']
... )
>>> confusion_matrix.matrix
array([[1, 0, 1],
       [0, 1, 0],
       [1, 0, 0]], dtype=int32)
Source code in src/supervision/metrics/detection.py
@classmethod
def from_tensors(
    cls,
    predictions: list[npt.NDArray[np.float32]],
    targets: list[npt.NDArray[np.float32]],
    classes: list[str],
    conf_threshold: float = 0.3,
    iou_threshold: float = 0.5,
    metric_target: MetricTarget = MetricTarget.BOXES,
) -> ConfusionMatrix:
    """
    Calculate confusion matrix based on predicted and ground-truth detections.

    Args:
        predictions: Each element of the list describes a single
            image and has `shape = (M, 6)` or `shape = (M, 10)` depending on
            `metric_target`.
            If `MetricTarget.BOXES`, each row is in
            `(x_min, y_min, x_max, y_max, class, conf)` format.
            If `MetricTarget.ORIENTED_BOUNDING_BOXES`, each row is in
            `(x1, y1, x2, y2, x3, y3, x4, y4, class, conf)` format.
        targets: Each element of the list describes a single
            image and has `shape = (N, 5)` or `shape = (N, 9)` depending on
            `metric_target`.
            If `MetricTarget.BOXES`, each row is in
            `(x_min, y_min, x_max, y_max, class)` format.
            If `MetricTarget.ORIENTED_BOUNDING_BOXES`, each row is in
            `(x1, y1, x2, y2, x3, y3, x4, y4, class)` format.
        classes: Model class names.
        conf_threshold: Detection confidence threshold between `0` and `1`.
            Detections with lower confidence will be excluded.
        iou_threshold: Detection iou threshold between `0` and `1`.
            Detections with lower iou will be classified as `FP`.
        metric_target: The type of detection data to use.
            Determines expected tensor shapes (see Args above for column
            layouts). `MetricTarget.MASKS` is not supported.

    Returns:
        New instance of ConfusionMatrix.

    Examples:
        ```pycon
        >>> import supervision as sv
        >>> import numpy as np
        >>> targets = [
        ...     np.array([
        ...         [0.0, 0.0, 3.0, 3.0, 0],
        ...         [2.0, 2.0, 5.0, 5.0, 0],
        ...         [6.0, 1.0, 8.0, 3.0, 1],
        ...     ])
        ... ]
        >>> predictions = [
        ...     np.array([
        ...         [0.0, 0.0, 3.0, 3.0, 0, 0.9],
        ...         [0.1, 0.1, 3.0, 3.0, 0, 0.9],
        ...         [6.0, 1.0, 8.0, 3.0, 1, 0.8],
        ...     ])
        ... ]
        >>> confusion_matrix = sv.ConfusionMatrix.from_tensors(
        ...     predictions=predictions,
        ...     targets=targets,
        ...     classes=['person', 'dog']
        ... )
        >>> confusion_matrix.matrix
        array([[1, 0, 1],
               [0, 1, 0],
               [1, 0, 0]], dtype=int32)

        ```
    """
    _assert_supported_target(metric_target)
    _validate_input_tensors(predictions, targets, metric_target=metric_target)

    num_classes = len(classes)
    matrix: npt.NDArray[np.int32] = np.zeros(
        (num_classes + 1, num_classes + 1), dtype=np.int32
    )
    for true_batch, detection_batch in zip(targets, predictions):
        matrix += cls.evaluate_detection_batch(
            predictions=detection_batch,
            targets=true_batch,
            num_classes=num_classes,
            conf_threshold=conf_threshold,
            iou_threshold=iou_threshold,
            metric_target=metric_target,
        )
    return cls(
        matrix=matrix,
        classes=classes,
        conf_threshold=conf_threshold,
        iou_threshold=iou_threshold,
        metric_target=metric_target,
    )

plot(save_path: str | None = None, title: str | None = None, classes: list[str] | None = None, normalize: bool = False, fig_size: tuple[int, int] = (12, 10)) -> Figure

Create confusion matrix plot and save it at selected location.

Parameters:

Name Type Description Default
save_path
str | None

Path to save the plot. If not provided, plot will be displayed.

None
title
str | None

Title of the plot.

None
classes
list[str] | None

List of classes to be displayed on the plot. If not provided, all classes will be displayed.

None
normalize
bool

If True, normalize the confusion matrix.

False
fig_size
tuple[int, int]

Size of the plot.

(12, 10)

Returns:

Type Description
Figure

Confusion matrix plot.

Source code in src/supervision/metrics/detection.py
def plot(
    self,
    save_path: str | None = None,
    title: str | None = None,
    classes: list[str] | None = None,
    normalize: bool = False,
    fig_size: tuple[int, int] = (12, 10),
) -> Figure:
    """
    Create confusion matrix plot and save it at selected location.

    Args:
        save_path: Path to save the plot. If not provided,
            plot will be displayed.
        title: Title of the plot.
        classes: List of classes to be displayed on the plot.
            If not provided, all classes will be displayed.
        normalize: If True, normalize the confusion matrix.
        fig_size: Size of the plot.

    Returns:
        Confusion matrix plot.
    """
    from matplotlib import pyplot as plt

    # Cast to float so that the NaN masking below never hits an integer
    # matrix (assigning NaN into an int array raises ValueError).
    array = self.matrix.astype(np.float64)

    if normalize:
        eps = 1e-8
        array = array / (array.sum(0).reshape(1, -1) + eps)

    array[array < 0.005] = np.nan

    fig, ax = plt.subplots(figsize=fig_size, tight_layout=True, facecolor="white")

    class_names = classes if classes is not None else self.classes
    use_labels_for_ticks = class_names is not None and (0 < len(class_names) < 99)
    if use_labels_for_ticks:
        x_tick_labels = [*class_names, "FN"]
        y_tick_labels = [*class_names, "FP"]
        num_ticks = len(x_tick_labels)
    else:
        x_tick_labels = None
        y_tick_labels = None
        num_ticks = len(array)
    im = ax.imshow(array, cmap="Blues")

    cbar = ax.figure.colorbar(im, ax=ax)
    cbar.mappable.set_clim(vmin=0, vmax=float(np.nanmax(array)))

    if x_tick_labels is None:
        tick_interval = 2
    else:
        tick_interval = 1
    ax.set_xticks(np.arange(0, num_ticks, tick_interval), labels=x_tick_labels)
    ax.set_yticks(np.arange(0, num_ticks, tick_interval), labels=y_tick_labels)

    plt.setp(ax.get_xticklabels(), rotation=90, ha="right", rotation_mode="default")

    labelsize = 10 if num_ticks < 50 else 8
    ax.tick_params(axis="both", which="both", labelsize=labelsize)

    if num_ticks < 30:
        for i in range(array.shape[0]):
            for j in range(array.shape[1]):
                n_preds = array[i, j]
                if not np.isnan(n_preds):
                    ax.text(
                        j,
                        i,
                        f"{n_preds:.2f}" if normalize else f"{n_preds:.0f}",
                        ha="center",
                        va="center",
                        color="black"
                        if n_preds < 0.5 * np.nanmax(array)
                        else "white",
                    )

    if title:
        ax.set_title(title, fontsize=20)

    ax.set_xlabel("Predicted")
    ax.set_ylabel("True")
    ax.set_facecolor("white")
    if save_path:
        fig.savefig(
            save_path, dpi=250, facecolor=fig.get_facecolor(), transparent=True
        )
    return fig

supervision.metrics.detection.MeanAveragePrecision dataclass

Deprecated

MeanAveragePrecision is deprecated and will be removed in supervision-0.31.0.

The deprecated implementation provides results that are inconsistent with pycocotools. Please use supervision.metrics.mean_average_precision.MeanAveragePrecision instead, which matches the results of pycocotools and is now the recommended approach.

Mean Average Precision for object detection tasks.

Attributes:

Name Type Description
map50_95 float

Mean Average Precision (mAP) calculated over IoU thresholds ranging from 0.50 to 0.95 with a step size of 0.05.

map50 float

Mean Average Precision (mAP) calculated specifically at an IoU threshold of 0.50.

map75 float

Mean Average Precision (mAP) calculated specifically at an IoU threshold of 0.75.

per_class_ap50_95 NDArray[float64]

Average Precision (AP) values calculated over IoU thresholds ranging from 0.50 to 0.95 with a step size of 0.05, provided for each individual class.

Source code in src/supervision/metrics/detection.py
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
@deprecated_class(
    target=TargetMode.NOTIFY,
    deprecated_in="0.27.0",
    remove_in="0.31.0",
)
@dataclass(frozen=True)
class MeanAveragePrecision:
    """
    !!! deprecated "Deprecated"
        `MeanAveragePrecision` is **deprecated** and will be removed in
        `supervision-0.31.0`.

        The deprecated implementation provides results that are inconsistent with
        `pycocotools`. Please use
        `supervision.metrics.mean_average_precision.MeanAveragePrecision` instead,
        which matches the results of `pycocotools` and is now the recommended approach.

    Mean Average Precision for object detection tasks.

    Attributes:
        map50_95: Mean Average Precision (mAP) calculated over IoU thresholds
            ranging from `0.50` to `0.95` with a step size of `0.05`.
        map50: Mean Average Precision (mAP) calculated specifically at
            an IoU threshold of `0.50`.
        map75: Mean Average Precision (mAP) calculated specifically at
            an IoU threshold of `0.75`.
        per_class_ap50_95: Average Precision (AP) values calculated over
            IoU thresholds ranging from `0.50` to `0.95` with a step size of `0.05`,
            provided for each individual class.
    """

    map50_95: float
    map50: float
    map75: float
    per_class_ap50_95: npt.NDArray[np.float64]

    @classmethod
    def from_detections(
        cls,
        predictions: list[Detections],
        targets: list[Detections],
    ) -> MeanAveragePrecision:
        """
        Calculate mean average precision based on predicted and ground-truth detections.

        Args:
            targets: Detections objects from ground-truth.
            predictions: Detections objects predicted by the model.
        Returns:
            New instance of ConfusionMatrix.

        Examples:
            ```pycon
            >>> import numpy as np
            >>> import supervision as sv
            >>> targets = [
            ...     sv.Detections(
            ...         xyxy=np.array([[0, 0, 10, 10]]),
            ...         class_id=np.array([0])
            ...     )
            ... ]
            >>> predictions = [
            ...     sv.Detections(
            ...         xyxy=np.array([[0, 0, 10, 10]]),
            ...         class_id=np.array([0]),
            ...         confidence=np.array([0.9])
            ...     )
            ... ]
            >>> mAP = sv.MeanAveragePrecision.from_detections(
            ...     predictions=predictions,
            ...     targets=targets,
            ... )
            >>> round(float(mAP.map50), 2)
            1.0

            ```
        """
        prediction_tensors = []
        target_tensors = []
        for prediction, target in zip(predictions, targets):
            prediction_tensors.append(
                detections_to_tensor(prediction, with_confidence=True)
            )
            target_tensors.append(detections_to_tensor(target, with_confidence=False))
        return cls.from_tensors(
            predictions=prediction_tensors,
            targets=target_tensors,
        )

    @classmethod
    def benchmark(
        cls,
        dataset: DetectionDataset,
        callback: Callable[[npt.NDArray[np.uint8]], Detections],
    ) -> MeanAveragePrecision:
        """
        Calculate mean average precision from dataset and callback function.

        Args:
            dataset: Object detection dataset used for evaluation.
            callback: Function that takes
                an image as input and returns Detections object.
        Returns:
            New instance of MeanAveragePrecision.

        Example:
            ```python
            import supervision as sv
            from ultralytics import YOLO

            dataset = sv.DetectionDataset.from_yolo(...)

            model = YOLO(...)
            def callback(image: np.ndarray) -> sv.Detections:
                result = model(image)[0]
                return sv.Detections.from_ultralytics(result)

            mean_average_precision = sv.MeanAveragePrecision.benchmark(
                dataset = dataset,
                callback = callback
            )

            print(mean_average_precision.map50_95)
            # 0.433
            ```
        """
        predictions, targets = [], []
        for _, image, annotation in dataset:
            predictions_batch = callback(image)
            predictions.append(predictions_batch)
            targets.append(annotation)
        return cls.from_detections(
            predictions=predictions,
            targets=targets,
        )

    @classmethod
    def from_tensors(
        cls,
        predictions: list[npt.NDArray[np.float32]],
        targets: list[npt.NDArray[np.float32]],
    ) -> MeanAveragePrecision:
        """
        Calculate Mean Average Precision based on predicted and ground-truth
            detections at different threshold.

        Args:
            predictions: Each element of the list describes
                a single image and has `shape = (M, 6)` where `M` is
                the number of detected objects. Each row is expected to be
                in `(x_min, y_min, x_max, y_max, class, conf)` format.
            targets: Each element of the list describes a single
                image and has `shape = (N, 5)` where `N` is the
                number of ground-truth objects. Each row is expected to be in
                `(x_min, y_min, x_max, y_max, class)` format. An empty array
                (``N = 0``) represents a background image; all predictions on
                that image count as false positives and reduce AP accordingly.

        Returns:
            MeanAveragePrecision: New instance computed from the provided
                predictions and targets.

        Examples:
            ```pycon
            >>> import supervision as sv
            >>> import numpy as np
            >>> targets = [
            ...     np.array([
            ...         [0.0, 0.0, 3.0, 3.0, 0],
            ...         [2.0, 2.0, 5.0, 5.0, 0],
            ...         [6.0, 1.0, 8.0, 3.0, 1],
            ...     ])
            ... ]
            >>> predictions = [
            ...     np.array([
            ...         [0.0, 0.0, 3.0, 3.0, 0, 0.9],
            ...         [0.1, 0.1, 3.0, 3.0, 0, 0.9],
            ...         [6.0, 1.0, 8.0, 3.0, 1, 0.8],
            ...     ])
            ... ]
            >>> mAP = sv.MeanAveragePrecision.from_tensors(
            ...     predictions=predictions,
            ...     targets=targets,
            ... )
            >>> round(float(mAP.map50), 2)
            0.75

            >>> bg_pred = [np.array([[0., 0., 10., 10., 0, 0.9]], dtype=np.float32)]
            >>> bg_tgt = [np.zeros((0, 5), dtype=np.float32)]
            >>> mAP_bg = sv.MeanAveragePrecision.from_tensors(
            ...     predictions=bg_pred,
            ...     targets=bg_tgt,
            ... )
            >>> float(mAP_bg.map50)
            0.0

            ```
        """
        _validate_input_tensors(predictions, targets)
        iou_thresholds = np.linspace(0.5, 0.95, 10, dtype=np.float32)
        stats = []

        # Gather matching stats for predictions and targets
        for true_objs, predicted_objs in zip(targets, predictions):
            if predicted_objs.shape[0] == 0:
                if true_objs.shape[0]:
                    stats.append(
                        (
                            np.zeros((0, iou_thresholds.size), dtype=bool),
                            *np.zeros((2, 0)),
                            true_objs[:, 4],
                        )
                    )
                continue

            if true_objs.shape[0]:
                matches = cls._match_detection_batch(
                    predicted_objs, true_objs, iou_thresholds
                )
                stats.append(
                    (
                        matches,
                        predicted_objs[:, 5],
                        predicted_objs[:, 4],
                        true_objs[:, 4],
                    )
                )
            else:
                # Background image: no GT boxes, so all predictions are FP matches.
                # This lowers AP for classes that appear in at least one GT image
                # elsewhere; classes absent from all GT images are excluded from AP
                # (they never appear in true_class_ids and are skipped by the AP loop).
                stats.append(
                    (
                        np.zeros(
                            (predicted_objs.shape[0], iou_thresholds.size), dtype=bool
                        ),
                        predicted_objs[:, 5],
                        predicted_objs[:, 4],
                        np.zeros((0,), dtype=np.float32),
                    )
                )

        # Compute average precisions if any matches exist
        if stats:
            concatenated_stats = [np.concatenate(items, 0) for items in zip(*stats)]
            average_precisions = cls._average_precisions_per_class(
                cast(npt.NDArray[np.bool_], concatenated_stats[0]),
                cast(npt.NDArray[np.float32], concatenated_stats[1]),
                cast(npt.NDArray[np.int32], concatenated_stats[2]),
                cast(npt.NDArray[np.int32], concatenated_stats[3]),
            )
            if average_precisions.size == 0:
                # All images had no ground-truth objects; FPs recorded but no class
                # to accumulate AP over → return 0.0 rather than NaN.
                map50, map75, map50_95 = 0.0, 0.0, 0.0
            else:
                map50 = float(average_precisions[:, 0].mean())
                map75 = float(average_precisions[:, 5].mean())
                map50_95 = float(average_precisions.mean())
        else:
            map50, map75, map50_95 = 0, 0, 0
            average_precisions = np.array([])

        return cls(
            map50_95=map50_95,
            map50=map50,
            map75=map75,
            per_class_ap50_95=average_precisions,
        )

    @staticmethod
    def compute_average_precision(
        recall: npt.NDArray[np.float64],
        precision: npt.NDArray[np.float64],
    ) -> float:
        """
        Compute the average precision using 101-point interpolation (COCO), given
            the recall and precision curves.

        Args:
            recall: The recall curve.
            precision: The precision curve.

        Returns:
            Average precision.
        """
        extended_recall = np.concatenate(([0.0], recall, [1.0]))
        extended_precision = np.concatenate(([0.0], precision, [0.0]))
        max_accumulated_precision = np.flip(
            np.maximum.accumulate(np.flip(extended_precision))
        )
        interpolated_recall_levels = np.linspace(0, 1, 101)
        recall_indices = np.searchsorted(
            extended_recall, interpolated_recall_levels, side="left"
        )
        interpolated_precision = max_accumulated_precision[recall_indices]
        average_precision = np.mean(interpolated_precision)

        return float(average_precision)

    @staticmethod
    def _match_detection_batch(
        predictions: npt.NDArray[np.float32],
        targets: npt.NDArray[np.float32],
        iou_thresholds: npt.NDArray[np.float32],
    ) -> npt.NDArray[np.bool_]:
        """
        Match predictions with target labels based on IoU levels.

        Args:
            predictions: Batch prediction. Describes a single image and
                has `shape = (M, 6)` where `M` is the number of detected objects.
                Each row is expected to be in
                `(x_min, y_min, x_max, y_max, class, conf)` format.
            targets: Batch target labels. Describes a single image and
                has `shape = (N, 5)` where `N` is the number of ground-truth objects.
                Each row is expected to be in
                `(x_min, y_min, x_max, y_max, class)` format.
            iou_thresholds: Array contains different IoU thresholds.

        Returns:
            Matched prediction with target labels result.
        """
        num_predictions, num_iou_levels = predictions.shape[0], iou_thresholds.shape[0]
        correct = np.zeros((num_predictions, num_iou_levels), dtype=bool)
        iou = box_iou_batch(targets[:, :4], predictions[:, :4])
        correct_class = targets[:, 4:5] == predictions[:, 4]

        for i, iou_level in enumerate(iou_thresholds):
            matched_indices = np.where((iou >= iou_level) & correct_class)

            for t, p in _greedy_match(iou, matched_indices):
                correct[p, i] = True
        result: npt.NDArray[np.bool_] = correct
        return result

    @staticmethod
    def _average_precisions_per_class(
        matches: npt.NDArray[np.bool_],
        prediction_confidence: npt.NDArray[np.float32],
        prediction_class_ids: npt.NDArray[np.int32],
        true_class_ids: npt.NDArray[np.int32],
        eps: float = 1e-16,
    ) -> npt.NDArray[np.float64]:
        """
        Compute the average precision, given the recall and precision curves.
        Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.

        Args:
            matches: True positives.
            prediction_confidence: Objectness value from 0-1.
            prediction_class_ids: Predicted object classes.
            true_class_ids: True object classes.
            eps: Small value to prevent division by zero.

        Returns:
            Average precision for different IoU levels.
        """
        sorted_indices = np.argsort(-prediction_confidence)
        matches = matches[sorted_indices]
        prediction_class_ids = prediction_class_ids[sorted_indices]

        unique_classes, class_counts = np.unique(true_class_ids, return_counts=True)
        num_classes = unique_classes.shape[0]

        average_precisions: npt.NDArray[np.float64] = np.zeros(
            (num_classes, matches.shape[1]), dtype=np.float64
        )

        for class_idx, class_id in enumerate(unique_classes):
            is_class = prediction_class_ids == class_id
            total_true = class_counts[class_idx]
            total_prediction = is_class.sum()

            if total_prediction == 0 or total_true == 0:
                continue

            false_positives = (1 - matches[is_class]).cumsum(0)
            true_positives = matches[is_class].cumsum(0)
            recall = true_positives / (total_true + eps)
            precision = true_positives / (true_positives + false_positives)

            for iou_level_idx in range(matches.shape[1]):
                average_precisions[class_idx, iou_level_idx] = (
                    MeanAveragePrecision.compute_average_precision(
                        recall[:, iou_level_idx], precision[:, iou_level_idx]
                    )
                )

        result: npt.NDArray[np.float64] = average_precisions
        return result

Methods:

benchmark(dataset: DetectionDataset, callback: Callable[[npt.NDArray[np.uint8]], Detections]) -> MeanAveragePrecision classmethod

Calculate mean average precision from dataset and callback function.

Parameters:

Name Type Description Default
dataset
DetectionDataset

Object detection dataset used for evaluation.

required
callback
Callable[[NDArray[uint8]], Detections]

Function that takes an image as input and returns Detections object.

required

Returns: New instance of MeanAveragePrecision.

Example
import supervision as sv
from ultralytics import YOLO

dataset = sv.DetectionDataset.from_yolo(...)

model = YOLO(...)
def callback(image: np.ndarray) -> sv.Detections:
    result = model(image)[0]
    return sv.Detections.from_ultralytics(result)

mean_average_precision = sv.MeanAveragePrecision.benchmark(
    dataset = dataset,
    callback = callback
)

print(mean_average_precision.map50_95)
# 0.433
Source code in src/supervision/metrics/detection.py
@classmethod
def benchmark(
    cls,
    dataset: DetectionDataset,
    callback: Callable[[npt.NDArray[np.uint8]], Detections],
) -> MeanAveragePrecision:
    """
    Calculate mean average precision from dataset and callback function.

    Args:
        dataset: Object detection dataset used for evaluation.
        callback: Function that takes
            an image as input and returns Detections object.
    Returns:
        New instance of MeanAveragePrecision.

    Example:
        ```python
        import supervision as sv
        from ultralytics import YOLO

        dataset = sv.DetectionDataset.from_yolo(...)

        model = YOLO(...)
        def callback(image: np.ndarray) -> sv.Detections:
            result = model(image)[0]
            return sv.Detections.from_ultralytics(result)

        mean_average_precision = sv.MeanAveragePrecision.benchmark(
            dataset = dataset,
            callback = callback
        )

        print(mean_average_precision.map50_95)
        # 0.433
        ```
    """
    predictions, targets = [], []
    for _, image, annotation in dataset:
        predictions_batch = callback(image)
        predictions.append(predictions_batch)
        targets.append(annotation)
    return cls.from_detections(
        predictions=predictions,
        targets=targets,
    )

compute_average_precision(recall: npt.NDArray[np.float64], precision: npt.NDArray[np.float64]) -> float staticmethod

Compute the average precision using 101-point interpolation (COCO), given the recall and precision curves.

Parameters:

Name Type Description Default
recall
NDArray[float64]

The recall curve.

required
precision
NDArray[float64]

The precision curve.

required

Returns:

Type Description
float

Average precision.

Source code in src/supervision/metrics/detection.py
@staticmethod
def compute_average_precision(
    recall: npt.NDArray[np.float64],
    precision: npt.NDArray[np.float64],
) -> float:
    """
    Compute the average precision using 101-point interpolation (COCO), given
        the recall and precision curves.

    Args:
        recall: The recall curve.
        precision: The precision curve.

    Returns:
        Average precision.
    """
    extended_recall = np.concatenate(([0.0], recall, [1.0]))
    extended_precision = np.concatenate(([0.0], precision, [0.0]))
    max_accumulated_precision = np.flip(
        np.maximum.accumulate(np.flip(extended_precision))
    )
    interpolated_recall_levels = np.linspace(0, 1, 101)
    recall_indices = np.searchsorted(
        extended_recall, interpolated_recall_levels, side="left"
    )
    interpolated_precision = max_accumulated_precision[recall_indices]
    average_precision = np.mean(interpolated_precision)

    return float(average_precision)

from_detections(predictions: list[Detections], targets: list[Detections]) -> MeanAveragePrecision classmethod

Calculate mean average precision based on predicted and ground-truth detections.

Parameters:

Name Type Description Default
targets
list[Detections]

Detections objects from ground-truth.

required
predictions
list[Detections]

Detections objects predicted by the model.

required

Returns: New instance of ConfusionMatrix.

Examples:

>>> import numpy as np
>>> import supervision as sv
>>> targets = [
...     sv.Detections(
...         xyxy=np.array([[0, 0, 10, 10]]),
...         class_id=np.array([0])
...     )
... ]
>>> predictions = [
...     sv.Detections(
...         xyxy=np.array([[0, 0, 10, 10]]),
...         class_id=np.array([0]),
...         confidence=np.array([0.9])
...     )
... ]
>>> mAP = sv.MeanAveragePrecision.from_detections(
...     predictions=predictions,
...     targets=targets,
... )
>>> round(float(mAP.map50), 2)
1.0
Source code in src/supervision/metrics/detection.py
@classmethod
def from_detections(
    cls,
    predictions: list[Detections],
    targets: list[Detections],
) -> MeanAveragePrecision:
    """
    Calculate mean average precision based on predicted and ground-truth detections.

    Args:
        targets: Detections objects from ground-truth.
        predictions: Detections objects predicted by the model.
    Returns:
        New instance of ConfusionMatrix.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> import supervision as sv
        >>> targets = [
        ...     sv.Detections(
        ...         xyxy=np.array([[0, 0, 10, 10]]),
        ...         class_id=np.array([0])
        ...     )
        ... ]
        >>> predictions = [
        ...     sv.Detections(
        ...         xyxy=np.array([[0, 0, 10, 10]]),
        ...         class_id=np.array([0]),
        ...         confidence=np.array([0.9])
        ...     )
        ... ]
        >>> mAP = sv.MeanAveragePrecision.from_detections(
        ...     predictions=predictions,
        ...     targets=targets,
        ... )
        >>> round(float(mAP.map50), 2)
        1.0

        ```
    """
    prediction_tensors = []
    target_tensors = []
    for prediction, target in zip(predictions, targets):
        prediction_tensors.append(
            detections_to_tensor(prediction, with_confidence=True)
        )
        target_tensors.append(detections_to_tensor(target, with_confidence=False))
    return cls.from_tensors(
        predictions=prediction_tensors,
        targets=target_tensors,
    )

from_tensors(predictions: list[npt.NDArray[np.float32]], targets: list[npt.NDArray[np.float32]]) -> MeanAveragePrecision classmethod

Calculate Mean Average Precision based on predicted and ground-truth detections at different threshold.

Parameters:

Name Type Description Default
predictions
list[NDArray[float32]]

Each element of the list describes a single image and has shape = (M, 6) where M is the number of detected objects. Each row is expected to be in (x_min, y_min, x_max, y_max, class, conf) format.

required
targets
list[NDArray[float32]]

Each element of the list describes a single image and has shape = (N, 5) where N is the number of ground-truth objects. Each row is expected to be in (x_min, y_min, x_max, y_max, class) format. An empty array (N = 0) represents a background image; all predictions on that image count as false positives and reduce AP accordingly.

required

Returns:

Name Type Description
MeanAveragePrecision MeanAveragePrecision

New instance computed from the provided predictions and targets.

Examples:

>>> import supervision as sv
>>> import numpy as np
>>> targets = [
...     np.array([
...         [0.0, 0.0, 3.0, 3.0, 0],
...         [2.0, 2.0, 5.0, 5.0, 0],
...         [6.0, 1.0, 8.0, 3.0, 1],
...     ])
... ]
>>> predictions = [
...     np.array([
...         [0.0, 0.0, 3.0, 3.0, 0, 0.9],
...         [0.1, 0.1, 3.0, 3.0, 0, 0.9],
...         [6.0, 1.0, 8.0, 3.0, 1, 0.8],
...     ])
... ]
>>> mAP = sv.MeanAveragePrecision.from_tensors(
...     predictions=predictions,
...     targets=targets,
... )
>>> round(float(mAP.map50), 2)
0.75

>>> bg_pred = [np.array([[0., 0., 10., 10., 0, 0.9]], dtype=np.float32)]
>>> bg_tgt = [np.zeros((0, 5), dtype=np.float32)]
>>> mAP_bg = sv.MeanAveragePrecision.from_tensors(
...     predictions=bg_pred,
...     targets=bg_tgt,
... )
>>> float(mAP_bg.map50)
0.0
Source code in src/supervision/metrics/detection.py
@classmethod
def from_tensors(
    cls,
    predictions: list[npt.NDArray[np.float32]],
    targets: list[npt.NDArray[np.float32]],
) -> MeanAveragePrecision:
    """
    Calculate Mean Average Precision based on predicted and ground-truth
        detections at different threshold.

    Args:
        predictions: Each element of the list describes
            a single image and has `shape = (M, 6)` where `M` is
            the number of detected objects. Each row is expected to be
            in `(x_min, y_min, x_max, y_max, class, conf)` format.
        targets: Each element of the list describes a single
            image and has `shape = (N, 5)` where `N` is the
            number of ground-truth objects. Each row is expected to be in
            `(x_min, y_min, x_max, y_max, class)` format. An empty array
            (``N = 0``) represents a background image; all predictions on
            that image count as false positives and reduce AP accordingly.

    Returns:
        MeanAveragePrecision: New instance computed from the provided
            predictions and targets.

    Examples:
        ```pycon
        >>> import supervision as sv
        >>> import numpy as np
        >>> targets = [
        ...     np.array([
        ...         [0.0, 0.0, 3.0, 3.0, 0],
        ...         [2.0, 2.0, 5.0, 5.0, 0],
        ...         [6.0, 1.0, 8.0, 3.0, 1],
        ...     ])
        ... ]
        >>> predictions = [
        ...     np.array([
        ...         [0.0, 0.0, 3.0, 3.0, 0, 0.9],
        ...         [0.1, 0.1, 3.0, 3.0, 0, 0.9],
        ...         [6.0, 1.0, 8.0, 3.0, 1, 0.8],
        ...     ])
        ... ]
        >>> mAP = sv.MeanAveragePrecision.from_tensors(
        ...     predictions=predictions,
        ...     targets=targets,
        ... )
        >>> round(float(mAP.map50), 2)
        0.75

        >>> bg_pred = [np.array([[0., 0., 10., 10., 0, 0.9]], dtype=np.float32)]
        >>> bg_tgt = [np.zeros((0, 5), dtype=np.float32)]
        >>> mAP_bg = sv.MeanAveragePrecision.from_tensors(
        ...     predictions=bg_pred,
        ...     targets=bg_tgt,
        ... )
        >>> float(mAP_bg.map50)
        0.0

        ```
    """
    _validate_input_tensors(predictions, targets)
    iou_thresholds = np.linspace(0.5, 0.95, 10, dtype=np.float32)
    stats = []

    # Gather matching stats for predictions and targets
    for true_objs, predicted_objs in zip(targets, predictions):
        if predicted_objs.shape[0] == 0:
            if true_objs.shape[0]:
                stats.append(
                    (
                        np.zeros((0, iou_thresholds.size), dtype=bool),
                        *np.zeros((2, 0)),
                        true_objs[:, 4],
                    )
                )
            continue

        if true_objs.shape[0]:
            matches = cls._match_detection_batch(
                predicted_objs, true_objs, iou_thresholds
            )
            stats.append(
                (
                    matches,
                    predicted_objs[:, 5],
                    predicted_objs[:, 4],
                    true_objs[:, 4],
                )
            )
        else:
            # Background image: no GT boxes, so all predictions are FP matches.
            # This lowers AP for classes that appear in at least one GT image
            # elsewhere; classes absent from all GT images are excluded from AP
            # (they never appear in true_class_ids and are skipped by the AP loop).
            stats.append(
                (
                    np.zeros(
                        (predicted_objs.shape[0], iou_thresholds.size), dtype=bool
                    ),
                    predicted_objs[:, 5],
                    predicted_objs[:, 4],
                    np.zeros((0,), dtype=np.float32),
                )
            )

    # Compute average precisions if any matches exist
    if stats:
        concatenated_stats = [np.concatenate(items, 0) for items in zip(*stats)]
        average_precisions = cls._average_precisions_per_class(
            cast(npt.NDArray[np.bool_], concatenated_stats[0]),
            cast(npt.NDArray[np.float32], concatenated_stats[1]),
            cast(npt.NDArray[np.int32], concatenated_stats[2]),
            cast(npt.NDArray[np.int32], concatenated_stats[3]),
        )
        if average_precisions.size == 0:
            # All images had no ground-truth objects; FPs recorded but no class
            # to accumulate AP over → return 0.0 rather than NaN.
            map50, map75, map50_95 = 0.0, 0.0, 0.0
        else:
            map50 = float(average_precisions[:, 0].mean())
            map75 = float(average_precisions[:, 5].mean())
            map50_95 = float(average_precisions.mean())
    else:
        map50, map75, map50_95 = 0, 0, 0
        average_precisions = np.array([])

    return cls(
        map50_95=map50_95,
        map50=map50,
        map75=map75,
        per_class_ap50_95=average_precisions,
    )

Comments