tlc.integration.detectron2.bounding_box_metrics_collector¶

Collect metrics for bounding box predictions.

This module provides a metrics collector for bounding box predictions, designed for use with detectron2. It converts detectron2 Instances objects (or raw COCO-format dicts) into 3LC bounding box and segmentation metric data.

Module Contents¶

Classes¶

Class

Description

BoundingBoxMetricsCollector

Collect predicted bounding boxes and segmentations for detectron2 models.

CocoAnnotation

A single ground truth annotation in the COCO format.

CocoGroundTruth

A single ground truth annotation in the COCO format.

CocoPrediction

A single prediction in the COCO results format.

API¶

class BoundingBoxMetricsCollector(
classes: list[str],
label_mapping: dict[int, int],
preprocess_fn: Callable[[Any, PredictorOutput], tuple[list[CocoGroundTruth], list[CocoPrediction]]] | None = None,
compute_aggregates: bool = True,
save_segmentations: bool = False,
)¶

Bases: tlc.metrics.collectors.metrics_collector_base.MetricsCollector

Collect predicted bounding boxes and segmentations for detectron2 models.

This metrics collector converts model predictions into 3LC bounding box (and optionally segmentation) metric data. It accepts either detectron2 Instances objects or raw COCO-format dicts.

For working with different sample/prediction formats, the preprocess_fn argument can be used to provide a custom preprocessing function. This function should take a batch of samples and predictions and return a tuple of lists of CocoGroundTruth and CocoPrediction respectively.

Parameters:
  • classes – A list of class names.

  • label_mapping – A dictionary mapping class indices to the range [0, num_classes). Class indices in the source dataset could be in any range, so this mapping is used to convert them to the range [0, num_classes), which is usually used in object detection models.

  • preprocess_fn – A function that takes a batch of samples and a batch of predictions and returns modified lists in a standard format, compatible with this metrics collector.

  • compute_aggregates – Whether to compute aggregate metrics.

  • save_segmentations – Whether to save predicted segmentation masks.

Create a new metrics collector.

Parameters:
  • preprocess_fn – A function that pre-processes the batch and predictor output before computing the metrics.

  • compute_aggregates – Whether to compute aggregates for the metrics.

property column_schemas: dict[str, Schema]¶
compute_metrics(
batch: Any,
predictor_output: PredictorOutput,
) dict[str, Any]¶

Compute metrics for bounding box predictions.

Parameters:
  • batch – A batch of samples.

  • predictor_output – The output from the predictor.

Returns:

A dictionary mapping metric names to metric values for a batch of inputs.

preprocess(
batch: Any,
predictor_output: PredictorOutput,
) tuple[list[CocoGroundTruth], list[CocoPrediction]]¶

Preprocess the raw batch and predictor output.

Recognizes and transforms detectron2 samples/predictions, otherwise assumes that the samples/predictions are already in the COCO format.

Parameters:
  • batch – A batch of samples.

  • predictor_output – A batch of predictions.

Returns:

A tuple containing the preprocessed batch and predictions.

class CocoAnnotation¶

Bases: typing.TypedDict

A single ground truth annotation in the COCO format.

Corresponds to the official COCO annotation format: https://cocodataset.org/#format-data

Initialize self. See help(type(self)) for accurate signature.

bbox: list[float] = None¶

The bounding box of the annotation in XYWH format. Foreign COCO field, name fixed by the format.

category_id: int = None¶

The category ID of the annotation. This is the index of the class in the classes list.

image_id: int | None = None¶

The ID of the image that the annotation belongs to.

score: float | None = None¶

The confidence score of the annotation, if the annotation comes from a model prediction.

segmentation: Any | None = None¶
class CocoGroundTruth¶

Bases: typing.TypedDict

A single ground truth annotation in the COCO format.

Corresponds to the official COCO annotation format: https://cocodataset.org/#format-data

Initialize self. See help(type(self)) for accurate signature.

annotations: list[CocoAnnotation] = None¶

The list of ground truth annotations in the image.

height: int | None = None¶

The height of the image.

image_id: int | None = None¶

The ID of the image that the annotation belongs to.

width: int | None = None¶

The width of the image.

class CocoPrediction¶

Bases: typing.TypedDict

A single prediction in the COCO results format.

Initialize self. See help(type(self)) for accurate signature.

annotations: list[CocoAnnotation] = None¶

The list of predicted annotations in the image.