
Batch Object Detection with Slicing Aided Hyper Inference (SAHI)
Source:R/utils_dl_batch.R
image_detect_sahi_batch.RdRuns Slicing Aided Hyper Inference (image_detect_sahi()) on a batch of images
defined by a directory path, a filename pattern, a vector of file paths,
or a list of Image objects. Ultra-high-resolution images (such as drone orthophotos,
aerial mapping surveys, and macro scans) are dynamically sliced into overlapping tiles
of size slice_width x slice_height, deep learning inference is executed per slice,
and all candidate boxes are transformed into global image coordinates and unified
using Non-Maximum Merging (Greedy NMM) or standard NMS.
Supports sequential or parallel processing (via mirai), optional RStudio Background Job execution,
saving annotated detection images to disk, and returns a consolidated predictions dataset
along with per-image and per-class summaries.
Usage
image_detect_sahi_batch(
data,
pattern = NULL,
path = NULL,
slice_height = 640,
slice_width = 640,
overlap_height_ratio = 0.2,
overlap_width_ratio = 0.2,
parallel = FALSE,
show_point = FALSE,
keep_images = FALSE,
save_image = FALSE,
dir_processed = "./proc",
prefix = "proc_",
verbose = TRUE,
as_job = FALSE,
...
)
image_sahi_batch(
data,
pattern = NULL,
path = NULL,
slice_height = 640,
slice_width = 640,
overlap_height_ratio = 0.2,
overlap_width_ratio = 0.2,
parallel = FALSE,
show_point = FALSE,
keep_images = FALSE,
save_image = FALSE,
dir_processed = "./proc",
prefix = "proc_",
verbose = TRUE,
as_job = FALSE,
...
)
image_sahi_detect_batch(
data,
pattern = NULL,
path = NULL,
slice_height = 640,
slice_width = 640,
overlap_height_ratio = 0.2,
overlap_width_ratio = 0.2,
parallel = FALSE,
show_point = FALSE,
keep_images = FALSE,
save_image = FALSE,
dir_processed = "./proc",
prefix = "proc_",
verbose = TRUE,
as_job = FALSE,
...
)Arguments
- data
Input data. Can be:
A character string representing a directory path containing images (e.g.,
"drone_orthos").A character pattern to match image files in
pathor the current directory (e.g.,"-","^img_").A character vector of image file paths.
A list of
Imageobjects or numeric arrays.
- pattern
Optional regex pattern to filter image filenames when
datais a directory path.- path
Optional directory path to search when
datais a pattern. Defaults to".".- 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.- parallel
Logical. If
TRUE, runs inference in parallel across images usingmirai. Defaults toFALSE.- show_point
Logical. Whether to show a point at the centroid of each detected object. Defaults to
FALSE.- keep_images
Logical. Whether to keep the full original images attached as attributes in
$results. Defaults toFALSEto prevent excessive RAM consumption during large batches (saving gigabytes of memory).- save_image
Logical. Whether to save annotated prediction images with bounding boxes to disk. Defaults to
FALSE.- dir_processed
Character string. Directory where annotated images should be saved when
save_image = TRUE. Defaults to"./proc".- prefix
Character string. Prefix prepended to saved output filenames. Defaults to
"proc_".- verbose
Logical. Whether to show progress bars, timing, and formatted CLI summaries. Defaults to
TRUE.- as_job
Logical. If
TRUEand running inside RStudio, runs the batch detection process in an RStudio Background Job. Defaults toFALSE.- ...
Additional arguments passed to
image_detect_sahi()(e.g.,model,conf_threshold,iou_threshold,match_metric,postprocess_type,match_threshold,classes,agnostic_nms,seam_merging,full_image_inference,lower_size,upper_size,size_mode,cex,badge,rainbow,lwd).
Value
An S3 object of class c("sahi_detect_batch", "dl_detect_batch", "list") containing:
predictions: A consolidateddata.frameof all detected bounding boxes across all images, with columnsimg,id,xmin,ymin,xmax,ymax,label,score.summary: A per-imagedata.framewith columnsimg,num_objects,mean_score, and per-class counts.class_summary: A dataset-level summary table of counts and proportions per detected class.results: A named list of raw individual detection outputs fromimage_detect_sahi().n_images: Total number of images processed.elapsed: Processing time in seconds.parallel: Logical indicating whether parallel execution was used.
Performance
The model is resolved once and every image reuses the cached ONNX session. With
parallel = TRUE each worker process loads its own copy of the model, so:
on CPU (
engine = "cpu") andthreads = 0, each worker getsphysical cores / workersONNX threads to avoid oversubscribing the CPU;on GPU (
engine = "gpu") concurrency is capped atgetOption("pliman.gpu_workers", 2), because every worker opens its own GPU session (GPU memory grows with the number of workers);temporary workers are started and stopped on every call. Start persistent workers with
pliman_daemons()to keep models loaded across calls;pass file paths rather than in-memory images, so only paths are sent to the workers.
Examples
if (FALSE) { # interactive()
library(pliman)
# 1. Batch SAHI detection across a folder of drone orthophotos
res <- image_detect_sahi_batch(
data = "drone_flights",
model = "seedlings.onnx",
slice_width = 640,
slice_height = 640,
conf_threshold = 0.35
)
head(res$summary)
head(res$predictions)
# 2. Parallel SAHI detection using 4 workers
res_par <- image_detect_sahi_batch(
data = "drone_flights",
model = "seedlings.onnx",
parallel = TRUE
)
}