Repository: roboflow/supervision
File: supervision/metrics/detection.py (Python)

Code before:
                stats.append(
                    (
                        correct,
                        detection_batch[:, conf_index],
                        detection_batch[:, class_index],
                        true_batch[:, class_index],
                    )
                )

        stats = [np.concatenate(x, 0) for x in zip(*stats)]

        if len(stats) and stats[0].any():
            average_precisions = cls.average_precisions_per_class(*stats)
            ap50, ap75, average_precisions = (
                average_precisions[:, 0],
                average_precisions[:, 5],
                average_precisions.mean(1),
            )
            map50, map75, map = ap50.mean(), ap75.mean(), average_precisions.mean()

        return cls(
            map=map, map50=map50, map75=map75, average_precisions=average_precisions
        )

    @staticmethod
    def detections_to_tensor(detections: Detections) -> np.ndarray:
        if detections.class_id is None:
            raise ValueError(
                "MeanAveragePrecision can only be calculated for Detections with class_id"
            )

        return np.concatenate(
            [
                detections.xyxy,
                np.expand_dims(detections.class_id, 1),
                np.expand_dims(detections.confidence, 1),
            ],
            1,
        )

    @staticmethod
    def targets_to_tensor(detections: Detections) -> np.ndarray:
        if detections.class_id is None:
            raise ValueError(
                "MeanAveragePrecision can only be calculated for Detections with class_id"
            )
        return np.hstack([detections.xyxy, np.expand_dims(detections.class_id, 1)])

    @staticmethod
    def _match_detection_batch(
        predictions: np.ndarray, targets: np.ndarray, iou_levels: np.ndarray

Review comment (refactor):
@hardikdava, is there any reason you decided to create your implementation of `detections_to_tensor`? I'm not sure, but it looks like we could make `ConfusionMatrix.detections_to_tensor` a reusable utility. And use it in both places. 

Code after:
                    (
                        correct,
                        detection_batch[:, conf_index],
                        detection_batch[:, class_index],
                        true_batch[:, class_index],
                    )
                )

        stats = [np.concatenate(x, 0) for x in zip(*stats)]

        if len(stats) and stats[0].any():
            average_precisions = cls._average_precisions_per_class(*stats)
            ap50, ap75, average_precisions = (
                average_precisions[:, 0],
                average_precisions[:, 5],
                average_precisions.mean(1),
            )
            map50, map75, map = ap50.mean(), ap75.mean(), average_precisions.mean()

        return cls(map=map, map50=map50, map75=map75, per_class_ap=average_precisions)

    @staticmethod
    def _match_detection_batch(
        predictions: np.ndarray, targets: np.ndarray, iou_levels: np.ndarray
    ) -> np.ndarray:
        """
        Args:
            predictions (np.ndarray): batch prediction
            targets (np.ndarray): batch target labels
            iou_levels (np.ndarray): iou levels array contains different iou levels

        Returns:
            (np.ndarray): matched prediction with target lebels result
        """
        correct = np.zeros((predictions.shape[0], iou_levels.shape[0])).astype(bool)
        iou = box_iou_batch(targets[:, :4], predictions[:, :4])

        correct_class = targets[:, 4:5] == predictions[:, 4]

        for i in range(len(iou_levels)):
            x = np.where((iou >= iou_levels[i]) & correct_class)

            if x[0].shape[0]:
                _X1 = np.concatenate(
                    [np.expand_dims(x[0], 1), np.expand_dims(x[1], 1)], axis=1
                )
                _x2 = iou[x[0], x[1]][:, None]
                matches = np.concatenate([_X1, _x2], axis=1)
                if x[0].shape[0] > 1:
                    matches = matches[matches[:, 2].argsort()[::-1]]
                    matches = matches[np.unique(matches[:, 1], return_index=True)[1]]