Skip to contents

yolo_dataset_augment() applies robust offline geometric and photometric augmentations (horizontal/vertical flips, 90/180/270 rotations, random zoom (scale), brightness, contrast, noise, and blur) to a YOLO dataset. Crucially, all bounding boxes and segmentation polygon vertices are dynamically recomputed with mathematical precision to match the transformed images.

Augmentations are exclusively applied to the train/ split, strictly keeping val/ and test/ pristine for unbiased evaluation.

Usage

yolo_dataset_augment(
  data,
  out_dir = NULL,
  total_imgs = NULL,
  copies = 1L,
  flip_h = FALSE,
  flip_v = FALSE,
  rotate = TRUE,
  rotate_range = c(-45, 45),
  perspective = FALSE,
  perspective_x = c(15, 30),
  perspective_y = c(15, 30),
  perspective_z = c(15, 30),
  perspective_scale = c(0.95, 1.05),
  bg_col = "white",
  brightness = FALSE,
  brightness_range = c(0.6, 1.2),
  contrast = FALSE,
  contrast_range = c(1, 3),
  noise = FALSE,
  noise_sd = c(0.05, 0.1),
  blur = FALSE,
  blur_sigma = c(2, 4),
  cutout = FALSE,
  cutout_target = c("object", "image"),
  cutout_pos = c("corner", "random"),
  cutout_n = c(3, 5),
  cutout_size = c(0.4, 0.6),
  cutout_fill = "mean",
  mosaic = FALSE,
  mosaic_prob = 0.5,
  mosaic_overlap = 0.2,
  mosaic_crop = c("quadrant", "random", "center"),
  mosaic_blend = c("none", "feather", "overlay"),
  mosaic_min_area = 0.1,
  scramble_background = FALSE,
  scramble_prop = 0.05,
  scramble_grid = c(8, 8),
  scramble_mode = c("grid", "block_swap", "pixel"),
  permute = FALSE,
  permute_prob = 0.5,
  permute_pos = c("random", "left", "right", "top", "bottom", "corner"),
  permute_fill = c("border_slices", "mean", "blur", "noise"),
  permute_margin = 0.02,
  quality = 90,
  max_ops = c(1, 3),
  seed = 42,
  verbose = TRUE,
  progress = TRUE,
  as_job = FALSE,
  scale = FALSE,
  scale_range = c(0.5, 1.5),
  scale_min_area = 0.1,
  prop = NULL,
  n = NULL,
  shear = FALSE,
  shear_x = c(-15, 15),
  shear_y = c(-15, 15),
  shear_min_area = 0.1,
  bgr = FALSE,
  mixup = FALSE,
  mixup_prob = 0.5,
  mixup_alpha = 32,
  mixup_ratio = NULL,
  cutmix = FALSE,
  cutmix_prob = 0.5,
  cutmix_size = c(0.25, 0.6),
  cutmix_min_area = 0.1,
  ...
)

Arguments

data

A yolo_dataset object, yolo_dataset_cls object, or directory path.

out_dir

Output directory. If NULL (default), augments the dataset in-place.

total_imgs

Integer. The total number of training images desired after data augmentation. For example, if you have 500 images and request total_imgs = 1250, exactly 750 new augmented variants will be generated and distributed across images. If NULL (default), copies is used.

copies

Integer. The number of augmented variants generated per original training image (default: 1L). Ignored if total_imgs is specified.

flip_h

Logical. Allow horizontal flipping with probability 0.5 (default: FALSE).

flip_v

Logical. Allow vertical flipping with probability 0.5 (default: FALSE).

rotate

Vector or logical. Rotation augmentation in degrees. Can be:

  • A single numeric angle (e.g., rotate = 45 or rotate = 90 to strictly rotate by that angle),

  • A continuous range c(min, max) of angles (default: c(-45, 45)),

  • A vector of discrete angles to sample from (e.g. c(0, 90, 180, 270)),

  • TRUE (samples uniformly from rotate_range), or FALSE/0 (no rotation).

rotate_range

