
Extract Foundation Features and Semantic Visualization with DINOv2
Source:R/utils_neural.R
image_features_dl.RdExtracts dense patch embeddings and global representation vectors from DINOv2 (Vision Transformer ViT-S/14), and computes top-3 PCA false-color semantic segmentation without any labels or supervised training.
Usage
image_features_dl(
img,
model = "dinov2",
patch_size = 14,
return_pca = TRUE,
interpolate = TRUE,
threads = 0,
engine = c("gpu", "cpu"),
device_id = -1,
verbose = TRUE,
plot = TRUE,
dir = pliman_model_dir(),
...
)Arguments
- img
An
Imageobject, a 2D/3D numeric array, a path to an image file, or a list of images.- model
Model name or path to a custom ONNX file. Defaults to
"dinov2".- patch_size
Size of ViT patches in pixels (default 14).
- return_pca
Logical. If
TRUE, computes top-3 principal components of patch embeddings.- interpolate
Logical. If
TRUE, interpolates the PCA map from patch grid (37x37) to original image resolution.- threads
Number of CPU threads (default 0 for auto-tuning).
- engine
Execution engine:
"cpu"or"gpu"(DirectML).- device_id
GPU device ID (default -1 for auto).
- verbose
Logical. Whether to show progress messages.
- plot
Logical. Whether to plot the PCA false-color semantic image.
- dir
Model directory (default
pliman_model_dir()).- ...
Additional arguments passed to
graphics::plot().
Value
A list containing:
cls_token: 384-dimensional global semantic representation vector.pca_image: AnImageobject representing the 3-component PCA false-color semantic map.pca_r,pca_g,pca_b: Matrices of normalized PCA coordinates.num_patches: Total number of spatial patches.embed_dim: Dimensionality of patch embeddings.
Examples
if (FALSE) { # interactive()
library(pliman)
img <- image_import("leaf.png")
feats <- image_features_dl(img)
}