PyTorch Utilities

class faster_coco_eval.utils.pytorch.FasterCocoDetection(root, annFile, transform=None, target_transform=None, transforms=None)

Bases: CocoDetection

MS Coco Detection Dataset.

Parameters:
  • root (str | Path) – Root directory where images are downloaded to.

  • annFile (str) – Path to json annotation file.

  • transform (Callable | None) – A function/transform that takes in a PIL image and returns a transformed version. E.g., transforms.ToTensor.

  • target_transform (Callable | None) – A function/transform that takes in the target and transforms it.

  • transforms (Callable | None) – A function/transform that takes input sample and its target as entry and returns a transformed version.

class faster_coco_eval.utils.pytorch.FasterCocoEvaluator(coco_gt, iou_types, lvis_style=False, ranges={'large': [9216, 10000000000.0], 'medium': [1024, 9216], 'small': [0, 1024]})

Bases: object

COCO evaluator for distributed evaluation.

Parameters:
  • coco_gt (COCO) – Ground truth COCO object.

  • iou_types (list[str]) – List of IoU types to evaluate, e.g., [‘bbox’, ‘segm’, ‘keypoints’].

  • lvis_style (bool) – Whether to use LVIS-style evaluation. Defaults to False.

accumulate()

Accumulates evaluation results.

Return type:

None

Returns:

None

cleanup()

Cleans up and re-initializes the evaluator state for a new evaluation run.

Return type:

None

Returns:

None

prepare(predictions, iou_type)

Prepares predictions for COCO evaluation.

Parameters:
  • predictions (dict[Any, Any]) – Dictionary mapping image ids to predictions.

  • iou_type (str) – Type of IoU to prepare for, e.g., ‘bbox’, ‘segm’, or ‘keypoints’.

Returns:

List of detection results in COCO format.

Return type:

list[dict[str, Any]]

prepare_for_coco_detection(predictions)

Converts bounding box predictions to COCO detection format.

Parameters:

predictions (dict[Any, Any]) – Dictionary mapping image ids to prediction dicts. Each prediction dict must contain: - “boxes”: Tensor of shape [N, 4] (x1, y1, x2, y2) - “scores”: torch.Tensor of shape [N] of length N - “labels”: torch.Tensor of shape [N] of length N

Returns:

List of detection results in COCO format.

Return type:

list[dict[str, Any]]

prepare_for_coco_keypoint(predictions)

Converts keypoint predictions to COCO keypoint format.

Parameters:

predictions (dict[Any, Any]) – Dictionary mapping image ids to prediction dicts. Each prediction dict must contain: - “boxes”: Tensor of shape [N, 4] - “scores”: torch.Tensor of shape [N] - “labels”: torch.Tensor of shape [N] - “keypoints”: Tensor of shape [N, K, 3]

Returns:

List of keypoint results in COCO format.

Return type:

list[dict[str, Any]]

prepare_for_coco_segmentation(predictions)

Converts mask predictions to COCO segmentation format.

Parameters:

predictions (dict[Any, Any]) – Dictionary mapping image ids to prediction dicts. Each prediction dict must contain: - “scores”: torch.Tensor of shape [N] - “labels”: torch.Tensor of shape [N] - “masks”: Tensor of shape [N, 1, H, W]

Returns:

List of segmentation results in COCO format.

Return type:

list[dict[str, Any]]

summarize()

Prints and stores evaluation statistics.

Return type:

None

Returns:

None

synchronize_between_processes()

Synchronizes results across distributed processes.

Return type:

None

Returns:

None

update(predictions)

Updates the evaluator with new predictions.

Parameters:

predictions (dict[Any, Any]) – Dictionary mapping image ids to predictions. The structure depends on the IoU type.

Return type:

None

Returns:

None

Implementation modules

MS Coco Detection <http://mscoco.org/dataset/#detections-challenge2016>`_ Dataset.

Mostly copy-paste from https://github.com/pytorch/vision/blob/edfd5a7701310589927d2f83bed11cfeb06965a1/torchvision/datasets/coco.py The difference is that pycocotools is replaced by a faster library faster-coco-eval

class faster_coco_eval.utils.pytorch.coco_dataset.FasterCocoDetection(root, annFile, transform=None, target_transform=None, transforms=None)

MS Coco Detection Dataset.

Parameters:
  • root (str | Path) – Root directory where images are downloaded to.

  • annFile (str) – Path to json annotation file.

  • transform (Callable | None) – A function/transform that takes in a PIL image and returns a transformed version. E.g., transforms.ToTensor.

  • target_transform (Callable | None) – A function/transform that takes in the target and transforms it.

  • transforms (Callable | None) – A function/transform that takes input sample and its target as entry and returns a transformed version.