Numeric vector of length 2 giving c(min, max) rotation angles in degrees when rotate = TRUE (default: c(-45, 45)).

perspective

Logical. Whether to apply 3D camera perspective warp augmentation (default: FALSE). When TRUE, simulates realistic pitch, yaw, and roll camera tilts.

perspective_x

Numeric vector of length 2 giving c(min, max) pitch tilt range around X in degrees (default: c(20, 40)). If given as a positive range (e.g. c(15, 30)), samples random positive or negative tilts between 20 and 40 degrees.

perspective_y

Numeric vector of length 2 giving c(min, max) yaw tilt range around Y in degrees (default: c(20, 40)). If given as a positive range (e.g. c(15, 30)), samples random positive or negative tilts between 20 and 40 degrees.

perspective_z

Numeric vector of length 2 giving c(min, max) roll angle range around Z in degrees (default: c(20, 40)). If given as a positive range (e.g. c(15, 30)), samples random positive or negative rolls between 20 and 40 degrees.

perspective_scale

Numeric vector of length 2 giving c(min, max) zoom scale range (default: c(0.95, 1.05)).

bg_col

Background fill color for exposed canvas (default: "white").

brightness

Logical. Allow random brightness jitter (default: FALSE).

brightness_range

Numeric vector of length 2 giving c(min, max) brightness scaling factors (default: c(0.6, 1.2)).

contrast

Logical. Allow random contrast adjustment via adaptive histogram equalization (image_contrast()). Defaults to FALSE.

contrast_range

Numeric vector of length 2 giving c(min, max) clip limit for image_contrast() (default: c(1, 3)).

noise

Logical. Allow additive Gaussian noise (default: FALSE).

noise_sd

Numeric vector of length 2 giving c(min, max) standard deviation of Gaussian noise (default: c(0.05, 0.1)).

blur

Logical. Allow Gaussian blurring (default: FALSE).

blur_sigma

Numeric vector of length 2 giving c(min, max) Gaussian blur sigma (default: c(2, 4)).

cutout

Logical. Whether to apply Cutout / Random Erasing augmentation to simulate occlusions (default: FALSE).

cutout_target

Target location for cutout rectangles. Either "object" (default, occludes parts of annotated objects) or "image" (places cutout rectangles randomly across the entire image canvas).

cutout_pos

Positioning strategy when cutout_target = "object". Either "corner" (default, randomly anchors the cutout at one of the four bounding box corners, simulating realistic partial occlusion of objects) or "random" (places cutout anywhere within the bounding box).

cutout_n

Number of cutout rectangles to generate per image/object. Can be an integer or integer range c(min, max) (default: c(3, 5)).

cutout_size

Numeric vector of length 2 giving c(min, max) relative dimensions of the cutout rectangle. When cutout_target = "object", it is relative to object bounding box dimensions (default: c(0.4, 0.6)). When cutout_target = "image", it is relative to image dimensions.

cutout_fill

Fill color or pattern for cutout rectangles. Can be "mean" (channel-wise mean image color, default), "black", "white", "noise" (random uniform noise), or any valid R color name.

mosaic

Logical or numeric. Whether to generate 4-image mosaic augmentations (image_mosaic()). Can be TRUE (applies mosaic with probability mosaic_prob), FALSE (default, disabled), or a numeric probability between 0 and 1 (e.g. mosaic = 1.0 for 100% mosaic augmentation).

mosaic_prob

Numeric between 0 and 1. Probability of applying mosaic augmentation when mosaic = TRUE. Defaults to 0.5.

mosaic_overlap

Fraction of overlap between adjacent tiles in the mosaic composite (default 0.20, i.e., 20%). Passed to image_mosaic().

mosaic_crop

Region to crop from each image for the mosaic: "quadrant" (default), "random", or "center".

mosaic_blend

Blending mode for overlapping regions in the mosaic: "overlay" (default for object detection datasets), "feather", or "none".

mosaic_min_area

Minimum visible area fraction (between 0 and 1) required to retain an object cut by mosaic quadrant seams (default 0.10, i.e., 10 percent).

