Skip to contents

yolo_dataset_scramble() (and alias yolo_dataset_shuffle()) generates authentic negative background images (empty label files with zero objects) directly from existing images in a YOLO dataset using patch scrambling / jigsaw tile permutation.

By dividing original images into a grid of tiles (e.g. 8x8 or 16x16), randomly shuffling the tile locations, and optionally applying random 90-degree rotations and flips to individual tiles, the image retains 100% of the true domain characteristics:

  • Authentic sensor noise, ISO grain, and camera compression

  • Exact spectral color distribution, white balance, and contrast

  • Natural scene lighting and shadow hues

Crucially, the puzzle scrambling completely destroys all coherent object contours, aspect ratios, and spatial semantics. Providing these images with empty .txt labels instructs the YOLO object detector that random textures and color patches without structured object contours must produce zero detections, drastically suppressing false-positive hallucinations in production.

Usage

yolo_dataset_scramble(
  data,
  n = NULL,
  prop = 0.05,
  grid = c(8, 8),
  mode = c("grid", "block_swap", "pixel"),
  swap_prob = 0.5,
  shuffle_pixels = TRUE,
  rotate_tiles = TRUE,
  flip_tiles = TRUE,
  train_prop = 0.8,
  prefix = "scramble_",
  quality = 90,
  seed = 42,
  as_job = FALSE,
  verbose = TRUE
)

Arguments

data

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

n

Integer. Exact number of scrambled background images to generate. If NULL, prop is used. Defaults to NULL.

prop

Numeric between 0 and 1. Proportion of original images to generate as scrambled backgrounds (e.g., 0.05 for 5% negatives, following Ultralytics recommendation of 2-10%). Defaults to 0.05.

grid

Integer vector of length 1 or 2. Grid dimensions for tile slicing (e.g. c(8, 8) or 8). Defaults to c(8, 8). Ignored when mode = "pixel".

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).

swap_prob

Numeric between 0 and 1. Fraction of tiles swapped when mode = "block_swap" (default: 0.5).

shuffle_pixels

Logical. Whether to randomize/shuffle all pixels internally within each individual tile (default: TRUE). Combining tile permutation with intra-tile pixel randomization completely dissolves any recognizable sub-objects (e.g. wood logs) while strictly conserving the local background color and textural palette of each region!

rotate_tiles

Logical. Whether to apply random 90-degree rotations (0°, 90°, 180°, 270°) to individual scrambled tiles (default: TRUE). Ignored when mode = "pixel".

flip_tiles

Logical. Whether to apply random horizontal/vertical flips to individual scrambled tiles (default: TRUE). Ignored when mode = "pixel".

train_prop

Numeric. Proportion of scrambled background images placed in the train split vs val split. Defaults to 0.80.

prefix

Character. Filename prefix for the generated scrambled background files (default: "scramble_"). Each generated image and label is named <prefix><uuid>.<ext> (e.g. scramble_6dfb8eb1fe94439dac814f8b1fbe1b45.jpg), guaranteeing unique identifiers and preventing accidental file overwrites when running repeatedly.

quality

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

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 show progress messages (default: TRUE).

Value

An invisible yolo_dataset object pointing to the augmented dataset.

Examples

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

# 1. Tile permutation with intra-tile pixel randomization (default)
yolo_dataset_scramble("my_dataset", prop = 0.05, grid = c(8, 8), shuffle_pixels = TRUE)

# 2. Local block swap preserving partial background continuity
yolo_dataset_scramble("my_dataset", prop = 0.05, grid = c(10, 10), mode = "block_swap")

# 3. Full pixel shuffle (exact spectral color noise)
yolo_dataset_scramble("my_dataset", prop = 0.05, mode = "pixel")

# Undo/clean scrambled backgrounds
yolo_dataset_scramble_undo("my_dataset")
}