Efficient AD#

EfficientAd: Accurate Visual Anomaly Detection at Millisecond-Level Latencies.

This module implements the EfficientAd model for fast and accurate anomaly detection. EfficientAd uses a student-teacher architecture with a pre-trained EfficientNet backbone to achieve state-of-the-art performance with millisecond-level inference times.

The model consists of:
  • A pre-trained EfficientNet teacher network

  • A lightweight student network

  • Knowledge distillation training

  • Anomaly detection via feature comparison

Example

>>> from anomalib.data import MVTec
>>> from anomalib.models import EfficientAd
>>> from anomalib.engine import Engine
>>> datamodule = MVTec()
>>> model = EfficientAd()
>>> engine = Engine()
>>> engine.fit(model, datamodule=datamodule)  
>>> predictions = engine.predict(model, datamodule=datamodule)  
Paper:

“EfficientAd: Accurate Visual Anomaly Detection at Millisecond-Level Latencies” https://arxiv.org/pdf/2303.14535.pdf

See also

anomalib.models.image.efficient_ad.torch_model.EfficientAdModel:

PyTorch implementation of the EfficientAd model architecture.

class anomalib.models.image.efficient_ad.lightning_model.EfficientAd(imagenet_dir='./datasets/imagenette', teacher_out_channels=384, model_size=EfficientAdModelSize.S, lr=0.0001, weight_decay=1e-05, padding=False, pad_maps=True, pre_processor=True, post_processor=True, evaluator=True, visualizer=True)#

Bases: AnomalibModule

PL Lightning Module for the EfficientAd algorithm.

The EfficientAd model uses a student-teacher architecture with a pretrained EfficientNet backbone for fast and accurate anomaly detection.

Parameters:
  • imagenet_dir (Path | str) – Directory path for the Imagenet dataset. Defaults to "./datasets/imagenette".

  • teacher_out_channels (int) – Number of convolution output channels. Defaults to 384.

  • model_size (EfficientAdModelSize | str) – Size of student and teacher model. Defaults to EfficientAdModelSize.S.

  • lr (float) – Learning rate. Defaults to 0.0001.

  • weight_decay (float) – Optimizer weight decay. Defaults to 0.00001.

  • padding (bool) – Use padding in convolutional layers. Defaults to False.

  • pad_maps (bool) – Relevant if padding=False. If True, pads the output anomaly maps to match size of padding=True case. Defaults to True.

  • pre_processor (PreProcessor | bool, optional) – Pre-processor used to transform input data before passing to model. Defaults to True.

  • post_processor (PostProcessor | bool, optional) – Post-processor used to process model predictions. Defaults to True.

  • evaluator (Evaluator | bool, optional) – Evaluator used to compute metrics. Defaults to True.

  • visualizer (Visualizer | bool, optional) – Visualizer used to create visualizations. Defaults to True.

Example

>>> from anomalib.models import EfficientAd
>>> model = EfficientAd(
...     imagenet_dir="./datasets/imagenette",
...     model_size="s",
...     lr=1e-4
... )
configure_optimizers()#

Configure optimizers for training.

Sets up Adam optimizer with learning rate scheduler that decays LR by 0.1 at 95% of training.

Returns:

Dictionary containing:
  • optimizer: Adam optimizer

  • lr_scheduler: StepLR scheduler

Return type:

dict

Raises:

ValueError – If neither max_epochs nor max_steps is defined.

classmethod configure_pre_processor(image_size=None)#

Configure default pre-processor for EfficientAd.

Note that ImageNet normalization is applied in the forward pass, not here.

Parameters:

image_size (tuple[int, int] | None, optional) – Target image size. Defaults to (256, 256).

Returns:

Configured pre-processor with resize transform.

Return type:

PreProcessor

property learning_type: LearningType#

Get model’s learning type.

Returns:

Always LearningType.ONE_CLASS

Return type:

LearningType

map_norm_quantiles(dataloader)#

Calculate quantiles of student and autoencoder feature maps.