scramble_background

Logical or numeric. Whether to synthesize negative background images via jigsaw patch scrambling (yolo_dataset_scramble()). If numeric (e.g. 0.05), it specifies the proportion of negative background images to generate relative to the training set. If TRUE, defaults to scramble_prop = 0.05 (5% negative background samples). Defaults to FALSE.

scramble_prop

Numeric between 0 and 1. Proportion of negative background images to generate when scramble_background = TRUE (default: 0.05).

scramble_grid

Integer vector of length 1 or 2 specifying grid divisions for tile scrambling (default: c(8, 8)). Ignored when scramble_mode = "pixel".

scramble_mode

Character specifying the scrambling strategy: "grid" (default jigsaw tile shuffle), "block_swap" (local block swapping preserving partial background continuity), or "pixel" (global pixel shuffle).

permute

Logical. Whether to apply object cluster permutation/relocation augmentation to break center bias. Defaults to FALSE.

permute_prob

Numeric probability in [0, 1] of applying cluster permutation to an image when permute = TRUE. Defaults to 0.5.

permute_pos

Character string specifying target placement regions for relocated clusters: "random", "left", "right", "top", "bottom", or "corner". Defaults to "random".

permute_fill

Character string specifying how vacated object regions are filled: "border_slices", "mean", "blur", or "noise". Defaults to "border_slices".

permute_margin

Numeric relative margin fraction kept clear around image borders when relocating clusters. Defaults to 0.02.

quality

Integer between 1 and 100. JPEG compression quality for exported augmented images (default: 90). Quality 85-90 preserves pristine pixel fidelity while encoding several times faster than 95+.

max_ops

Maximum number of transformations/alterations applied to each augmented image copy (default: c(1, 3)). Can be an integer (e.g., 2 or 3, meaning between 1 and max_ops operations are randomly selected), a range c(min, max) (e.g. c(1, 3)), or NULL (no limit, independent probabilities for all enabled operations).

seed

Optional integer seed for reproducibility (default: 42).

verbose

Logical. Whether to print informative messages. Defaults to TRUE.

progress

Logical. Whether to display a CLI progress bar during data augmentation. Defaults to TRUE.

as_job

Logical. If TRUE and running inside RStudio, launches the data augmentation process in the background via RStudio Background Jobs, leaving the main R console completely free. Defaults to FALSE.

scale

Random zoom (scale) augmentation that keeps the image size, equivalent to the scale hyper-parameter of Ultralytics. Objects appear at other sizes, which makes the model robust to the distance between camera and objects. Can be FALSE (default), TRUE (zoom factor sampled uniformly from scale_range), a single number s (Ultralytics convention: factor sampled from c(1 - s, 1 + s), e.g. scale = 0.5 gives 0.5 to 1.5), or a numeric range c(min, max). With a factor above 1 the image is enlarged and a random window of the original size is cropped; below 1 it is shrunk and pasted at a random position on a bg_col canvas. Boxes, polygons and keypoints are recomputed. Defined after the other arguments for backward compatibility.

scale_range

Numeric vector of length 2 giving c(min, max) zoom factors used when scale = TRUE (default: c(0.5, 1.5)).

scale_min_area

Minimum fraction (between 0 and 1) of an object's area that must remain inside the image after a zoom-in crop for the object to be kept (default 0.10, as in Ultralytics).

prop

Optional numeric in (0, 1] giving the proportion of original training images to sample for augmentation (e.g., prop = 0.2 augments a random 20% subset). Ignored if total_imgs or n is specified.

n

Optional positive integer giving the exact number of original training images to sample for augmentation. Ignored if total_imgs is specified.

shear

Logical or numeric. Introduces a 2D geometric transformation that skews the image along both x-axis and y-axis while maintaining parallel lines (default: FALSE). If numeric (e.g., 15 or c(-15, 15)), sets the shear angle range in degrees along both axes.

shear_x, shear_y

