AnomalyVFM#

Vision Foundation Model (VFM) based zero-shot anomaly detection model.

Example

>>> from anomalib.models.image import AnomalyVFM
>>> # Zero-shot approach
>>> model = AnomalyVFM()
class anomalib.models.image.anomalyvfm.lightning_model.AnomalyVFM(pre_processor=True, post_processor=True, evaluator=True, visualizer=True, precision=PrecisionType.FLOAT32)#

Bases: AnomalibModule

Vision Foundation Model (VFM) based zero-shot anomaly detection model.

Example

>>> from anomalib.models.image import AnomalyVFM
>>> # Zero-shot approach
>>> model = AnomalyVFM()
classmethod configure_post_processor()#

Configure the default post processor.

Returns:

Post-processor for one-class models that

converts raw scores to anomaly predictions

Return type:

PostProcessor | None

classmethod configure_pre_processor(image_size=None)#

Configure the default pre-processor for AnomalyVFM.

Pre-processor resizes images.

Parameters:

image_size (tuple[int, int] | None) – Target size for resizing. Defaults to (768, 768).

Returns:

Configured AnomalyVFM pre-processor

Return type:

PreProcessor

static configure_transforms(image_size=None)#

Configure image transforms.

Parameters:

image_size (tuple[int, int] | None) – Ignored as each model has its own transforms. Defaults to None.

Return type:

None

property learning_type: LearningType#

Get the learning type of the model. This model always uses zero-shot learning.

Returns:

ZERO_SHOT.

Return type:

LearningType

predict_step(batch, *args, **kwargs)#

Redirect to validation step.

Return type:

ImageBatch

test_step(batch, *args, **kwargs)#

Redirect to validation step.

Return type:

ImageBatch

to_onnx(*_, **__)#

Skip export to onnx.

Return type:

None

to_openvino(*_, **__)#

Skip export to openvino.

Return type:

None

to_torch(*_, **__)#

Skip export to torch.

Return type:

None

property trainer_arguments: dict[str, int | float]#

Get trainer arguments.

Returns:

Empty dict as no training is needed.

Return type:

dict[str, int | float]

validation_step(batch, *args, **kwargs)#

Perform the validation step and return the anomaly map and anomaly score.

Parameters:
  • batch (ImageBatch) – Input batch

  • args – Additional arguments.

  • kwargs – Additional keyword arguments.

Returns:

batch dictionary containing anomaly-maps and anomaly-scores.

Return type:

Union[Tensor, Mapping[str, Any], None]

PyTorch model for the AnomalyVFM model implementation.

class anomalib.models.image.anomalyvfm.torch_model.AnomalyVFMModel#

Bases: Module

AnomalyVFM PyTorch model.

This model integrates a base Vision Foundation Model (RADIO) configured with Parameter-Efficient Fine-Tuning (PEFT), alongside a simple decoder for generating pixel-level anomaly masks and a simple predictor for image-level anomaly scores.

forward(img)#

Forward pass to compute anomaly scores and masks.

Parameters:

img (Tensor) – Input image batch of shape (b, C, H, W).

Returns:

A tuple containing:
  • anomaly_score (torch.Tensor): Image-level anomaly predictions.

  • anomaly_mask (torch.Tensor): Pixel-level anomaly prediction masks.

Return type:

tuple[Tensor, Tensor]