Skip to contents

yolo_dataset_balance() balances class distributions across all YOLO dataset types (classification, object detection, instance segmentation, and OBB) to eliminate majority class bias.

  • For object detection and instance segmentation (task %in% c("detect", "segment", "obb")):

    • strategy = "copypaste" (default): Uses state-of-the-art instance-level Copy-Paste augmentation to extract instances of minority classes and blend them into training images until all classes reach target_count.

    • strategy = "undersample": Downsamples images containing exclusively majority classes.

  • For image classification (task == "classify"):

    • strategy = "oversample": Balances folder images via geometric/photometric augmentation.

    • strategy = "undersample": Downsamples majority class folders to target_count.

Usage

yolo_dataset_balance(
  data,
  strategy = c("copypaste", "oversample", "undersample"),
  target_count = NULL,
  classes = NULL,
  clean = TRUE,
  seed = 42,
  as_job = FALSE,
  verbose = TRUE,
  ...
)

Arguments

data

A yolo_dataset object, yolo_dataset_cls object, or directory path.

strategy

Strategy to apply: "copypaste" (default for detection/segmentation), "oversample", or "undersample".

target_count

Optional integer target count per class in train/. If NULL, uses the maximum class count for oversampling/copy-paste and the minimum count for undersampling.

classes

Optional character vector or integer IDs of specific classes to balance.

clean

Logical. If TRUE (default), cleans previously balanced files (_bal*.ext, _copypaste*.ext) before computing class distributions.

seed

Optional random seed (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).

...

Additional arguments forwarded to yolo_dataset_copypaste().

Value

A yolo_dataset object with balanced class distributions.

Examples

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

# Balance detection/segmentation dataset using instance Copy-Paste
ds_balanced <- yolo_dataset_balance("my_yolo_dataset")
}