Skip to contents

yolo_dataset_to_segment() transforms an existing YOLO object detection dataset (with bounding box labels: class_id cx cy w h) into a high-precision YOLO instance segmentation dataset (with polygon labels: class_id x1 y1 x2 y2 ... xn yn) using Meta AI's Segment Anything Model 2.1 (SAM 2.1).

For each image, the existing bounding boxes are extracted as spatial prompts (box prompts) and fed to SAM 2.1, which isolates object pixels and produces sub-pixel polygon contours.

Usage

yolo_dataset_to_segment(
  data,
  out_dir = NULL,
  threshold = 0.5,
  n_points = 40L,
  min_area = 15,
  fill_hull = TRUE,
  box_margin = 0.05,
  copy = TRUE,
  engine = c("cpu", "gpu"),
  device_id = -1,
  batch_size = 16L,
  overwrite = FALSE,
  resume = FALSE,
  as_job = FALSE,
  verbose = TRUE,
  ...
)

yolo_dataset_detect_to_segment(
  data,
  out_dir = NULL,
  threshold = 0.5,
  n_points = 40L,
  min_area = 15,
  fill_hull = TRUE,
  box_margin = 0.05,
  copy = TRUE,
  engine = c("cpu", "gpu"),
  device_id = -1,
  batch_size = 16L,
  overwrite = FALSE,
  resume = FALSE,
  as_job = FALSE,
  verbose = TRUE,
  ...
)

yolo_dataset_det_to_seg(
  data,
  out_dir = NULL,
  threshold = 0.5,
  n_points = 40L,
  min_area = 15,
  fill_hull = TRUE,
  box_margin = 0.05,
  copy = TRUE,
  engine = c("cpu", "gpu"),
  device_id = -1,
  batch_size = 16L,
  overwrite = FALSE,
  resume = FALSE,
  as_job = FALSE,
  verbose = TRUE,
  ...
)

Arguments

data

A yolo_dataset object or directory path to an existing YOLO detection dataset.

out_dir

Output directory path for the new segmentation dataset. Defaults to paste0(basename(dir_path), "_seg").

threshold

Numeric. Probability threshold for SAM 2.1 mask binarization (default: 0.5).

n_points

Integer. Maximum number of polygon vertices per object (default: 40L).

min_area

Numeric. Minimum polygon pixel area to retain (default: 15).

fill_hull

Logical. Whether to fill holes inside segmented masks (default: TRUE).

box_margin

Safety margin (padding proportion) added around bounding boxes before segmenting with Meta SAM 2.1 (e.g. 0.05 expands 5% on each side). Prevents masks from cutting off object borders. Defaults to 0.05 (5%).

copy

Logical. Whether to copy image files to out_dir (default: TRUE).

engine

Execution engine: "cpu" (default) or "gpu".

device_id

GPU device ID (default: -1).

batch_size

Integer. Maximum batch size of bounding boxes passed to SAM 2.1 decoder at once (default: 16L). The decoder replicates the high-resolution image features (about 8 MB) for every box in a batch, so larger batches run out of fast GPU memory. In one test (DirectML, 1280 x 1280 images), batches of 4-32 boxes cost about 25 ms per box and batches of 64 about 75 ms per box. The best value depends on the GPU and its memory.

overwrite

Logical. Whether to overwrite out_dir if it already exists (default: FALSE).

resume

Logical. If TRUE, resumes processing an existing dataset directory by skipping images that have already been converted (default: FALSE).

as_job

Logical. If TRUE and running inside RStudio, runs the conversion as an RStudio Background Job (default: FALSE).

verbose

Logical. Whether to display progress messages (default: TRUE).

...

Additional arguments. Legacy parameters such as parallel or workers are gracefully ignored with an informative notice.

Value

An object of class c("yolo_dataset", "list") pointing to out_dir.

Examples

if (FALSE) { # interactive()
library(pliman)

# Convert a bounding-box dataset into a polygon segmentation dataset
ds_seg <- yolo_dataset_to_segment(
  data    = "dataset_cafe_det",
  out_dir = "dataset_cafe_seg"
)
}