
Batch Instance and Semantic Segmentation with Deep Learning
Source:R/utils_dl_batch.R
image_segment_dl_batch.RdRuns deep learning segmentation models (e.g., YOLO-seg, Grounded-SAM, U2-Net, StarDist)
on a batch of images defined by a directory path, a filename pattern, a vector of file paths,
or a list of Image objects. Supports sequential or parallel processing (via mirai),
optional automated morphometric feature extraction (via dl_extract_phenotypes()),
and returns a consolidated predictions dataset along with per-image summaries.
Usage
image_segment_dl_batch(
data,
pattern = NULL,
path = NULL,
parallel = FALSE,
sahi = FALSE,
slice_height = 640,
slice_width = 640,
overlap_height_ratio = 0.2,
overlap_width_ratio = 0.2,
match_metric = "IOS",
match_threshold = 0.5,
postprocess_type = "GREEDY_NMM",
measure = TRUE,
pixel_size = NULL,
show_point = FALSE,
keep_images = FALSE,
save_image = FALSE,
dir_processed = "./proc",
prefix = "proc_",
verbose = TRUE,
imgsz = NULL,
as_job = FALSE,
...
)
image_segment_batch(
data,
pattern = NULL,
path = NULL,
parallel = FALSE,
sahi = FALSE,
slice_height = 640,
slice_width = 640,
overlap_height_ratio = 0.2,
overlap_width_ratio = 0.2,
match_metric = "IOS",
match_threshold = 0.5,
postprocess_type = "GREEDY_NMM",
measure = TRUE,
pixel_size = NULL,
show_point = FALSE,
keep_images = FALSE,
save_image = FALSE,
dir_processed = "./proc",
prefix = "proc_",
verbose = TRUE,
imgsz = NULL,
as_job = FALSE,
...
)
yolo_segment_batch(
data,
pattern = NULL,
path = NULL,
parallel = FALSE,
sahi = FALSE,
slice_height = 640,
slice_width = 640,
overlap_height_ratio = 0.2,
overlap_width_ratio = 0.2,
match_metric = "IOS",
match_threshold = 0.5,
postprocess_type = "GREEDY_NMM",
measure = TRUE,
pixel_size = NULL,
show_point = FALSE,
keep_images = FALSE,
save_image = FALSE,
dir_processed = "./proc",
prefix = "proc_",
verbose = TRUE,
imgsz = NULL,
as_job = FALSE,
...
)Arguments
- data
Input data. Can be:
A character string representing a directory path containing images (e.g.,
"flax_grains").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".".- parallel
Logical. If
TRUE, runs inference in parallel across images usingmirai. Defaults toFALSE.- sahi
Logical. If
TRUE, uses SAHI (Slicing Aided Hyper Inference) for tiled segmentation on large or high-resolution images. Defaults toFALSE.- slice_height
Integer. Height of sliding window slices when
sahi = TRUE. Defaults to640.- slice_width
Integer. Width of sliding window slices when
sahi = TRUE. Defaults to640.- overlap_height_ratio
Numeric scalar between 0 and 0.5. Vertical slice overlap fraction when
sahi = TRUE. Defaults to0.20.- overlap_width_ratio
Numeric scalar between 0 and 0.5. Horizontal slice overlap fraction when
sahi = TRUE. Defaults to0.20.- match_metric
Character string. Box merging overlap metric used when
sahi = TRUE("IOS"or"IOU"). Defaults to"IOS".- match_threshold
Numeric scalar. Overlap threshold for merging slice detections when
sahi = TRUE. Defaults to0.50.- postprocess_type
Character string. Post-processing algorithm used when
sahi = TRUE("GREEDY_NMM"or"NMS"). Defaults to"GREEDY_NMM".- measure
Logical. If
TRUE, automatically extracts granular morphometric, spectral, and geometric phenotypes for every segmented instance usingdl_extract_phenotypes(). Defaults toTRUE.- pixel_size
Optional numeric physical size per pixel (e.g.,
0.05for 0.05 cm/px) passed todl_extract_phenotypes().- show_point
Logical. Whether to show a point at the centroid of each segmented 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 segmented or highlight mask images to disk. Defaults to
FALSE.- dir_processed
Character string. Directory where segmented 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.- imgsz
Target input image resolution for YOLO inference (e.g.,
640,960, or1024). IfNULL(default), inferred automatically or defaults to640.- as_job
Logical. If
TRUEand running inside RStudio, runs the batch segmentation process in an RStudio Background Job. Defaults toFALSE.- ...
Additional arguments passed to
image_segment_dl()(e.g.,model,type,threshold,box_threshold,text_threshold,iou_threshold,prompt,col_background,col_highlight,alpha,bbox,mask,engine,device_id).
Value
An S3 object of class c("dl_segment_batch", "list") containing:
predictions: A consolidateddata.frameof all detected bounding boxes or instance centroids across all images.phenotypes: (Ifmeasure = TRUEand instance segmentation was run) A consolidateddata.frameof all morphological, spectral, and shape traits with columnimg.summary: A per-imagedata.framesummarizing total objects detected, total segmented area, and average size.results: A named list of raw individual segmentation outputs fromimage_segment_dl().
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.