library(petrographer)
library(dplyr)
library(fs)This vignette walks through a whole-slide style inference workflow using a pretrained detection model. The focus is:
For now this is detection-first. Segmentation can also be run on large images, but its workflow is different because RF-DETR segmentation does not yet use the SAHI path in this package.
library(petrographer)
library(dplyr)
library(fs)Replace the placeholder path with a whole-slide image or any large thin-section image you want to analyze.
wsi_path <- "path/to/whole_slide_image.tif"
file_exists(wsi_path)Use from_pretrained() to load a detector from your local board or the public hub.
model <- from_pretrained(
model_id = "inclusions_small",
device = "cpu",
confidence = 0.5
)
modelThe loaded object keeps the parsed manifest and training summary, so you can inspect provenance if needed:
model$manifest$model
model$training_summary$trainingFor large images, sliced inference usually works better than trying to process the entire image at once. slice_size and overlap let you trade off runtime, memory use, and boundary stability.
output_dir <- path("results", path_file(path_ext_remove(wsi_path)))
predictions <- predict_image(
image_path = wsi_path,
model = model,
use_slicing = TRUE,
slice_size = 1024,
overlap = 0.2,
output_dir = output_dir,
save_visualizations = TRUE
)
predictionsThe returned tibble contains one row per detected object, including class labels, confidence scores, bounding-box-derived geometry, and summary fields added by enhance_results().
dir_ls(output_dir)
viz_path <- dir_ls(output_dir, glob = "*_prediction.png", fail = FALSE)
if (length(viz_path)) {
knitr::include_graphics(viz_path[[1]])
}per_image <- summarize_by_image(predictions)
per_image
population <- get_population_stats(predictions)
populationThese summaries are useful for:
Once you are happy with the settings on one image, scale out to a directory.
batch_results <- predict_images(
input_dir = "path/to/whole_slide_directory",
model = model,
use_slicing = TRUE,
slice_size = 1024,
overlap = 0.2,
output_dir = "results/batch_whole_slides",
save_visualizations = TRUE
)
batch_resultsslice_size and overlap to match your image scale and memory budgetpredictions with tidyverse tools for class-specific summariessummarize_by_image() and get_population_stats() for reporting