
Evaluate and Validate a Trained YOLO Model on a Test Dataset
Source:R/utils_yolo_test.R
yolo_model_test.Rdyolo_model_test() (and its alias yolo_test()) evaluates an object detection or
instance segmentation YOLO model on an independent test dataset (or validation split).
It applies batch inference using image_detect_dl_batch() (or image_segment_dl_batch()),
matches predictions against ground-truth bounding boxes using greedy IoU bipartite matching,
and computes key computer vision and counting metrics:
Detection Performance: Precision, Recall, F1-Score, mAP@50, mAP@75, mAP@50:95, and mean IoU.
Counting Validation: Ground Truth vs. Predicted counts per image, Mean Absolute Error (MAE), Root Mean Squared Error (RMSE), and coefficient of determination (\(R^2\)).
Confusion Matrix: Multi-class and single-class confusion matrix, including background False Positives and False Negatives.
Diagnostic Curves: Precision-Recall (PR) curve, F1-Confidence curve, and IoU score distribution.
Usage
yolo_model_test(
model,
data = "yolo_dataset",
split = c("test", "val", "train"),
task = NULL,
conf_threshold = 0.25,
iou_threshold = 0.5,
iou_range = seq(0.5, 0.95, by = 0.05),
max_det = 500L,
classes = NULL,
parallel = FALSE,
verbose = TRUE,
...
)
yolo_test(
model,
data = "yolo_dataset",
split = c("test", "val", "train"),
task = NULL,
conf_threshold = 0.25,
iou_threshold = 0.5,
iou_range = seq(0.5, 0.95, by = 0.05),
max_det = 500L,
classes = NULL,
parallel = FALSE,
verbose = TRUE,
...
)Arguments
- model
A trained model identifier. Can be:
An object of class
yolo_train(fromyolo_train()).Path to a trained
.onnxor.ptweights file (e.g.,"best.pt","best.onnx").A pre-trained model name (e.g.,
"yolo11s","yolo26n"). If a.ptweights file is provided,yolo_export()is automatically invoked if the corresponding.onnxfile is not already found.
- data
Character string or
yolo_datasetobject. Path to the YOLO dataset root directory (containingimages/andlabels/, ordata.yaml). Default is"yolo_dataset".- split
Character string. Dataset split to evaluate:
"test"(default),"val", or"train". If"test"is selected but the directory contains no images, gracefully falls back to"val".- task
Character string. Computer vision task:
"detect"(default) or"segment". IfNULL, auto-detected from the model name or dataset annotations.- conf_threshold
Numeric. Confidence score threshold for candidate detections (default
0.25).- iou_threshold
Numeric. IoU overlap threshold for matching predictions to ground-truth objects (default
0.5).- iou_range
Numeric vector. Sequence of IoU thresholds used to calculate mAP50:95. Default is
seq(0.5, 0.95, by = 0.05).- max_det
Integer. Maximum number of detections permitted per image. Defaults to
500Lto prevent truncating dense seed/capsule populations.- classes
Optional character or numeric vector of class names or IDs to evaluate. If
NULL(default), all classes present in the dataset are evaluated.- parallel
Logical. Whether to run batch inference in parallel using mirai. Defaults to
FALSE.- verbose
Logical. Whether to show progress bars, timing, and formatted CLI summaries. Defaults to
TRUE.- ...
Additional arguments passed to
image_detect_dl_batch()orimage_segment_dl_batch().
Value
An S3 object of class c("yolo_test", "list") containing:
metrics: A single-rowdata.framewith overall benchmark statistics:total_images,total_gt,total_pred,tp,fp,fn,precision,recall,f1,mean_iou,map50,map75,map50_95,mae,rmse,r2,slope,intercept.by_class: Adata.frameof per-class detection metrics (Support, Precision, Recall, F1, AP50).by_image: Adata.framecomparing ground-truth counts vs. predicted counts, differences, TP, FP, and FN per image.confusion_matrix: A confusion matrixdata.frameof counts (Ground Truth vs. Predicted, including Background).confusion_matrix_norm: Normalized confusion matrix (by actual ground truth row).pr_curve: Adata.framecontaining recall and precision coordinates for plotting PR curves.f1_curve: Adata.framecontaining confidence thresholds and corresponding F1 scores.iou_distribution: A numeric vector of exact IoU overlap values for all matched True Positives.predictions: Consolidateddata.frameof raw predictions fromimage_detect_dl_batch().info: A list of execution metadata (model, split, dataset path, timestamp, task).
Examples
if (FALSE) { # interactive()
library(pliman)
# 1. Test model on the independent test split
test_res <- yolo_model_test(
model = "capsulas_yolo11s/weights/best.pt",
data = "dataset_capsulas",
split = "test"
)
# 2. View summary metrics
print(test_res)
# 3. Generate diagnostic dashboard plots
plot(test_res, which = "all")
plot(test_res, which = "counts")
plot(test_res, which = "confusion")
}