
Explain YOLO Model Detections via Perturbation Saliency Heatmaps
Source:R/utils_yolo_diagnostics.R
yolo_explain.Rdyolo_explain() generates visual saliency attribution heatmaps for object detection
predictions by perturbing (occluding/blurring) spatial image patches and measuring the
resulting drop in detection confidence score.
Visualizing which image regions (e.g., embryo vs endosperm, leaf margin vs lesion center) most strongly contribute to the model's confidence is essential for opening the deep learning "black box", confirming model robustness, and satisfying rigorous review criteria in Q1 journals.
Usage
yolo_explain(
img,
model = "yolo26n",
target = NULL,
grid_size = 24L,
stride = NULL,
perturb_type = c("blur", "mean", "zero"),
target_region = c("box", "full"),
col_palette = "inferno",
alpha = 0.55,
conf_threshold = 0.2,
iou_threshold = 0.45,
engine = "gpu",
plot = TRUE,
verbose = TRUE,
...
)
yolo_saliency(
img,
model = "yolo26n",
target = NULL,
grid_size = 24L,
stride = NULL,
perturb_type = c("blur", "mean", "zero"),
target_region = c("box", "full"),
col_palette = "inferno",
alpha = 0.55,
conf_threshold = 0.2,
iou_threshold = 0.45,
engine = "gpu",
plot = TRUE,
verbose = TRUE,
...
)Arguments
- img
An
Imageobject or character string path to an image file.- model
A trained YOLO model identifier or ONNX file path (e.g.,
"yolo26n").- target
Optional integer or character string specifying which detection to explain:
NULL(default): Explains the detection with the highest confidence score.An integer
1, 2, ...: Explains the detection at that rank.A character string: Explains the highest confidence detection of that class name.
- grid_size
Integer. Size in pixels of occlusion patches. Defaults to
24L.- stride
Optional integer. Stride between adjacent patches. Defaults to
grid_size / 2for smooth attribution maps.- perturb_type
Character string specifying how patches are masked:
"blur"(default): Blurs the patch to eliminate high-frequency edges and textures."mean": Replaces the patch with the mean pixel color of the image."zero": Sets the patch pixels to 0 (black).
- target_region
Character string:
"box"(default): Restricts perturbations to the target object's bounding box (+15% margin) for 20x faster computation."full": Sweeps the entire image canvas.
- col_palette
Character string. Name of the color palette for the attribution heatmap:
"inferno"(default),"viridis","plasma", or"magma".- alpha
Numeric. Opacity of the heatmap overlay on the original image (0 to 1). Defaults to
0.55.- conf_threshold
Numeric. Baseline candidate detection threshold. Defaults to
0.20.- iou_threshold
Numeric. NMS threshold. Defaults to
0.45.- engine
Character string. Compute engine:
"gpu"(default) or"cpu".- plot
Logical. Whether to render the saliency visualization. Defaults to
TRUE.- verbose
Logical. Whether to display progress messages. Defaults to
TRUE.- ...
Additional arguments forwarded to
image_detect_dl().
Value
An S3 object of class c("yolo_saliency", "list") containing:
heatmap: A normalized 2D matrix of attribution values \([0, 1]\).target_box: Bounding box coordinates of the explained object.target_class: Predicted class label of the explained object.baseline_conf: Baseline prediction confidence before perturbation.image: The original image object.col_palette: Heatmap color palette used.alpha: Heatmap transparency level.