COCO evaluator that works in distributed mode.

Mostly copy-paste from https://github.com/pytorch/vision/blob/edfd5a7/references/detection/coco_eval.py The difference is that pycocotools is replaced by a faster library faster-coco-eval

class faster_coco_eval.utils.pytorch.coco_eval.FasterCocoEvaluator(coco_gt, iou_types, lvis_style=False, ranges={'large': [9216, 10000000000.0], 'medium': [1024, 9216], 'small': [0, 1024]})

COCO evaluator for distributed evaluation.

Parameters:
  • coco_gt (COCO) – Ground truth COCO object.

  • iou_types (list[str]) – List of IoU types to evaluate, e.g., [‘bbox’, ‘segm’, ‘keypoints’].

  • lvis_style (bool) – Whether to use LVIS-style evaluation. Defaults to False.

accumulate()

Accumulates evaluation results.

Return type:

None

Returns:

None

cleanup()

Cleans up and re-initializes the evaluator state for a new evaluation run.

Return type:

None

Returns:

None

prepare(predictions, iou_type)

Prepares predictions for COCO evaluation.

Parameters:
  • predictions (dict[Any, Any]) – Dictionary mapping image ids to predictions.

  • iou_type (str) – Type of IoU to prepare for, e.g., ‘bbox’, ‘segm’, or ‘keypoints’.

Returns:

List of detection results in COCO format.

Return type:

list[dict[str, Any]]

prepare_for_coco_detection(predictions)

Converts bounding box predictions to COCO detection format.

Parameters:

predictions (dict[Any, Any]) – Dictionary mapping image ids to prediction dicts. Each prediction dict must contain: - “boxes”: Tensor of shape [N, 4] (x1, y1, x2, y2) - “scores”: torch.Tensor of shape [N] of length N - “labels”: torch.Tensor of shape [N] of length N

Returns:

List of detection results in COCO format.

Return type:

list[dict[str, Any]]

prepare_for_coco_keypoint(predictions)

Converts keypoint predictions to COCO keypoint format.

Parameters:

predictions (dict[Any, Any]) – Dictionary mapping image ids to prediction dicts. Each prediction dict must contain: - “boxes”: Tensor of shape [N, 4] - “scores”: torch.Tensor of shape [N] - “labels”: torch.Tensor of shape [N] - “keypoints”: Tensor of shape [N, K, 3]

Returns:

List of keypoint results in COCO format.

Return type:

list[dict[str, Any]]

prepare_for_coco_segmentation(predictions)

Converts mask predictions to COCO segmentation format.

Parameters:

predictions (dict[Any, Any]) – Dictionary mapping image ids to prediction dicts. Each prediction dict must contain: - “scores”: torch.Tensor of shape [N] - “labels”: torch.Tensor of shape [N] - “masks”: Tensor of shape [N, 1, H, W]

Returns:

List of segmentation results in COCO format.

Return type:

list[dict[str, Any]]

summarize()

Prints and stores evaluation statistics.

Return type:

None

Returns:

None

synchronize_between_processes()

Synchronizes results across distributed processes.

Return type:

None

Returns:

None

update(predictions)

Updates the evaluator with new predictions.

Parameters:

predictions (dict[Any, Any]) – Dictionary mapping image ids to predictions. The structure depends on the IoU type.

Return type:

None

Returns:

None

faster_coco_eval.utils.pytorch.coco_eval.all_gather(data, world_size=None)

Run all_gather on arbitrary picklable data (not necessarily tensors).

Parameters:
  • data (Any) – Any picklable object.

  • world_size (int) – Number of processes in distributed mode. If None, auto-detect.

Returns:

List of data gathered from each rank.

Return type:

list[Any]

faster_coco_eval.utils.pytorch.coco_eval.convert_to_xywh(boxes)

Converts bounding boxes from (xmin, ymin, xmax, ymax) to (xmin, ymin, width, height) format.

Parameters:

boxes (Tensor) – Bounding boxes of shape [N, 4].

Returns:

Converted bounding boxes of shape [N, 4].

Return type:

Tensor

faster_coco_eval.utils.pytorch.coco_eval.merge(img_ids, eval_imgs, world_size=None)

Merges evaluation results from all processes.

Parameters:
  • img_ids (list[Any] | ndarray) – List or array of image ids.

  • eval_imgs (list[Any]) – List of evaluation image results.

  • world_size (int) – Number of processes in distributed mode. If None, auto-detect.

Returns:

(merged image ids, merged eval images)

Return type:

tuple[list[Any], list[Any]]