Computes the 90% and 99.5% quantiles of the feature maps from both the student network and autoencoder on normal (good) validation samples.

Parameters:

dataloader (DataLoader) – Validation dataloader.

Returns:

Dictionary containing:
  • qa_st: 90% quantile of student maps

  • qa_ae: 90% quantile of autoencoder maps

  • qb_st: 99.5% quantile of student maps

  • qb_ae: 99.5% quantile of autoencoder maps

Return type:

dict[str, torch.Tensor]

on_train_start()#

Set up model before training begins.

Performs the following steps: 1. Validates training parameters (batch size=1, no normalization) 2. Sets up pretrained teacher model 3. Prepares ImageNette dataset 4. Calculates channel statistics

Raises:

ValueError – If train_batch_size != 1 or transforms contain normalization.

Return type:

None

on_validation_start()#

Calculate feature map statistics before validation.

Computes quantiles of feature maps on validation set and updates model.

Return type:

None

prepare_imagenette_data(image_size)#

Prepare ImageNette dataset transformations.

Sets up data transforms and downloads ImageNette dataset if not present.

Parameters:

image_size (tuple[int, int] | torch.Size) – Target image size for transforms.

Return type:

None

prepare_pretrained_model()#

Prepare the pretrained teacher model.

Downloads and loads pretrained weights for the teacher model if not already present.

Return type:

None

teacher_channel_mean_std(dataloader)#

Calculate channel-wise mean and std of teacher model activations.

Computes running mean and standard deviation of teacher model feature maps over the full dataset.

Parameters:

dataloader (DataLoader) – Dataloader for the dataset.

Returns:

Dictionary containing:
  • mean: Channel-wise means of shape (1, C, 1, 1)

  • std: Channel-wise standard deviations of shape (1, C, 1, 1)

Return type:

dict[str, torch.Tensor]

Raises:

ValueError – If no data is provided (n remains None).

property trainer_arguments: dict[str, Any]#

Get trainer arguments.

Returns:

Dictionary with trainer arguments:
  • num_sanity_val_steps: 0

Return type:

dict[str, Any]

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

Perform training step.

Computes student, autoencoder and combined losses using both the input batch and a batch from ImageNette.

Parameters:
  • batch (Batch) – Input batch containing image and labels

  • *args – Additional arguments (unused)

  • **kwargs – Additional keyword arguments (unused)

Returns:

Dictionary containing total loss

Return type:

dict[str, torch.Tensor]

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

Perform validation step.

Generates anomaly maps for the input batch.

Parameters:
  • batch (Batch) – Input batch

  • *args – Additional arguments (unused)

  • **kwargs – Additional keyword arguments (unused)

Returns:

Batch with added predictions

Return type:

STEP_OUTPUT

PyTorch implementation of the EfficientAd model architecture.

This module contains the PyTorch implementation of the student, teacher and autoencoder networks used in EfficientAd for fast and accurate anomaly detection.

The model consists of:
  • A pre-trained EfficientNet teacher network

  • A lightweight student network

  • Knowledge distillation training

  • Anomaly detection via feature comparison

Example

>>> from anomalib.models.image.efficient_ad.torch_model import EfficientAdModel
>>> model = EfficientAdModel()
>>> input_tensor = torch.randn(32, 3, 256, 256)
>>> output = model(input_tensor)
>>> output["anomaly_map"].shape
torch.Size([32, 256, 256])
Paper:

“EfficientAd: Accurate Visual Anomaly Detection at Millisecond-Level Latencies” https://arxiv.org/pdf/2303.14535.pdf

See also

anomalib.models.image.efficient_ad.lightning_model.EfficientAd:

Lightning implementation of the EfficientAd model.

class anomalib.models.image.efficient_ad.torch_model.EfficientAdModel(teacher_out_channels, model_size=EfficientAdModelSize.S, padding=False, pad_maps=True)#

Bases: Module

EfficientAd model.

The EfficientAd model consists of a teacher and student network for anomaly detection. The teacher network is pre-trained and frozen, while the student network is trained to match the teacher’s outputs.

