Skip to contents

Classifies images using pre-trained YOLO26 classification models across 1,000 ImageNet categories, or open-vocabulary text candidates using OpenAI CLIP (ViT-B/32) cross-modal embeddings and cosine similarity softmax.

Usage

image_classify_dl(
  img,
  candidates = NULL,
  model = NULL,
  top_k = NULL,
  labels = NULL,
  format_prompt = TRUE,
  plot = FALSE,
  col = "#2b5c8f",
  threads = 0,
  engine = c("gpu", "cpu"),
  device_id = -1,
  verbose = TRUE,
  dir = pliman_model_dir(),
  ...
)

Arguments

img

An Image object, a 2D/3D numeric array, a path to an image file, or a list of images.

candidates

An optional character vector of candidate class names or natural language descriptions. When supplied, performs zero-shot open-vocabulary classification using CLIP. Defaults to NULL.

model

Model name or path to a custom ONNX file. Defaults to "yolo26n-cls" when candidates = NULL, or "clip-vit-b32" when candidates is provided.

top_k

Integer. Number of top class predictions to return (defaults to 5 for YOLO models, or length(candidates) for CLIP zero-shot classification).

labels

Optional character vector of class names (defaults to 1,000 standard ImageNet classes).

format_prompt

Logical. If TRUE (default), candidate strings that do not contain "photo" or "a " are automatically formatted as "a photo of a {candidate}" for optimal CLIP zero-shot accuracy.

plot

Logical. Whether to display the image with top predicted class labels (or horizontal bar chart for CLIP).

col

Fill color for probability bar chart when candidates is supplied (default "#2b5c8f").

threads

Number of CPU threads (default 0 for auto-tuning).

engine

Execution engine: "cpu" or "gpu" (DirectML).

device_id

GPU device ID (default -1 for auto).

verbose

Logical. Whether to show progress messages.

dir

Model directory (default pliman_model_dir()).

...

Additional arguments passed to graphics::plot() or graphics::barplot().

Value

A data frame containing top predictions.

Examples

if (FALSE) { # interactive()
  library(pliman)
  img <- image_import("sample.png")
  # YOLO classification
  image_classify_dl(img)

  # CLIP zero-shot classification
  res <- image_classify_dl(img, candidates = c("healthy leaf", "rust lesion", "insect damage"))
  print(res)
}