yolo_benchmark() measures how fast a YOLO ONNX model runs on the current machine,
separating the raw C++ inference path (run_yolo_cpp(): letterbox + ONNX Runtime +
decoding/NMS) from the complete R pipeline of image_detect_dl() (model lookup,
post-processing and result tables). Optionally it also times tiled inference with
image_detect_sahi().
Warm-up iterations are run first and excluded from the timings, so the first-call costs (session creation, graph optimisation and, on GPU, DirectML shader compilation) do not distort the averages.
Each iteration is timed in C++ with std::chrono::steady_clock, a monotonic clock
with sub-microsecond resolution, instead of base::proc.time(), which on Windows only
advances in steps of about 10-16 ms. The measured resolution of the clock is stored in
the "clock_resolution_ns" attribute of the result.
Usage
yolo_benchmark(
model = "yolo26n",
img = NULL,
imgsz = NULL,
engine = c("gpu", "cpu"),
threads = 0,
n = 20,
warmup = 3,
device_id = -1,
sahi = FALSE,
conf_threshold = 0.25,
verbose = TRUE
)Arguments
- model
Character. Model name (e.g.
"yolo26n") or path to a.onnxfile.- img
Image used for timing: an
imageobject, a file path orNULL(default), which uses the example image"farm.jpg"(a scene with people, animals and vehicles).- imgsz
Optional integer inference size.
NULLuses the model's native size.- engine
Execution engine:
"gpu"(DirectML, Windows) or"cpu".- threads
Integer. ONNX Runtime CPU threads (
0= runtime default).- n
Integer. Number of timed iterations per stage.
- warmup
Integer. Untimed warm-up iterations before each stage.
- device_id
Integer GPU index (
-1= adapter with the most VRAM).- sahi
Logical. Also time
image_detect_sahi()with 640 px slices.- conf_threshold
Confidence threshold used during timing.
- verbose
Logical. Print a summary.
Value
A data.frame (class pliman_benchmark) with one row per stage and columns
stage, ms_mean, ms_sd, ms_min, fps and n_objects. Attributes hold the
model file, engine, threads, image size, the active ONNX sessions and the measured
clock resolution in nanoseconds ("clock_resolution_ns").
Details
Speed depends heavily on the hardware and on the model input size (imgsz). Some
practical notes:
engine = "gpu"uses DirectML (Windows / DirectX 12). Gains depend on the GPU; integrated GPUs may be slower than a multi-core CPU for small models.FP16 models (
yolo_export_fp16()) are faster only on GPUs with native half-precision throughput (e.g. NVIDIA Tensor Cores, RTX 20xx and newer). On GPUs without fast FP16 (e.g. GTX 10xx/16xx) or on CPU, the casting overhead can make them slower than FP32.Dynamic INT8 models (
onnx_quantize()) target CPU execution; on DirectML they may fall back to slower kernels.With
engine = "cpu",threadscontrols the ONNX Runtime intra-op threads (0= all physical cores).
Examples
if (FALSE) { # interactive()
library(pliman)
farm <- image_pliman("farm.jpg")
yolo_benchmark("yolo26n", img = farm, engine = "cpu")
yolo_benchmark("yolo26n", img = farm, engine = "gpu", sahi = TRUE)
}
