
Balance Class Distributions via Copy-Paste, Augmentation or Downsampling
Source:R/utils_yolo_dataset.R
yolo_dataset_balance.Rdyolo_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 reachtarget_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 totarget_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_datasetobject,yolo_dataset_clsobject, 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/. IfNULL, 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
TRUEand 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().