Learn R Programming

torchvision (version 0.7.0)

oxfordiiitpet_segmentation_dataset: Oxford-IIIT Pet Segmentation Dataset

Description

The Oxford-IIIT Pet Dataset is a segmentation dataset consisting of color images of 37 pet breeds (cats and dogs). Each image is annotated with a pixel-level trimap segmentation mask, identifying pet, background, and outline regions. It is commonly used for evaluating models on object segmentation tasks.

Usage

oxfordiiitpet_segmentation_dataset(
  root = tempdir(),
  train = TRUE,
  target_type = "category",
  transform = NULL,
  target_transform = NULL,
  download = FALSE
)

Value

A torch dataset object oxfordiiitpet_dataset. Each item is a named list:

  • x: a H x W x 3 integer array representing an RGB image.

  • y$masks: a boolean tensor of shape (3, H, W), representing the segmentation trimap as one-hot masks.

  • y$label: an integer representing the class label, depending on the target_type:

    • "category": an integer in 1–37 indicating the pet breed.

    • "binary-category": 1 for Cat, 2 for Dog.

Arguments

root

Character. Root directory where the dataset is stored or will be downloaded to. Files are placed under root/oxfordiiitpet.

train

Logical. If TRUE, use the training set; otherwise, use the test set. Not applicable to all datasets.

target_type

Character. One of "category" or "binary-category" (default: "category").

transform

Optional. A function that takes an image and returns a transformed version (e.g., normalization, cropping).

target_transform

Optional. A function that transforms the label.

download

Logical. If TRUE, downloads the dataset to root/. If the dataset is already present, download is skipped.

Examples

Run this code
if (FALSE) {
# Load the Oxford-IIIT Pet dataset with basic tensor transform
oxfordiiitpet <- oxfordiiitpet_segmentation_dataset(
   transform = transform_to_tensor,
   download = TRUE
)

# Retrieve the image tensor, segmentation mask and label
first_item <- oxfordiiitpet[1]
first_item$x  # RGB image tensor of shape (3, H, W)
first_item$y$masks   # (3, H, W) bool tensor: pet, background, outline
first_item$y$label  # Integer label (1–37 or 1–2 depending on target_type)
oxfordiiitpet$classes[first_item$y$label] # Class name of the label

# Visualize
overlay <- draw_segmentation_masks(first_item)
tensor_image_browse(overlay)
}

Run the code above in your browser using DataLab