Numeric angle or range c(min, max) in degrees for horizontal (x-axis) and vertical (y-axis) shear when shear = TRUE (default: c(-15, 15)).

shear_min_area

Minimum visible area fraction in [0, 1] required to retain an object partially pushed outside the border by shearing (default: 0.10).

bgr

Logical or numeric in [0, 1]. Swaps the color channels of an image from RGB to BGR (default: FALSE). When numeric, specifies the per-image probability of applying the channel swap.

mixup

Logical or numeric in [0, 1]. Blends two images and concatenates their labels with a given probability (default: FALSE). When TRUE, uses mixup_prob; when numeric (e.g., 0.5), sets the probability directly.

mixup_prob

Numeric in [0, 1]. Probability of applying MixUp when mixup = TRUE (default: 0.5).

mixup_alpha

Positive numeric concentration parameter for rbeta(1, alpha, alpha) mixing weights (default: 32.0, centered near 0.5) or a numeric range c(min, max).

mixup_ratio

Optional numeric range c(min, max) alias overriding mixup_alpha for uniform mixing weights.

cutmix

Logical or numeric in [0, 1]. Cuts a rectangular region from one image and pastes it onto another image (default: FALSE), trimming/dropping occluded base labels and clipping donor labels to the pasted patch. When numeric, defines the probability of attempting CutMix (1.0 attempts on every image, 0.5 on 50% of images, 0.0 disables it).

cutmix_prob

Numeric in [0, 1]. Probability of applying CutMix when cutmix = TRUE (default: 0.5).

cutmix_size

Numeric vector c(min, max) of relative patch width and height for CutMix (default: c(0.25, 0.60)).

cutmix_min_area

Minimum visible area fraction in [0, 1] required to keep partially occluded or clipped objects in CutMix (default: 0.10).

...

Additional arguments (supports alias blurr = TRUE, blurr_sigma, noise_range).

Value

A yolo_dataset object pointing to the augmented dataset directory.

Details

How copies, total_imgs, and transformations work:

  • total_imgs: When specified, sets the exact total number of training images desired after augmentation. For example, if you currently have 500 original training images and specify total_imgs = 1250, the function generates exactly 750 new augmented variants (distributed evenly across training images and classes). Overrides copies.

  • copies = 1L (default when total_imgs = NULL): Generates 1 new augmented variant per original training image (doubling training size).

  • copies = 3L: Generates 3 distinct augmented variants per original training image (quadrupling training size).

The parameters (rotate, flip_h, flip_v, scale, brightness, contrast, noise, blur) define the pool of eligible transformations. Rather than generating a full Cartesian grid product of all possible combinations, the function samples a unique combination of transformations for each copy requested.

Already augmented images (named with suffix _aug[0-9]+) are automatically skipped, preventing recursive dataset explosion if the function is executed multiple times.

Examples

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

# 1. Default: 1 augmented copy per image (doubles training size)
ds_aug1 <- yolo_dataset_augment("dataset_cafe")

# 2. Set an exact target total number of images (e.g. exactly 1250 training images)
ds_aug1250 <- yolo_dataset_augment("dataset_cafe", total_imgs = 1250)

# 3. Cutout augmentation focused on objects
ds_cutout <- yolo_dataset_augment("dataset_cafe",
                                  cutout = TRUE,
                                  cutout_target = "object",
                                  cutout_size = c(0.4, 0.6))

# 4. Mosaic augmentation (4 images stitched with 20% overlap)
ds_mosaic <- yolo_dataset_augment("dataset_cafe",
                                  mosaic = TRUE,
                                  mosaic_overlap = 0.20)

# 5. Break center bias: permute/relocate target clusters to borders with authentic background fill
ds_perm <- yolo_dataset_augment("dataset_cafe",
                                permute = TRUE,
                                permute_prob = 0.5,
                                permute_fill = "border_slices")

# 6. Random zoom between 0.5x and 1.5x (objects closer to and farther from the camera)
ds_scale <- yolo_dataset_augment("dataset_cafe", scale = 0.5, bg_col = "grey45")
}