
Slicing Aided Hyper Inference (SAHI) for Object Detection
Source:R/utils_yolo_sahi.R
image_detect_sahi.Rdimage_detect_sahi() performs high-resolution sliding-window hyper inference
on ultra-large images, drone orthomosaics, and macro scans. Large images (e.g. 20–50 Megapixels)
are dynamically sliced into overlapping tiles of size slice_width x slice_height.
Deep learning inference is executed per slice, and all bounding boxes are transformed to global coordinates and merged using Greedy Non-Maximum Merging (Greedy NMM) or standard NMS. This prevents small objects (seeds, aphids, stomata, seedlings) from vanishing when downscaled, and seamlessly unifies objects that cross tile seams.
Usage
image_detect_sahi(
img,
model = "yolo26n",
slice_height = 640,
slice_width = 640,
overlap_height_ratio = 0.2,
overlap_width_ratio = 0.2,
conf_threshold = 0.25,
iou_threshold = 0.45,
match_metric = c("IOS", "IOU"),
postprocess_type = c("GREEDY_NMM", "NMS"),
match_threshold = 0.5,
classes = NULL,
agnostic_nms = FALSE,
cross_class_iou = 0.7,
seam_merging = TRUE,
full_image_inference = TRUE,
lower_size = NULL,
upper_size = NULL,
size_mode = c("relative", "absolute"),
cex = 1,
pad = 1,
bbox = TRUE,
show_box = NULL,
show_point = FALSE,
point_size = 1.5,
point_pch = 16,
point_col = NULL,
show_text = TRUE,
show_conf = TRUE,
show_class = TRUE,
show_id = FALSE,
badge = TRUE,
rainbow = TRUE,
plot = TRUE,
verbose = TRUE,
imgsz = NULL,
...
)Arguments
- img
An
imageobject, file path, or 3D array.- model
YOLO ONNX model name or file path (e.g.,
"yolo26n","yolov8s.onnx").- slice_height
Integer. Height of sliding window slices. Defaults to
640.- slice_width
Integer. Width of sliding window slices. Defaults to
640.- overlap_height_ratio
Numeric scalar between 0 and 0.5. Height overlap fraction. Defaults to
0.20.- overlap_width_ratio
Numeric scalar between 0 and 0.5. Width overlap fraction. Defaults to
0.20.- conf_threshold
Numeric scalar between 0 and 1. Minimum detection confidence. Defaults to
0.25.- iou_threshold
Numeric scalar between 0 and 1. Threshold for cross-slice matching. Defaults to
0.45.- match_metric
Character. Overlap metric:
"IOS"(Intersection over Smaller area, default) or"IOU"(Intersection over Union)."IOS"is strongly recommended for SAHI because it naturally merges fragments and sub-slices with larger parent detections.- postprocess_type
Character. Post-processing merge strategy:
"GREEDY_NMM"(default, Non-Maximum Merging, unifying overlapping pieces into single encompassing boxes) or"NMS"(standard Non-Maximum Suppression). With"GREEDY_NMM", merging is repeated over the merged boxes until no more boxes can be merged, so the pieces of an object cut by several slices end up in a single box.- match_threshold
Numeric scalar between 0 and 1. Threshold for merging candidates. Defaults to
0.50.- classes
Optional character or integer vector of class names or IDs to keep (e.g.,
"car"orc("car", "truck")). Detections belonging to other classes are discarded before post-processing. Defaults toNULL(all classes kept).- agnostic_nms
Logical. If
TRUE, bounding boxes and masks from different classes compete during post-processing suppression. Defaults toFALSE.- cross_class_iou
Numeric in
(0, 1). Merging is done separately for each class, so the same object detected with different classes in different slices (e.g. a pickup as"car"in one slice and"truck"in another) would produce two overlapping boxes. After merging, boxes of different classes whose IoU is at leastcross_class_iouare treated as the same object and only the most confident one is kept. The default (0.7) removes these duplicates while keeping different objects that overlap, such as a person riding a motorcycle. Set toNULLor1to disable. Ignored whenagnostic_nms = TRUE.- seam_merging
Logical. If
TRUE(default), seamlessly detects and merges objects that have been sliced in half across tile cut boundaries.- full_image_inference
Logical. If
TRUE(default), runs an additional detection pass on the resized full canvas to capture objects larger than individual slices.- lower_size
Optional numeric value to filter out small detected objects. When
size_mode = "relative"(default), objects with a bounding box area less thanlower_sizetimes the mean area of all candidate boxes are removed (e.g.,lower_size = 0.2removes objects smaller than 20% of the average object size). Whensize_mode = "absolute", objects with an area (in pixels) belowlower_sizeare removed. Defaults toNULL(no lower size filter).- upper_size
Optional numeric value to filter out abnormally large detected objects. When
size_mode = "relative"(default), objects with a bounding box area greater thanupper_sizetimes the mean area of all candidate boxes are removed (e.g.,upper_size = 2.0removes objects larger than 200% of the average object size). Whensize_mode = "absolute", objects with an area (in pixels) aboveupper_sizeare removed. Defaults toNULL(no upper size filter).- size_mode
Character string specifying how
lower_sizeandupper_sizeare interpreted."relative"(default) treats values as proportions/multiples of the mean bounding box area."absolute"treats values as area in pixels.- cex
Numeric character expansion for labels and badges. Defaults to
1.0.- pad
Numeric padding around bounding box labels or badges. Defaults to
1.0.- bbox
Logical. If
TRUE(default), draws bounding boxes.- show_box
Logical. Explicitly control whether bounding boxes are drawn. Defaults to
TRUE(or value ofbbox). Supported aliases:box,boxes,show_boxes.- show_point
Logical. If
TRUE, draws points at the centroid of each detected object. Defaults toFALSE. Supported aliases:points,point,show_points.- point_size
Numeric. Size (cex) of centroid points when
show_point = TRUE. Defaults to1.5. Supported aliases:marker_size,point_cex.- point_pch
Integer. Symbol code for centroid points. Defaults to
16(filled circle). Supported alias:marker_pch.- point_col
Color for centroid points. Defaults to
NULL(uses matching box/instance color). Supported alias:marker_col.- show_text
Logical. Display text labels on bounding boxes. Defaults to
TRUE.- show_conf
Logical. Display confidence score on labels/badges. Defaults to
TRUE.- show_class
Logical. Display class name on labels/badges. Defaults to
TRUE.- show_id
Logical. Display instance ID number. Defaults to
FALSE.- badge
Logical. Display rounded badge background behind text labels. Defaults to
TRUE.- rainbow
Logical. If
TRUE(default), each object gets its own color. IfFALSE, objects of the same class share one color, with a different color for each class.- plot
Logical. Display global detections overlaid on the full image. Defaults to
TRUE.- verbose
Logical. Show progress bar and tile metrics. Defaults to
TRUE.- imgsz
Target input image resolution for YOLO inference (e.g.,
640,960, or1024). Default isNULL.- ...
Additional arguments passed to
image_detect_dl().col(a single color, a vector of colors, or a vector named by class) andlwd(box line width, default2) are also used to draw the plot.
Value
A data.frame of global bounding boxes (class image_detect_sahi, which also inherits from
image_detect_dl) with attributes image, counts, summary (text such as "54 cars, 10 trucks and 1 bus")
and plot_params. Use plot.image_detect_sahi() to
re-draw it with other visual settings.
Performance
For YOLO detection, pose and OBB models the slices (and the optional full-image pass)
are cropped, letterboxed and inferred in a single C++ call, with the model resolved
once. YOLO-World, classification models and return_features = TRUE use the
per-slice image_detect_dl() path. The cost grows with the number of slices, which
depends on slice_height, slice_width and the overlap ratios.