
Slicing Aided Hyper Inference (SAHI) for Instance Segmentation
Source:R/utils_yolo_sahi.R
image_segment_sahi.RdPerforms high-resolution sliding-window hyper inference for instance segmentation
using image_segment_dl(). Slices large canvases, predicts masks per tile,
shifts polygon contours to global coordinates, and applies Greedy Non-Maximum Merging (Greedy NMM)
with IOS and slice seam unification so objects cut across slice borders are cleanly assembled into complete objects.
Usage
image_segment_sahi(
img,
model = "yolo26n-seg",
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,
type = c("highlight", "segment", "mask"),
col_background = "white",
col_highlight = "salmon",
alpha = 0.4,
border = NA,
lwd = 1,
bbox = FALSE,
show_box = NULL,
show_point = FALSE,
point_size = 1.5,
point_pch = 16,
point_col = NULL,
mask = TRUE,
lower_size = NULL,
upper_size = NULL,
size_mode = c("relative", "absolute"),
cex = 1,
pad = 1,
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.- type
Type of presentation:
"highlight"(default, overlays translucent colored masks on original image),"segment"(draws polygon borders), or"mask"(binary/label mask).- col_background
Color of background when
type = "segment". Defaults to"white".- col_highlight
Fill color for highlighted instances when
rainbow = FALSE: a single color, a vector of colors, or a vector named by class (e.g.c(car = "red", truck = "blue")). When not given, each class gets its own color ("salmon"when there is a single class, or for classes missing from a named vector). Defaults to"salmon".- alpha
Numeric transparency factor (0 = fully transparent, 1 = fully opaque) for highlight fill. Defaults to
0.40.- border
Color for instance polygon borders. Defaults to
NA(no border).- lwd
Numeric border line width. Defaults to
1. Whenlwdis not given, bounding boxes are drawn with width2; when it is given, the same width is used for both.- bbox
Logical. If
TRUE, draws bounding boxes and class badges around segmented instances. Defaults toFALSE.- show_box
Logical. Explicitly control whether bounding boxes are drawn. Defaults to
NULL(or value ofbbox). Supported aliases:box,boxes,show_boxes.- show_point
Logical. If
TRUE, draws points at the centroid of each segmented 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 instance color). Supported alias:marker_col.- mask
Logical. If
TRUE(default), draws instance segmentation masks.- 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 factor for badges. Defaults to
1.0.- 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.