RAD#
Description#
RAD (Retrieval-based Anomaly Detection) is a training-free framework that stores anomaly-free features in a multi-layer memory bank and detects anomalies through multi-level retrieval with position-aware patch matching.
The model uses a DINOv3 ViT-B/16 backbone to extract features from multiple transformer blocks. At inference, it retrieves the most similar training images via CLS token similarity, then performs patch-level anomaly scoring using cosine distance with optional spatial position constraints.
Usage#
anomalib train \
--model rad \
--model.backbone vit_base_patch16_dinov3 \
--model.layers "[3, 6, 9, 11]" \
--model.k_image 150 \
--model.use_positional_bank true \
--model.pos_radius 1
Key Parameters#
Parameter |
Default |
Description |
|---|---|---|
|
|
ViT backbone from timm |
|
|
Transformer block indices (0-based) for feature extraction |
|
|
Number of nearest training images for local memory |
|
|
Enable position-aware patch matching |
|
|
Spatial neighborhood radius for positional matching |
|
|
Fraction of top anomaly pixels for image-level score |
|
|
Memory bank storage dtype; |
The defaults follow the paper’s MVTec-AD configuration. Other datasets use different values:
k_image is 900 for VisA and Real-IAD and 48 for 3D-ADAM, while pos_radius is 2 for VisA
and 0 for Real-IAD.
Note
Position-aware matching compares patches by their grid position, so the fitting and inference
image sizes must produce the same patch grid. Set use_positional_bank=False if they differ.
The memory bank is stored in the model state, so checkpoint size grows with the training set.
Setting bank_dtype=float16 halves the bank’s memory and checkpoint size and speeds up
scoring, at the cost of a small (order 1e-4) perturbation of the anomaly scores. The CLS
bank that drives image retrieval always stays at full precision, so the set of retrieved
training images is unaffected.
RAD: Retrieval-based Anomaly Detection.
This module implements the RAD Lightning model for training-free anomaly detection using multi-layer retrieval with position-aware patch matching.
Paper: https://arxiv.org/abs/2601.22763
Example
>>> from anomalib.data import MVTecAD
>>> from anomalib.models import Rad
>>> from anomalib.engine import Engine
>>> datamodule = MVTecAD()
>>> model = Rad()
>>> engine = Engine()
>>> engine.fit(model=model, datamodule=datamodule)
>>> predictions = engine.predict(model=model, datamodule=datamodule)
- class anomalib.models.image.rad.lightning_model.Rad(backbone='vit_base_patch16_dinov3', layers=None, pre_trained=True, k_image=150, use_positional_bank=True, pos_radius=1, max_ratio=0.01, layer_weights=None, bank_dtype=None, pre_processor=True, post_processor=True, evaluator=True, visualizer=True)#
Bases:
MemoryBankMixin,AnomalibModuleRAD Lightning Module for anomaly detection.
Implements Retrieval-based Anomaly Detection (RAD), a training-free framework that stores anomaly-free features in a multi-layer memory bank and detects anomalies through image-level retrieval followed by position-aware patch matching.
- Parameters:
backbone (
str) – Name of the ViT backbone from timm. Defaults to"vit_base_patch16_dinov3".layers (
list[int] |None) – Block indices (0-based) to extract features from. Defaults to[3, 6, 9, 11].pre_trained (
bool) – Whether to use pre-trained backbone weights. Defaults toTrue.k_image (
int) – Number of nearest-neighbor training images retrieved per test image. The paper uses150for MVTec-AD,900for VisA and Real-IAD, and48for 3D-ADAM. Defaults to150.use_positional_bank (
bool) – Enable position-aware patch matching. Defaults toTrue.pos_radius (
int) – Spatial neighborhood radius (in patch units). The paper uses1for MVTec-AD and 3D-ADAM,2for VisA, and0for Real-IAD. Defaults to1.max_ratio (
float) – Fraction of highest anomaly pixels pooled for image-level score.0means use max. Defaults to0.01.layer_weights (
list[float] |None) – Per-layer score fusion weights.Nonemeans uniform. Defaults toNone.bank_dtype (
dtype|str|None) – Storage dtype of the memory bank.Nonekeeps the backbone dtype.torch.float16halves bank memory and checkpoint size and speeds up scoring, at the cost of a small perturbation of the scores. Defaults toNone.pre_processor (
Module|bool) – Pre-processor instance or flag. Defaults toTrue.post_processor (
Module|bool) – Post-processor instance or flag. Defaults toTrue.evaluator (
Evaluator|bool) – Evaluator instance or flag. Defaults toTrue.visualizer (
Visualizer|bool) – Visualizer instance or flag. Defaults toTrue.
Note
RAD is training-free: fitting only fills the memory bank. The bank is stored in the model state, so checkpoints grow with the size of the anomaly-free training set.
Example
>>> from anomalib.data import MVTecAD >>> from anomalib.models import Rad >>> from anomalib.engine import Engine
>>> datamodule = MVTecAD() >>> model = Rad( ... backbone="vit_base_patch16_dinov3", ... layers=[3, 6, 9, 11], ... k_image=150, ... )
>>> engine = Engine() >>> engine.fit(model=model, datamodule=datamodule)
- static configure_optimizers()#
Configure optimizers.
- Returns:
RAD requires no optimization.
- Return type:
None
- static configure_post_processor()#
Configure the default post-processor.
- Returns:
Default one-class post-processor.
- Return type:
- classmethod configure_pre_processor(image_size=None, center_crop_size=None)#
Configure the default pre-processor for RAD.
- Parameters:
image_size (
tuple[int,int] |None) – Target resize dimensions. Defaults to(512, 512).center_crop_size (
tuple[int,int] |None) – Center crop dimensions. Defaults to(448, 448).
- Returns:
Configured pre-processor instance.
- Return type:
Example
>>> pre_processor = Rad.configure_pre_processor()
- fit()#
Build memory bank from accumulated training features.
- Return type:
None
- property learning_type: LearningType#
Learning type of this model.
- Returns:
LearningType.ONE_CLASS.- Return type:
LearningType
- property trainer_arguments: dict[str, Any]#
Default trainer arguments.
- Returns:
Trainer arguments for single-epoch feature extraction.
- Return type:
dict[str, Any]
- training_step(batch, *args, **kwargs)#
Extract and store features from training batch.
- Parameters:
batch (
Batch) – Input batch containing images.*args – Additional arguments (unused).
**kwargs – Additional keyword arguments (unused).
- Returns:
Dummy loss tensor for Lightning compatibility.
- Return type:
Tensor
- validation_step(batch, *args, **kwargs)#
Generate predictions for a batch of images.
- Parameters:
batch (
Batch) – Input batch containing images.*args – Additional arguments (unused).
**kwargs – Additional keyword arguments (unused).
- Returns:
Batch with predictions.
- Return type:
Union[Tensor,Mapping[str,Any],None]
PyTorch model for RAD (Retrieval-based Anomaly Detection).
This module implements the core RAD algorithm: multi-layer feature extraction, memory bank construction, image-level retrieval, and position-aware patch-level anomaly scoring.
Paper: https://arxiv.org/abs/2601.22763
- class anomalib.models.image.rad.torch_model.RadModel(backbone='vit_base_patch16_dinov3', pre_trained=True, layers=None, k_image=150, use_positional_bank=True, pos_radius=1, max_ratio=0.01, layer_weights=None, bank_dtype=None)#
Bases:
DynamicBufferMixin,ModuleRAD PyTorch model for anomaly detection.
Implements training-free retrieval-based anomaly detection using multi-layer ViT features and position-aware patch matching.
- Parameters:
backbone (
str) – Name of the ViT backbone. Defaults to"vit_base_patch16_dinov3".pre_trained (
bool) – Whether to use pre-trained backbone weights. Defaults toTrue.layers (
list[int] |None) – Block indices (0-based) to extract features from. Defaults to[3, 6, 9, 11].k_image (
int) – Number of nearest-neighbor training images retrieved per test image. The paper uses150for MVTec-AD,900for VisA and Real-IAD, and48for 3D-ADAM. Defaults to150.use_positional_bank (
bool) – Enable position-aware patch matching. Defaults toTrue.pos_radius (
int) – Spatial neighborhood radius (in patch units) for position-aware matching. The paper uses1for MVTec-AD and 3D-ADAM,2for VisA, and0for Real-IAD. Defaults to1.max_ratio (
float) – Fraction of highest anomaly pixels pooled for image-level score.0means use max. Defaults to0.01.layer_weights (
list[float] |None) – Weights for each layer in score fusion.Nonemeans uniform. Defaults toNone.bank_dtype (
dtype|str|None) – Storage dtype of the memory bank.Nonekeeps the backbone dtype (exact).torch.float16halves bank memory and checkpoint size and speeds up scoring, at the cost of a small (order1e-4) perturbation of the anomaly scores. Defaults toNone.
- Raises:
ValueError – If
layersis empty,k_imageis not positive,pos_radiusis negative,max_ratiois outside[0, 1], orlayer_weightsis negative, sums to zero, or does not have one entry per layer.
Example
>>> model = RadModel() >>> x = torch.randn(2, 3, 448, 448) >>> model.training = True >>> _ = model(x)
- build_memory_bank()#
Build the memory bank from accumulated training features.
The banks are written into pre-allocated buffers one chunk at a time, and each chunk is released as soon as it has been copied. Concatenating and then stacking instead would hold two full-size copies of the bank at once, doubling the peak memory of fitting.
- Raises:
ValueError – If no training features have been collected.
- Return type:
None
- forward(input_tensor)#
Process input through the model.
During training, extracts and stores features. During inference, computes anomaly maps and scores via retrieval-based matching.
- Parameters:
input_tensor (
Tensor) – Input images of shape(B, 3, H, W).- Returns:
- Embeddings during training, or
InferenceBatch with anomaly maps and scores during inference.
- Return type:
Tensor|InferenceBatch