GeneralAD#

Architecture#

GeneralAD Architecture

Lightning implementation of GeneralAD.

class anomalib.models.image.general_ad.lightning_model.GeneralAD(backbone='vit_large_patch14_dinov2.lvd142m', layers=(24,), hidden_dim=2048, lr=0.0005, lr_decay_factor=0.2, weight_decay=1e-05, epochs=160, noise_std=0.25, dsc_layers=1, dsc_heads=4, dsc_dropout=0.1, num_fake_patches=-1, fake_feature_type='random', top_k=10, pre_trained=True, pre_processor=True, post_processor=True, evaluator=True, visualizer=True)#

Bases: AnomalibModule

GeneralAD Lightning module.

Parameters:
  • backbone (str) – Timm backbone used as frozen feature extractor.

  • layers (Sequence[int]) – Backbone stages or transformer blocks used for patch features.

  • hidden_dim (int) – Hidden size of the patch discriminator.

  • lr (float) – Optimizer learning rate.

  • lr_decay_factor (float) – Final cosine annealing ratio.

  • weight_decay (float) – Optimizer weight decay.

  • epochs (int) – Number of epochs used to parameterize the LR scheduler.

  • noise_std (float) – Standard deviation for synthetic anomalous features.

  • dsc_layers (int) – Number of attention blocks in the discriminator.

  • dsc_heads (int) – Number of attention heads in the discriminator.

  • dsc_dropout (float) – Dropout rate in the discriminator.

  • num_fake_patches (int) – Maximum number of perturbed patches per image.

  • fake_feature_type (Literal['random', 'attn', 'copy_out', 'shuffle', 'randshuffle', 'copy_out_and_random', 'copy_out_and_attn', 'shuffle_and_random', 'shuffle_and_attn', 'randshuffle_and_random', 'randshuffle_and_attn']) – Strategy for generating pseudo anomalies.

  • top_k (int) – Number of highest patch scores to average for image scoring.

  • pre_trained (bool) – Whether to use pretrained backbone weights.

  • pre_processor (PreProcessor | bool) – Optional anomalib pre-processor.

  • post_processor (PostProcessor | bool) – Optional anomalib post-processor.

  • evaluator (Evaluator | bool) – Optional anomalib evaluator.

  • visualizer (Visualizer | bool) – Optional anomalib visualizer.

static configure_evaluator()#

Configure validation and test metrics for checkpoint selection.

Upstream GeneralAD selects the best checkpoint using image-level AUROC on the validation loop. In our MVTec reproduction, validation is configured as SAME_AS_TEST to mirror the upstream behavior, so monitoring image_AUROC here reproduces the same selection rule.

Return type:

Evaluator

configure_optimizers()#

Configure optimizer and cosine LR schedule.

Return type:

dict[str, Any]

classmethod configure_pre_processor(image_size=None)#

Configure the default pre-processor.

Return type:

PreProcessor

property learning_type: LearningType#

GeneralAD is a one-class anomaly detection model.

property trainer_arguments: dict[str, Any]#

Default trainer arguments for GeneralAD.

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

Run the self-supervised GeneralAD training step.

Return type:

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

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

Generate validation predictions.

Return type:

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

PyTorch model for the GeneralAD algorithm.

GeneralAD learns a patch-wise discriminator on top of frozen features extracted from a pretrained backbone. During training it creates pseudo-anomalous patch features using noise injection and patch shuffling/copying. During inference the discriminator scores each patch and the highest patch scores are aggregated into an image-level anomaly score.

class anomalib.models.image.general_ad.torch_model.AttentionBlock(embed_dim, hidden_dim, num_heads, dropout=0.0)#

Bases: Module

Transformer block used in the patch discriminator.

forward(x)#

Apply self-attention and MLP updates to discriminator features.

Return type:

Tensor

class anomalib.models.image.general_ad.torch_model.EVAFeatureExtractor(backbone, layers, image_size, pre_trained=True)#

Bases: Module

Extract patch features from EVA-style transformer backbones.

forward(images)#

Extract transformer patch features for EVA backbones.

Return type:

Tensor

class anomalib.models.image.general_ad.torch_model.GeneralADModel(backbone='vit_large_patch14_dinov2.lvd142m', layers=(24,), hidden_dim=2048, noise_std=0.25, dsc_layers=1, dsc_heads=4, dsc_dropout=0.1, image_size=(518, 518), num_fake_patches=-1, fake_feature_type='random', top_k=10, pre_trained=True)#

Bases: Module

Core GeneralAD model.

compute_loss(images)#

Compute the GeneralAD self-supervised training loss.

Return type:

Tensor

extract_features(images)#

Extract patch features and optional class-attention maps.

Return type:

tuple[Tensor, Tensor | None]

forward(images)#

Predict anomaly scores and maps.

Return type:

InferenceBatch

score_features(features)#

Convert patch logits to image and pixel anomaly scores.

Return type:

tuple[Tensor, Tensor]

class anomalib.models.image.general_ad.torch_model.PatchDiscriminator(embed_dim, hidden_dim, num_patches, num_layers=1, num_heads=12, dropout_rate=0.0)#

Bases: Module

Attention-based patch discriminator.

forward(features)#

Score each image patch with the discriminator.

Return type:

Tensor

class anomalib.models.image.general_ad.torch_model.ViTFeatureExtractor(backbone, layers, image_size, pre_trained=True)#

Bases: Module

Extract patch features and class-attention maps from ViT backbones.

forward(images, output_attn=False)#

Extract transformer patch features and optional class attention.

Return type:

Tensor | tuple[Tensor, Tensor]