Parameters:
  • teacher_out_channels (int) – Number of convolution output channels of the pre-trained teacher model.

  • model_size (EfficientAdModelSize) – Size of student and teacher model. Defaults to EfficientAdModelSize.S.

  • padding (bool) – Whether to use padding in convolutional layers. Defaults to False.

  • pad_maps (bool) – Whether to pad output anomaly maps when padding=False to match size of padded case. Only relevant if padding=False. Defaults to True.

Example

>>> from anomalib.models.image.efficient_ad.torch_model import (
...     EfficientAdModel,
...     EfficientAdModelSize
... )
>>> model = EfficientAdModel(
...     teacher_out_channels=384,
...     model_size=EfficientAdModelSize.S
... )
>>> input_tensor = torch.randn(32, 3, 256, 256)
>>> output = model(input_tensor)
>>> output.anomaly_map.shape
torch.Size([32, 1, 256, 256])

Notes

The model uses a student-teacher architecture where: - Teacher network is pre-trained and frozen - Student network learns to match teacher outputs - Autoencoder provides additional feature extraction - Anomaly scores are computed from student-teacher differences

static choose_random_aug_image(image)#

Apply random augmentation to input image.

Randomly selects and applies one of: brightness, contrast or saturation adjustment with coefficient sampled from U(0.8, 1.2).

Parameters:

image (torch.Tensor) – Input image tensor.

Returns:

Augmented image tensor.

Return type:

torch.Tensor

compute_losses(batch, batch_imagenet, distance_st)#

Compute training losses.

Computes three loss components: - Student-teacher loss (hard examples + ImageNet penalty) - Autoencoder reconstruction loss - Student-autoencoder consistency loss

Parameters:
Returns:

  • Student-teacher loss

  • Autoencoder loss

  • Student-autoencoder loss

Return type:

tuple[torch.Tensor, torch.Tensor, torch.Tensor]

compute_maps(batch, student_output, distance_st, normalize=True)#

Compute anomaly maps from model outputs.

Parameters:
  • batch (torch.Tensor) – Input batch of images.

  • student_output (torch.Tensor) – Student network output features.

  • distance_st (torch.Tensor) – Student-teacher distances.

  • normalize (bool) – Whether to normalize maps with pre-computed quantiles. Defaults to True.

Returns:

  • Student-teacher anomaly map

  • Student-autoencoder anomaly map

Return type:

tuple[torch.Tensor, torch.Tensor]

compute_student_teacher_distance(batch)#

Compute the student-teacher distance vectors.

Parameters:

batch (torch.Tensor) – Input batch of images.

Returns:

  • Student network output features

  • Squared distance between normalized teacher and student features

Return type:

tuple[torch.Tensor, torch.Tensor]

forward(batch, batch_imagenet=None, normalize=True)#

Forward pass through the model.

Parameters:
  • batch (torch.Tensor) – Input batch of images.

  • batch_imagenet (torch.Tensor | None) – Optional batch of ImageNet images for training. Defaults to None.

  • normalize (bool) – Whether to normalize anomaly maps. Defaults to True.

Returns:

If training:
  • Loss components (student-teacher, autoencoder, student-autoencoder)

If inference:
  • Batch containing anomaly maps and scores

Return type:

tuple[torch.Tensor, torch.Tensor, torch.Tensor] | InferenceBatch

get_maps(batch, normalize=False)#

Compute anomaly maps for a batch of images.

Convenience method that combines distance computation and map generation.

Parameters:
  • batch (torch.Tensor) – Input batch of images.

  • normalize (bool) – Whether to normalize maps. Defaults to False.

Returns:

  • Student-teacher anomaly map

  • Student-autoencoder anomaly map

Return type:

tuple[torch.Tensor, torch.Tensor]

static is_set(p_dic)#

Check if any parameters in the dictionary are non-zero.

Parameters:

p_dic (nn.ParameterDict) – Parameter dictionary to check.

Returns:

True if any parameter is non-zero, False otherwise.

Return type:

bool