FoundAD#
Description#
FoundAD trains a lightweight manifold projector on top of a frozen DINOv2/DINOv3 encoder, using CutPaste-synthesized anomalies for supervision. At inference, the MSE between encoder and projector features flags off-manifold (anomalous) patches. Effective in few-shot settings with only 1-4 normal training images. See the model README for the full architecture description and benchmark results.
Usage#
anomalib train --model FoundAD --data MVTecAD --data.category bottle
FoundAD: Foundation Visual Encoders Are Secretly Few-Shot Anomaly Detectors.
This module implements the FoundAD model for anomaly detection. The method trains a lightweight manifold projector on top of a frozen DINOv2 encoder, using CutPaste augmentation to provide off-manifold supervision. Anomaly detection is performed by measuring the MSE between encoder features and projector output — high MSE indicates that features lie off the normal image manifold.
The model is particularly effective in few-shot settings where only a small number of normal training samples are available.
Example
>>> from anomalib.data import MVTecAD
>>> from anomalib.models import FoundAD
>>> from anomalib.engine import Engine
>>> datamodule = MVTecAD()
>>> model = FoundAD()
>>> engine = Engine()
>>> engine.fit(model, datamodule=datamodule)
>>> predictions = engine.predict(model, datamodule=datamodule)
- Reference:
Zhai et al., “Foundation Visual Encoders Are Secretly Few-Shot Anomaly Detectors”, ICLR 2026. https://arxiv.org/abs/2510.01934
See also
anomalib.models.image.foundad.torch_model.FoundADModel:PyTorch implementation of the FoundAD model.
- class anomalib.models.image.foundad.lightning_model.FoundAD(encoder_name='dinov2_vit_base_14', pred_depth=6, pred_emb_dim=384, n_layer=3, top_k=10, dropout=0.2, feat_normed=False, use_pos_embed=False, image_size=None, lr=0.001, weight_decay=0.0001, color_jitter=0.5, use_few_shot_augmentation=True, pre_processor=True, post_processor=True, evaluator=True, visualizer=True)#
Bases:
AnomalibModuleFoundAD Lightning Module.
Trains a lightweight manifold projector on a frozen DINOv2 encoder with CutPaste augmentation. Only the projector parameters are learned.
- Parameters:
encoder_name (
str) – DINOv2 model name. Defaults to"dinov2_vit_base_14".pred_depth (
int) – Number of projector transformer blocks. Defaults to6.pred_emb_dim (
int) – Projector internal dimension. Defaults to384.n_layer (
int) – Encoder layer to extract features from (n-th from last). Defaults to3.top_k (
int) – Top-K patches for image-level scoring. Defaults to10.dropout (
float) – Dropout on features before projector. Defaults to0.2.feat_normed (
bool) – L2-normalize features. Defaults toFalse.use_pos_embed (
bool) – Use positional encoding in projector. Defaults toFalse.image_size (
int|None) – Input image size. Defaults to518for DINOv2 (37*14, patch size 14) or512for DINOv3 (32*16, patch size 16) encoders. Smaller values like224speed up inference at cost of resolution, but must stay divisible by the encoder’s patch size.lr (
float) – Learning rate. Defaults to1e-3.weight_decay (
float) – Weight decay for AdamW. Defaults to1e-4.color_jitter (
float) – CutPaste color jitter strength. Defaults to0.5.use_few_shot_augmentation (
bool) – Apply random orientation (flip/rotate) and appearance (color jitter/grayscale/blur) augmentation to the base training images before CutPaste. This matches the paper’s default training protocol and is important for expanding the diversity of a tiny (1/2/4-shot) training set; disabling it will hurt few-shot results. Defaults toTrue.pre_processor (
PreProcessor|bool) – Pre-processor configuration.post_processor (
PostProcessor|bool) – Post-processor configuration.evaluator (
Evaluator|bool) – Evaluator configuration.visualizer (
Visualizer|bool) – Visualizer configuration.
Example
>>> from anomalib.data import MVTecAD >>> from anomalib.models import FoundAD >>> from anomalib.engine import Engine
>>> model = FoundAD() >>> datamodule = MVTecAD() >>> engine = Engine() >>> engine.fit(model, datamodule=datamodule)
- configure_optimizers()#
Configure AdamW optimizer for the projector parameters.
Only the manifold projector is trained; the encoder is frozen.
- Return type:
Union[Optimizer,Sequence[Optimizer],tuple[Sequence[Optimizer],Sequence[Union[LRScheduler,ReduceLROnPlateau,LRSchedulerConfig]]],OptimizerConfig,OptimizerLRSchedulerConfig,Sequence[OptimizerConfig],Sequence[OptimizerLRSchedulerConfig],None]- Returns:
Optimizer configuration.
- classmethod configure_pre_processor(image_size=None)#
Configure default preprocessing for FoundAD.
Uses ImageNet normalization matching the DINOv2 encoder’s training distribution. Default image size is 518 (37*14) for optimal alignment with DINOv2 ViT-Base patch_size=14.
- Parameters:
image_size (
tuple[int,int] |None) – Target size as (H, W). Defaults to (518, 518).- Return type:
- Returns:
Configured PreProcessor.
- property learning_type: LearningType#
Return the learning type.
FoundAD is designed for few-shot settings but trains on normal samples only, making it ONE_CLASS in Anomalib’s taxonomy.
- Returns:
LearningType.ONE_CLASS.
- property trainer_arguments: dict[str, Any]#
Return FoundAD-specific trainer arguments.
- Returns:
Trainer configuration dict.
PyTorch model for the FoundAD implementation.
FoundAD uses a frozen DINOv2 encoder and trains a lightweight manifold projector to project encoder features back onto the natural image manifold. Anomalies are detected as patches where the MSE between encoder features and projector output is high — indicating the features lie off the normal manifold.
- Reference:
Zhai et al., “Foundation Visual Encoders Are Secretly Few-Shot Anomaly Detectors”, ICLR 2026. https://arxiv.org/abs/2510.01934
See also
anomalib.models.image.foundad.lightning_model.FoundAD:FoundAD Lightning model.
- class anomalib.models.image.foundad.torch_model.FoundADModel(encoder_name='dinov2_vit_base_14', pred_depth=6, pred_emb_dim=384, n_layer=3, top_k=10, dropout=0.2, feat_normed=False, use_pos_embed=False, image_size=None)#
Bases:
ModuleFoundAD: Foundation encoder + manifold projector anomaly detection model.
The model extracts features from a frozen DINOv2 encoder and passes them through a lightweight manifold projector. During training, it learns to project normal features back onto the manifold using MSE loss with CutPaste augmented supervision. During inference, high MSE indicates anomalous regions.
- Parameters:
encoder_name (
str) – DINOv2 model name. Defaults to"dinov2_vit_base_14".pred_depth (
int) – Number of projector transformer blocks. Defaults to6.pred_emb_dim (
int) – Projector internal dimension. Defaults to384.n_layer (
int) – Which intermediate encoder layer to extract features from (counting from the last layer). Defaults to3.top_k (
int) – Number of top anomalous patches for image-level scoring. Defaults to10.dropout (
float) – Dropout rate for features before projector. Defaults to0.2.feat_normed (
bool) – Whether to L2-normalize features. Defaults toFalse.use_pos_embed (
bool) – Whether to use positional encoding in projector. Defaults toFalse.image_size (
int|None) – Input image resolution used to size the projector’s positional embedding whenuse_pos_embed=True. Required to be consistent with the resolution the model is actually run at, since the encoder’s own default resolution (e.g. DINOv3’s 256) may not match. Defaults toNone, which falls back to the encoder’s native/pretrained resolution.
- Raises:
ValueError – If
top_kis not a positive integer.
Example
>>> model = FoundADModel(encoder_name="dinov2_vit_base_14") >>> images = torch.randn(2, 3, 518, 518) >>> model.eval() FoundADModel(...) >>> result = model(images) >>> result.anomaly_map.shape torch.Size([2, 1, 518, 518])
- extract_features(images)#
Extract features from the frozen encoder.
Uses
get_intermediate_layersto extract patch-token features (prefix/CLS/register tokens already stripped) from the n-th last encoder block. L2-normalized iffeat_normed=True(matching the original FoundAD, which normalizes encoder features before they are used as either the training/scoring target or the projector’s input).- Parameters:
images (
Tensor) – Input images of shape (B, C, H, W).- Return type:
Tensor- Returns:
Patch features of shape (B, num_patches, embed_dim).
- forward(images, augmented_images=None)#
Forward pass.
- During training:
Computes MSE loss between target features (from original images) and predicted features (from original or augmented images via projector). If
augmented_imagesis provided, with 50% probability the projector receives augmented features instead of normal ones.- During inference:
Computes per-patch MSE, generates an anomaly map via bilinear interpolation, applies Gaussian smoothing, and scores images using top-K patch MSE values.
- Parameters:
images (
Tensor) – Input images of shape (B, C, H, W).augmented_images (
Tensor|None) – CutPaste-augmented images for training. None during inference.
- Returns:
MSE loss as scalar tensor. Inference:
InferenceBatchwithpred_scoreandanomaly_map.- Return type:
Tensor|InferenceBatch
- predict(features)#
Pass features through the manifold projector.
- Parameters:
features (
Tensor) – Encoder features of shape (B, N, D).- Return type:
Tensor- Returns:
Projected features of shape (B, N, D).