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 MVTecAD
>>> from anomalib.models import EfficientAd
>>> from anomalib.engine import Engine
>>> datamodule = MVTecAD()
>>> 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:
AnomalibModulePL 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 to384.model_size (
EfficientAdModelSize|str) – Size of student and teacher model. Defaults toEfficientAdModelSize.S.lr (
float) – Learning rate. Defaults to0.0001.weight_decay (
float) – Optimizer weight decay. Defaults to0.00001.padding (
bool) – Use padding in convolutional layers. Defaults toFalse.pad_maps (
bool) – Relevant ifpadding=False. IfTrue, pads the output anomaly maps to match size ofpadding=Truecase. Defaults toTrue.pre_processor (
PreProcessor|bool) – Pre-processor used to transform input data before passing to model. Defaults toTrue.post_processor (
PostProcessor|bool) – Post-processor used to process model predictions. Defaults toTrue.evaluator (
Evaluator|bool) – Evaluator used to compute metrics. Defaults toTrue.visualizer (
Visualizer|bool) – Visualizer used to create visualizations. Defaults toTrue.
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 optimizerlr_scheduler: StepLR scheduler
- Return type:
- Raises:
ValueError – If neither
max_epochsnormax_stepsis 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.
- 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 mapsqa_ae: 90% quantile of autoencoder mapsqb_st: 99.5% quantile of student mapsqb_ae: 99.5% quantile of autoencoder maps
- Return type:
- 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 != 1or transforms contain normalization.- Return type:
- on_validation_start()#
Calculate feature map statistics before validation.
Computes quantiles of feature maps on validation set and updates model.
- Return type:
- prepare_imagenette_data(image_size)#
Prepare ImageNette dataset transformations.
Sets up data transforms and downloads ImageNette dataset if not present.
- prepare_pretrained_model()#
Prepare the pretrained teacher model.
Downloads and loads pretrained weights for the teacher model if not already present.
- Return type:
- 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:
- Raises:
ValueError – If no data is provided (
nremainsNone).
- training_step(batch, *args, **kwargs)#
Perform training step.
Computes student, autoencoder and combined losses using both the input batch and a batch from ImageNette.
- validation_step(batch, *args, **kwargs)#
Perform validation step.
Generates anomaly maps for the input batch.
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
>>> import torch
>>> model = EfficientAdModel()
>>> model.eval()
>>> input_tensor = torch.randn(32, 3, 256, 256)
>>> output = model(input_tensor)
>>> output.anomaly_map.shape
torch.Size([32, 1, 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=384, model_size=EfficientAdModelSize.S, padding=False, pad_maps=True)#
Bases:
ModuleEfficientAd 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. Defaults to384.model_size (
EfficientAdModelSize) – Size of student and teacher model. Defaults toEfficientAdModelSize.S.padding (
bool) – Whether to use padding in convolutional layers. Defaults toFalse.pad_maps (
bool) – Whether to pad output anomaly maps whenpadding=Falseto match size of padded case. Only relevant ifpadding=False. Defaults toTrue.
Example
>>> from anomalib.models.image.efficient_ad.torch_model import ( ... EfficientAdModel, ... ) >>> model = EfficientAdModel() >>> model.eval() >>> 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).
- 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
- compute_maps(batch, student_output, distance_st, normalize=True)#
Compute anomaly maps from model outputs.
- Parameters:
- Returns:
Student-teacher anomaly map
Student-autoencoder anomaly map
- Return type:
- compute_student_teacher_distance(batch)#
Compute the student-teacher distance vectors.
- forward(batch, batch_imagenet=None, normalize=True)#
Forward pass through the model.
- Parameters:
- Returns:
- If training:
Loss components (student-teacher, autoencoder, student-autoencoder)
- If inference:
Batch containing anomaly maps and scores
- Return type:
- get_maps(batch, normalize=False)#
Compute anomaly maps for a batch of images.
Convenience method that combines distance computation and map generation.
- static is_set(p_dic)#
Check if any parameters in the dictionary are non-zero.
- Parameters:
p_dic (
ParameterDict) – Parameter dictionary to check.- Returns:
Trueif any parameter is non-zero,Falseotherwise.- Return type: