Skip to contents

yolo_dataset_tiling() slices high-resolution images into uniform overlapping tiles (sliding window) suitable for training YOLO object detection and instance segmentation models.

It maps and adapts all existing YOLO bounding boxes (or polygons) to the local coordinate system of each tile, applying the truncation/preservation criterion: keeping objects whose center falls within the tile or whose intersection preserves at least 20% of the original area (configurable via min_area_ratio and assign_by), while masking out discarded border fragments when mask_truncated = TRUE.

Crucially, data splitting across train, val, and test is performed at the source image level (or inherited from existing split directories), ensuring that all tiles derived from the same original image remain in the same split to strictly prevent data leakage.

Empty background tiles (containing no objects) are preserved with empty .txt files to provide essential negative background samples for YOLO training.

Usage

yolo_dataset_tiling(
  data = NULL,
  out_dir = NULL,
  path = NULL,
  n = NULL,
  prop = NULL,
  tile_size = 640L,
  overlap = 0.2,
  n_tiles = NULL,
  min_area_ratio = 0.2,
  assign_by = c("center_or_overlap", "center_and_overlap", "center", "overlap"),
  min_box_size = 4,
  mask_truncated = TRUE,
  include_empty = TRUE,
  max_bg_ratio = NULL,
  split_ratio = c(0.7, 0.2, 0.1),
  pad = TRUE,
  bg_col = "black",
  class_names = NULL,
  seed = 42,
  as_job = FALSE,
  verbose = TRUE,
  progress = TRUE,
  ...,
  parallel = FALSE,
  workers = NULL
)

Arguments

data

A yolo_dataset object, or path to a YOLO dataset directory (or folder containing images and labels).

out_dir

Output directory where the tiled dataset will be created. If NULL (default), tiles the dataset in-place within the same directory.

path

Alias for data.

n

Optional integer. Number of source images to sample for tiling (e.g. n = 50). If NULL (default) and prop is NULL, all available images are tiled.

prop

Optional numeric between 0 and 1. Proportion of source images to sample for tiling (e.g. prop = 0.20 for 20% of the dataset). Ignored if n is provided.

tile_size

Integer or numeric vector of length 2 giving c(width, height). Standard multiple of 32 (default: 640L).

overlap

Numeric fraction of overlap between adjacent tiles (default: 0.20, i.e., 20%). Ignored if n_tiles is set.

n_tiles

Optional integer or numeric vector of length 2 giving exact number of tiles per axis (e.g. n_tiles = 5 or n_tiles = c(5, 5)). If provided, overrides overlap.

min_area_ratio

Minimum proportion of original object area preserved when truncated by a tile boundary (default: 0.20, i.e., 20%).

assign_by

Criterion for keeping truncated annotations: "center_or_overlap" (default, keeps if center is inside tile OR area ratio >= min_area_ratio), "center_and_overlap" (keeps only if center is inside AND area ratio >= min_area_ratio), "center" (only if center is inside), or "overlap" (only if area ratio >= min_area_ratio).

min_box_size

Minimum width/height in pixels for a cropped bounding box in the tile (default: 4).

mask_truncated

Logical. If TRUE (default), objects truncated by the tile boundary that fail the preservation criterion (i.e. are excluded from the tile's .txt labels because their preserved area is below min_area_ratio and/or their center falls outside the tile) have their remaining visible pixels in the tile image masked out with bg_col. This prevents unannotated partial objects on tile edges from acting as false-negative background penalties during YOLO training.

include_empty

Logical. Whether to generate empty .txt files and keep background tiles containing no objects (default: TRUE).

max_bg_ratio

Optional numeric fraction. Maximum ratio of background tiles relative to object tiles. If NULL (default), keeps all background tiles.

split_ratio

Numeric vector of length 3 giving c(train, val, test) proportions (default: c(0.70, 0.20, 0.10)). Ignored if the source dataset is already partitioned into splits.

pad

Logical. If an image is smaller than tile_size, whether to pad it with bg_col to tile_size (default: TRUE).

bg_col

Background color for padding when pad = TRUE (default: "black").

class_names

Character vector of class names. If NULL (default), auto-detected from source data.yaml or set to c("object").

seed

Optional integer seed for reproducible splitting (default: 42).

as_job

Logical. If TRUE and running inside RStudio, runs the dataset tiling as an RStudio Background Job in the Jobs tab, keeping the current R session free. Defaults to FALSE.

verbose

Logical. Whether to print progress and statistics. Defaults to TRUE.

progress

Logical. Whether to show a CLI progress bar. Defaults to TRUE.

...

Additional arguments.

parallel

Logical. If TRUE, source images are tiled on parallel workers (mirai daemons, one OpenMP thread each) when there are at least 16 of them; the tiles are the same as with sequential processing. Defaults to FALSE: the sequential path is already fast, and parallel workers help mainly on machines with many cores and fast (SSD) disks. With max_bg_ratio, the limit on background tiles is applied over the whole dataset after tiling, by removing a random (reproducible with seed) selection of the extra background tiles.

workers

Integer number of parallel workers. If NULL (default), 60% of the available cores are used.

Value

A yolo_dataset_tiled object (inheriting from yolo_dataset) pointing to the generated dataset directory (or the RStudio Job ID invisibly when as_job = TRUE).

Examples

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

# 1. Tile a high-resolution YOLO dataset with 20% overlap
tiled <- yolo_dataset_tiling("highres_dataset",
                             out_dir = "tiled_dataset",
                             tile_size = 640,
                             overlap = 0.20)
plot(tiled)

# 2. Uniform 5x5 grid per image with 50% minimum area preservation
tiled_5x5 <- yolo_dataset_tiling("highres_dataset",
                                 out_dir = "tiled_5x5",
                                 tile_size = 640,
                                 n_tiles = c(5, 5),
                                 min_area_ratio = 0.50)

# 3. Tile a subset of images (e.g. 50 images or 20% from a large 500-image dataset)
tiled_sub <- yolo_dataset_tiling("highres_dataset",
                                 out_dir = "tiled_sub",
                                 prop = 0.20,
                                 tile_size = 640)
}