Skip to contents

yolo_fewshot_fit() implements an interactive few-shot visual learning engine powered by Meta AI's Segment Anything 2.1 (SAM 2.1 / PerSAM). The user selects 3–5 sample instances of an object (e.g. seeds, beans, logs, leaves, lesions, fruits) by clicking on an image.

Using the PerSAM exemplar matching engine, the function computes visual prototypes from the exemplars and segments all other matching instances across the entire image with high precision.

The user can specify task = "segment" to obtain instance masks and polygonal boundaries for YOLO segmentation datasets, or task = "detect" for oriented/axis-aligned bounding boxes.

The resulting model object (yolo_fewshot_model) can be inspected, plotted, and directly propagated to hundreds of unannotated images in a folder using yolo_dataset_autolabel().

Usage

yolo_fewshot_fit(
  img,
  points = NULL,
  sample = NULL,
  label = "object",
  task = c("segment", "detect"),
  conf_threshold = 0.5,
  radius = NULL,
  min_dist = 12,
  min_area = NULL,
  max_area = NULL,
  max_objects = NULL,
  method = c("sam", "persam", "adaptive"),
  engine = c("gpu", "cpu"),
  device_id = -1,
  rainbow = TRUE,
  show_class = TRUE,
  show_conf = TRUE,
  badge = TRUE,
  interactive = base::interactive(),
  plot = TRUE,
  verbose = TRUE,
  ...
)

Arguments

img

An image object, file path, or matrix.

points, sample

Optional $(x, y)$ coordinates of exemplar points. If NULL (default), an interactive plotting window opens for the user to click on 3–5 representative objects.

label

Character vector naming the target class(es). Can be a single class (e.g., "bean", "leaf", "seed", "tora") or a character vector of multiple classes (e.g., c("leaf", "lesion"), c("healthy", "damaged")). Defaults to "object". When multiple classes are specified, yolo_fewshot_fit() interactively collects exemplars for each class in sequence, or uses the respective elements from points if a list is provided.

task

Target task: "segment" (instance segmentation polygons and masks) or "detect" (bounding boxes). Defaults to "segment".

conf_threshold

Numeric scalar between 0 and 1 specifying detection confidence/similarity threshold. Defaults to 0.50 (or 0.25 for adaptive).

radius

Optional estimated radius in pixels for target objects. If NULL (default), automatically detected from exemplar edge gradients.

min_dist

Minimum distance in pixels between detected instances. Defaults to 12.

min_area

Minimum object area in pixels. If NULL (default), automatically calibrated from the exemplar scale (0.20 * median_exemplar_area) to filter out background noise.

max_area

Maximum object area in pixels. If NULL (default), automatically calibrated (3.80 * median_exemplar_area).

max_objects

Maximum number of instances to detect. Defaults to NULL (no limit).

method

Segmentation method: "sam" (or "persam", Meta Segment Anything 2.1 via ONNX) or "adaptive" (fast color-texture morphology). Defaults to "sam".

engine

Execution engine for SAM: "cpu" or "gpu". Defaults to "cpu".

device_id

GPU device ID if engine = "gpu". Defaults to -1.

rainbow

Logical. If TRUE (default), plots instances in distinct rainbow colors.

show_class

Logical. Whether to display class name on labels/badges. Defaults to TRUE.

show_conf

Logical. Whether to display confidence score on labels/badges. Defaults to TRUE.

badge

Logical. Whether to draw a filled background badge behind labels. Defaults to TRUE.

interactive

Logical. Whether to enable interactive review and refinement after exemplar segmentation (allows adding missed objects, removing false positives, adjusting threshold, or resampling). Defaults to interactive().

plot

Logical. Display the detection and segmentation overlay. Defaults to TRUE.

verbose

Logical. Show progress messages. Defaults to TRUE.

...

Additional arguments passed to downstream methods or aliases (e.g., show_labels, show_scores, label_box).

Value

An object of class yolo_fewshot_model containing exemplar signatures, detected instances on the reference image, and parameters for batch propagation.

Examples

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

# 1. Fit exemplar model on reference photo (clicks or supplied coordinates)
yolo_fewshot_fit("img.jpg",
                 label = "tora",
                 task = "segment",
                 method = "sam")
}