rpx_benchmark.metrics.segmentation

Instance/semantic segmentation metric calculators.

 1"""Instance/semantic segmentation metric calculators."""
 2
 3from __future__ import annotations
 4
 5from typing import Dict
 6
 7from ..api import SegmentationGroundTruth, SegmentationPrediction, TaskType
 8from ..exceptions import MetricError
 9from .registry import MetricCalculator, register_metric
10
11
12@register_metric(TaskType.OBJECT_SEGMENTATION)
13class SegmentationMIoU(MetricCalculator):
14    """Mean Intersection-over-Union (mIoU) across GT classes.
15
16    Notes
17    -----
18    Averaged over the set of class ids present in the ground-truth
19    mask (background class id ``-1`` is ignored). Classes in the
20    prediction that do not appear in GT do not contribute — they only
21    reduce the IoU of other classes they overlap with.
22    """
23
24    name = "segmentation_miou"
25
26    def compute(
27        self,
28        prediction: SegmentationPrediction,
29        ground_truth: SegmentationGroundTruth,
30    ) -> Dict[str, float]:
31        if not isinstance(prediction, SegmentationPrediction):
32            raise MetricError(
33                f"SegmentationMIoU expected SegmentationPrediction, got "
34                f"{type(prediction).__name__}",
35            )
36        if not isinstance(ground_truth, SegmentationGroundTruth):
37            raise MetricError(
38                f"SegmentationMIoU expected SegmentationGroundTruth, got "
39                f"{type(ground_truth).__name__}",
40            )
41
42        from ..evaluators import segmentation_metrics
43
44        return segmentation_metrics(prediction.mask, ground_truth.mask)
@register_metric(TaskType.OBJECT_SEGMENTATION)
class SegmentationMIoU(rpx_benchmark.metrics.registry.MetricCalculator):
13@register_metric(TaskType.OBJECT_SEGMENTATION)
14class SegmentationMIoU(MetricCalculator):
15    """Mean Intersection-over-Union (mIoU) across GT classes.
16
17    Notes
18    -----
19    Averaged over the set of class ids present in the ground-truth
20    mask (background class id ``-1`` is ignored). Classes in the
21    prediction that do not appear in GT do not contribute — they only
22    reduce the IoU of other classes they overlap with.
23    """
24
25    name = "segmentation_miou"
26
27    def compute(
28        self,
29        prediction: SegmentationPrediction,
30        ground_truth: SegmentationGroundTruth,
31    ) -> Dict[str, float]:
32        if not isinstance(prediction, SegmentationPrediction):
33            raise MetricError(
34                f"SegmentationMIoU expected SegmentationPrediction, got "
35                f"{type(prediction).__name__}",
36            )
37        if not isinstance(ground_truth, SegmentationGroundTruth):
38            raise MetricError(
39                f"SegmentationMIoU expected SegmentationGroundTruth, got "
40                f"{type(ground_truth).__name__}",
41            )
42
43        from ..evaluators import segmentation_metrics
44
45        return segmentation_metrics(prediction.mask, ground_truth.mask)

Mean Intersection-over-Union (mIoU) across GT classes.

Notes

Averaged over the set of class ids present in the ground-truth mask (background class id -1 is ignored). Classes in the prediction that do not appear in GT do not contribute — they only reduce the IoU of other classes they overlap with.

name = 'segmentation_miou'
def compute( self, prediction: rpx_benchmark.api.SegmentationPrediction, ground_truth: rpx_benchmark.api.SegmentationGroundTruth) -> Dict[str, float]:
27    def compute(
28        self,
29        prediction: SegmentationPrediction,
30        ground_truth: SegmentationGroundTruth,
31    ) -> Dict[str, float]:
32        if not isinstance(prediction, SegmentationPrediction):
33            raise MetricError(
34                f"SegmentationMIoU expected SegmentationPrediction, got "
35                f"{type(prediction).__name__}",
36            )
37        if not isinstance(ground_truth, SegmentationGroundTruth):
38            raise MetricError(
39                f"SegmentationMIoU expected SegmentationGroundTruth, got "
40                f"{type(ground_truth).__name__}",
41            )
42
43        from ..evaluators import segmentation_metrics
44
45        return segmentation_metrics(prediction.mask, ground_truth.mask)

Return a dict of metric name → scalar for one sample.

Parameters

prediction : Any Task-specific Prediction dataclass (e.g. DepthPrediction). The concrete type is whatever the calculator is designed for. ground_truth : Any Task-specific GroundTruth dataclass (e.g. DepthGroundTruth).

Returns

dict[str, float] Metric values. Keys should be short snake_case names; values must be numeric so the runner can average them.

Raises

MetricError When inputs have the wrong shape/type/content.