coco_segmentation_dataset: COCO Segmentation Dataset

coco_segmentation_datasetR Documentation

COCO Segmentation Dataset

Description

Loads the MS COCO dataset for instance segmentation tasks.

Usage

coco_segmentation_dataset(
  root = tempdir(),
  train = TRUE,
  year = c("2017", "2014"),
  download = FALSE,
  transform = NULL,
  target_transform = NULL,
  item_transform = NULL
)

Arguments

root

Root directory where the dataset is stored or will be downloaded to.

train

Logical. If TRUE, loads the training split; otherwise, loads the validation split.

year

Character. Dataset version year. One of "2014" or "2017".

download

Logical. If TRUE, downloads the dataset if it's not already present in the root directory.

transform

Optional transform function applied to the image.

target_transform

Optional transform function applied to the target. Use target_transform_coco_masks to convert polygon annotations to binary masks.

item_transform

Optional transform function applied to the whole item, such as item_transform_rotate(), once transform and target_transform have been applied.

Details

The returned image x is in CHW format (channels, height, width), matching the torch convention. The dataset y offers instance segmentation annotations including labels, crowd indicators, and segmentation masks from the official COCO annotations.

Files are downloaded to a coco subdirectory in the torch cache directory for better organization.

Value

An object of class coco_segmentation_dataset. Each item is a list:

  • x: a ⁠(C, H, W)⁠ array representing the image.

  • y$labels: an integer torch_tensor with the class label for each object.

  • y$iscrowd: a boolean torch_tensor, where TRUE marks the object as part of a crowd.

  • y$segmentation: a list of segmentation polygons for each object.

  • y$masks: a ⁠(N, H, W)⁠ boolean torch_tensor containing binary segmentation masks (when using target_transform_coco_masks).

The returned object has S3 class "image_with_segmentation_mask" to enable automatic dispatch by visualization functions such as draw_segmentation_masks(), and its y has S3 class "segmentation_target" so that target transforms such as target_transform_coco_masks() dispatch on it. The dataset itself inherits "segmentation_dataset".

For object detection tasks without segmentation, use coco_detection_dataset instead.

See Also

coco_detection_dataset for object detection tasks

Other segmentation_dataset: cityscapes_dataset(), oxfordiiitpet_segmentation_dataset(), pascal_voc_datasets, rf100_peixos_segmentation_dataset()

Examples

## Not run: 
# Load dataset for instance segmentation
ds <- coco_segmentation_dataset(
  train = FALSE,
  year = "2017",
  download = TRUE,
  target_transform = target_transform_coco_masks
)

item <- ds[1]

# Visualize segmentation masks
masked <- draw_segmentation_masks(item)
tensor_image_browse(masked)

## End(Not run)

torchvision documentation built on Oct. 14, 2026, 1:09 a.m.