
End-to-End YOLO Active Learning & Bootstrap Training Workflow
Source:R/utils_yolo_train.R
yolo_train_workflow.Rdyolo_train_workflow() implements a complete active-learning bootstrap pipeline:
Few-Shot Sampling: Selects a small subset of representative sample images (e.g., 3–4 images) from an unlabelled dataset directory.
Auto-Annotation via Few-Shot / Exemplar Matching: Uses
yolo_dataset_export()with Meta PerSAM (or Grounded-SAM) to accurately annotate the sampled images.Offline Augmentation: Optionally expands those few images with
yolo_dataset_augment()into 20–50+ diverse images (3D perspective warping, contrast CLAHE, random cutout, noise, blur, rotations).High-Resolution Tiling: Optionally slices images into uniform tiles with
yolo_dataset_tiling()(e.g. 640x640), optimizing small object resolution and dataset volume.Automated Training: Trains a high-performance YOLO model via
yolo_train().Model Export: Produces ready-to-deploy
.onnxand.ptmodel files that can be directly passed intoyolo_dataset_autolabel()to annotate thousands of remaining images in a few seconds!
Usage
yolo_train_workflow(
data,
sample_n = 4L,
sample_images = NULL,
model_fewshot = "persam",
class_names = "object",
task = c("detect", "segment"),
prompt = NULL,
prompt_mode = c("first", "each"),
sim_threshold = 0.7,
crop_to_objects = TRUE,
crop_margin = 0.1,
augment = TRUE,
augment_total = 30L,
augment_args = list(),
tiling = TRUE,
tile_size = 640L,
tiling_args = list(),
train_model = "yolo26n",
epochs = 50L,
batch = 8L,
device = "auto",
close_mosaic = 15L,
output_dir = pliman_model_dir(),
output_name = NULL,
temp_dir = NULL,
dataset_dir = NULL,
keep_dataset = TRUE,
as_job = FALSE,
review = FALSE,
show_class = FALSE,
show_conf = FALSE,
badge = FALSE,
interactive = is.null(prompt) && base::interactive(),
verbose = TRUE,
...
)Arguments
- data
Directory path containing unlabelled images (e.g.
"dataset_complex"), or a vector of image paths.- sample_n
Number of images to sample from
dataifsample_imagesis not provided (default4L).- sample_images
Optional vector of specific image paths or filenames to label. If
NULL(default), samplessample_nimages fromdata.- model_fewshot
Few-shot / exemplar model to label the sample images. Defaults to
"persam". Can also be"grounded-sam","ben2", or an existing model.- class_names
Names of the classes to detect. Defaults to
"object".- task
Target task:
"detect"(bounding boxes) or"segment"(polygons). Defaults to"detect".- prompt
Visual exemplar prompt points or bounding box for PerSAM, or text prompt for Grounded-SAM. If
NULL(default) andinteractive = TRUE, opens an interactive locator window on the first sampled image.- prompt_mode
Prompt reuse mode:
"first"(default, prompt once on image 1 and propagate to all sample images) or"each"(prompt on each sample image).- sim_threshold
Similarity threshold for PerSAM (default
0.70).- crop_to_objects
Logical. Whether to crop sampled images to object bounding boxes with margin. Defaults to
TRUE.- crop_margin
Margin proportion around objects when cropping (default
0.10).- augment
Logical. Whether to apply offline data augmentation (default
TRUE).- augment_total
Target total number of augmented training images (default
30L).- augment_args
List of custom arguments passed to
yolo_dataset_augment().- tiling
Logical. Whether to apply dataset tiling (default
TRUE).- tile_size
Tile size in pixels (default
640).- tiling_args
List of custom arguments passed to
yolo_dataset_tiling().- train_model
Base pre-trained YOLO architecture to train (default
"yolo26n", or"yolo11n","yolo26s").- epochs
Number of training epochs (default
50).- batch
Batch size (default
8L, or4Lon low-VRAM GPUs).- device
Device to train on (
"auto",0, or"cpu"). Defaults to"auto".- close_mosaic
Number of final epochs to disable mosaic augmentation (default
15).- output_dir
Target directory to save the final exported
.onnxand.ptmodel files. Defaults topliman_model_dir().- output_name
Filename for the exported model (e.g.
"model_capsulas.onnx"). IfNULL, an informative name based ondatais automatically generated.- temp_dir
Directory used to construct the bootstrap training dataset. If
NULL(default), creates a temporary folder insidetempdir().- dataset_dir
Optional character directory path to save or reuse the constructed YOLO training dataset. If
NULL(default), usestemp_diror creates a persistent directory whenkeep_dataset = TRUE.- keep_dataset
Logical. If
FALSE(default), removes the temporary bootstrap dataset after training.- as_job
Logical. If
TRUEand running inside RStudio, launches the entire workflow (bootstrapping, augmentation, tiling, and YOLO training) as an asynchronous RStudio Background Job, immediately freeing up the R console. Default isFALSE. IfTRUE, keeps the folder intact.- review
Logical. Whether to generate review preview images during few-shot labeling (default
FALSE).- show_class
Logical. Whether to display class names on labels/badges during review (default
FALSE).- show_conf
Logical. Whether to display confidence values on labels/badges during review (default
FALSE).- badge
Logical. Whether to draw a filled badge behind label text (default
FALSE).- interactive
Logical. Whether to allow interactive clicking/refinement (default
interactive()).- verbose
Logical. Whether to display informative progress messages (default
TRUE).- ...
Additional arguments passed to
yolo_train()(e.g.,scale = 0.35,degrees = 15).
Value
An object of class c("yolo_workflow", "yolo_train") containing:
model_file: Path to the exported.onnxmodel ready for inference.pt_file: Path to the PyTorch.ptmodel weights.classes: Class names.task: Task type ("detect"or"segment").sample_images: Paths of the sample images used for bootstrap labeling.dataset_dir: Path to the training dataset (ifkeep_dataset = TRUE).workflow: Summary list of workflow parameters and metadata.Plus all metrics, diagnostic plots, and summaries from
yolo_train().
Examples
if (FALSE) { # interactive()
library(pliman)
# 1. Run bootstrap workflow: label 4 sample images with PerSAM, augment to 30, tile 640, train YOLO
wf <- yolo_train_workflow(
data = "dataset_complex",
sample_n = 4,
model_fewshot = "persam",
class_names = "cápsula",
augment = TRUE,
augment_total = 30,
tiling = TRUE,
tile_size = 640,
train_model = "yolo26n",
epochs = 50,
output_name = "modelo_capsulas.onnx"
)
# 2. Automatically propagate trained model across ALL hundreds of images in the folder!
ds_full <- yolo_dataset_autolabel("dataset_complex", model = wf, output_dir = "dataset_full")
}