Skip to contents

yolo_dataset_copypaste() implements native instance-level Copy-Paste augmentation (inspired by Ghiasi et al., CVPR) for YOLO object detection and instance segmentation datasets.

It extracts real instance objects (bounding boxes or exact polygon masks) of target or minority classes into an in-memory Object Bank, and intelligently pastes them onto other training images with:

  • Scale jitter (scale_range)

  • Random horizontal/vertical reflection (flip_h, flip_v)

  • Ambient luminance and contrast adaptation (match_luminance)

  • Smooth alpha-feathered edge blending (blend_edge)

  • Geometric occlusion checking and annotation updating (max_iou)

Usage

yolo_dataset_copypaste(
  data,
  out_dir = NULL,
  classes = NULL,
  target_count = NULL,
  copies = 1L,
  total_imgs = NULL,
  n_pastes = c(1L, 3L),
  scale_range = c(0.75, 1.25),
  rotation = c(-20, 20),
  flip_h = TRUE,
  flip_v = FALSE,
  match_luminance = TRUE,
  match_strength = 0.4,
  blend_edge = 3L,
  max_iou = 0.7,
  bank_size = 200L,
  quality = 90L,
  clean = FALSE,
  seed = 42,
  as_job = FALSE,
  verbose = TRUE,
  progress = TRUE
)

yolo_dataset_paste(
  data,
  out_dir = NULL,
  classes = NULL,
  target_count = NULL,
  copies = 1L,
  total_imgs = NULL,
  n_pastes = c(1L, 3L),
  scale_range = c(0.75, 1.25),
  rotation = c(-20, 20),
  flip_h = TRUE,
  flip_v = FALSE,
  match_luminance = TRUE,
  match_strength = 0.4,
  blend_edge = 3L,
  max_iou = 0.7,
  bank_size = 200L,
  quality = 90L,
  clean = FALSE,
  seed = 42,
  as_job = FALSE,
  verbose = TRUE,
  progress = TRUE
)

Arguments

data

A yolo_dataset object, directory path, or YAML file path.

out_dir

Character. Directory to save the augmented dataset. If NULL (default), augments in-place using suffix _copypaste<copy>.

classes

Character vector or integer IDs of target classes to extract and paste. If NULL (default), automatically identifies minority classes for balanced injection.

target_count

Optional integer target instances per class in train/. If specified, continues copy-pasting until each target class achieves at least target_count instances.

copies

Integer. Number of copy-paste augmented images generated per training image (default: 1L).

total_imgs

Optional integer. Target total number of training images after augmentation.

n_pastes

Integer vector of length 1 or 2. Range of instances pasted per augmented image (default: c(1, 3)).

scale_range

Numeric vector of length 2. Scaling factor range for pasted instances (default: c(0.75, 1.25)).

rotation

Numeric vector of length 1 or 2. Rotation angles in degrees for pasted instances (default: c(-20, 20)).

flip_h

Logical. Whether to apply 50% random horizontal reflection to pasted instances (default: TRUE).

flip_v

Logical. Whether to apply random vertical reflection to pasted instances (default: FALSE).

match_luminance

Logical. Whether to adapt local mean luminance and contrast of pasted instances to match the surrounding target image background (default: TRUE).

match_strength

Numeric between 0 and 1. Intensity of luminance matching (default: 0.40).

blend_edge

Integer. Pixel radius for alpha-feathering instance edges (default: 3L).

max_iou

Numeric between 0 and 1. Maximum allowable occlusion ratio before removing underlying annotations (default: 0.70).

bank_size

Integer. Maximum number of instances per class sampled at random into the object bank (default: 200L). Only images that contain a sampled instance are decoded, which keeps memory and time bounded on datasets with many objects per image. Use Inf to extract every instance.

quality

Integer (1-100). JPEG compression quality for exported images (default: 90).

clean

Logical. If TRUE, cleans previously generated _copypaste augmented images and labels before generating new copies. Defaults to FALSE.

seed

Optional integer seed for reproducibility (default: 42).

as_job

Logical. If TRUE and running inside RStudio, runs as a background job (default: FALSE).

verbose

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

progress

Logical. Whether to show a CLI progress bar (default: TRUE).

Value

An invisible yolo_dataset object pointing to the augmented dataset directory.

Examples

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

# 1. Copy-paste minority instances onto training images
ds_cp <- yolo_dataset_copypaste("my_dataset")

# 2. Specify target classes and higher paste density
ds_cp2 <- yolo_dataset_copypaste("my_dataset",
                                 classes = "rare_defect",
                                 n_pastes = c(2, 5),
                                 scale_range = c(0.8, 1.3))

# 3. Revert and clean up copy-pasted files
yolo_dataset_copypaste_clean("my_dataset")
}