SuperSimpleNet#
Architecture#
SuperSimpleNet.
ICPR 2024 - SuperSimpleNet: Unifying Unsupervised and Supervised Learning for Fast and Reliable Surface Defect Detection.
JIMS 2025 - No Label Left Behind: A Unified Surface Defect Detection Model for all Supervision Regimes
This module implements the SuperSimpleNet model for surface defect / anomaly detection. SuperSimpleNet is a simple yet strong discriminative model consisting of a pretrained feature extractor with upscaling, feature adaptor, train-time feature-level synthetic anomaly generation module, and segmentation-detection module.
Using the adapted features, the model predicts an anomaly map via the segmentation head and an anomaly score using the classification head. It delivers strong performance while maintaining fast inference.
Example
>>> from anomalib.data import MVTecAD
>>> from anomalib.models import Supersimplenet
>>> from anomalib.engine import Engine
>>> datamodule = MVTecAD()
>>> model = Supersimplenet()
>>> engine = Engine()
>>> engine.fit(model, datamodule=datamodule)
>>> predictions = engine.predict(model, datamodule=datamodule)
- Paper:
Original: SuperSimpleNet: Unifying Unsupervised and Supervised Learning for Fast and Reliable Surface Defect Detection. URL: https://arxiv.org/pdf/2408.03143
Extension: No label left behind: a unified surface defect detection model for all supervision regimes URL: https://link.springer.com/article/10.1007/s10845-025-02680-8
Notes
This implementation supports both unsupervised and supervised setting, but Anomalib currently supports only unsupervised learning.
See also
anomalib.models.image.supersimplenet.torch_model.SupersimplenetModel:PyTorch implementation of the SuperSimpleNet model.
- class anomalib.models.image.supersimplenet.lightning_model.Supersimplenet(perlin_threshold=0.2, backbone='wide_resnet50_2.tv_in1k', layers=['layer2', 'layer3'], supervised=False, adapt_cls_features=False, pre_processor=True, post_processor=True, evaluator=True, visualizer=True)#
Bases:
AnomalibModulePL Lightning Module for the SuperSimpleNet model.
- Parameters:
perlin_threshold (
float) – threshold value for Perlin noise thresholding during anomaly generation.backbone (
str) – backbone name. IMPORTANT! use only backbones with torchvision V1 weights ending on “.tv”.layers (
list[str]) – backbone layers utilisedsupervised (
bool) – whether the model will be trained in supervised mode. False by default (unsupervised).adapt_cls_features (
bool) – whether to adapt classification features (ICPR - True, JIMS - False (default)).pre_processor (
PreProcessor|bool) – Pre-processor instance or flag to use default. Defaults toTrue.post_processor (
PostProcessor|bool) – Post-processor instance or flag to use default. Defaults toTrue.evaluator (
Evaluator|bool) – Evaluator instance or flag to use default. Defaults toTrue.visualizer (
Visualizer|bool) – Visualizer instance or flag to use default. Defaults toTrue.
- configure_optimizers()#
Configure AdamW optimizer and MultiStepLR scheduler.
- Return type:
Union[Optimizer,Sequence[Optimizer],tuple[Sequence[Optimizer],Sequence[Union[LRScheduler,ReduceLROnPlateau,LRSchedulerConfig]]],OptimizerConfig,OptimizerLRSchedulerConfig,Sequence[OptimizerConfig],Sequence[OptimizerLRSchedulerConfig],None]
- classmethod configure_pre_processor(image_size=None)#
Configure the default pre-processor for SuperSimpleNet.
Pre-processor resizes images and normalizes using ImageNet statistics.
- Parameters:
image_size (
tuple[int,int] |None) – Target size for resizing. Defaults to(256, 256).- Returns:
Configured SuperSimpleNet pre-processor
- Return type:
- property learning_type: LearningType#
Return the learning type of the model.
This is subject to change in the future when support for supervised training is introduced.
- Returns:
Learning type of the model.
- Return type:
LearningType
- property trainer_arguments: dict[str, Any]#
Return SuperSimpleNet trainer arguments.
- training_step(batch, *args, **kwargs)#
Perform the training step input and return the loss.
- Parameters:
(batch (batch) – dict[str, str | torch.Tensor]): Input batch
args – Additional arguments.
kwargs – Additional keyword arguments.
- Returns:
Dictionary containing the loss value.
- Return type:
Union[Tensor,Mapping[str,Any],None]
- validation_step(batch, *args, **kwargs)#
Perform the validation step and return the anomaly map and anomaly score.
- Parameters:
batch (
Batch) – Input batchargs – Additional arguments.
kwargs – Additional keyword arguments.
- Returns:
batch dictionary containing anomaly-maps.
- Return type:
Union[Tensor,Mapping[str,Any],None]
PyTorch model for the SuperSimpleNet model implementation.
See also
anomalib.models.image.supersimplenet.lightning_model.Supersimplenet:SuperSimpleNet Lightning model.
- class anomalib.models.image.supersimplenet.torch_model.AnomalyMapGenerator(sigma)#
Bases:
ModuleFinal anomaly map generator, responsible for upscaling and smoothing.
- Parameters:
sigma (
float)
- forward(out_map, final_size)#
Upscale and smooth anomaly map to get final anomaly map of same size as input image.
- Parameters:
out_map (
Tensor) – output anomaly map from seg. head.final_size (
tuple[int,int]) – size (h, w) of final anomaly map.
- Returns:
final anomaly map.
- Return type:
Tensor
- class anomalib.models.image.supersimplenet.torch_model.FeatureAdapter(channel_dim)#
Bases:
ModuleFeature adapter used to adapt raw features for the task of anomaly detection.
- Parameters:
channel_dim (
int) – channel dimension of features.
- forward(features)#
Adapt features.
- Parameters:
features (
Tensor) – input features- Return type:
Tensor- Returns:
(torch.Tensor) adapted features
- class anomalib.models.image.supersimplenet.torch_model.SegmentationDetectionModule(channel_dim, stop_grad=False)#
Bases:
ModuleSegmentationDetection module responsible for prediction of anomaly map and score.
- Parameters:
channel_dim (
int) – channel dimension of features.stop_grad (
bool) – whether to stop gradient from class. head to seg. head.
- forward(seg_features, cls_features)#
Predict anomaly map and anomaly score.
- Parameters:
seg_features (
Tensor) – segmentation head features.cls_features (
Tensor) – classification head features.
- Return type:
tuple[Tensor,Tensor]- Returns:
predicted anomaly map and score.
- class anomalib.models.image.supersimplenet.torch_model.SupersimplenetModel(perlin_threshold=0.2, backbone='wide_resnet50_2.tv_in1k', layers=['layer2', 'layer3'], stop_grad=True, adapt_cls_features=False)#
Bases:
ModuleSuperSimpleNet Pytorch model.
It consists of feature extractor, feature adaptor, anomaly generation mechanism and segmentation-detection module.
- Parameters:
perlin_threshold (
float) – threshold value for Perlin noise thresholding during anomaly generation.backbone (
str) – backbone name. IMPORTANT! use only backbones with torchvision V1 weights ending on “.tv”.layers (
list[str]) – backbone layers utilisedstop_grad (
bool) – whether to stop gradient from class. to seg. head.adapt_cls_features (
bool) – whether to adapt classification features (ICPR - True, JIMS - False (default)).
- static downsample_mask(masks, feat_h, feat_w)#
Downsample the masks according to the feature dimensions.
Primarily used in supervised setting.
- Parameters:
masks (
Tensor) – input GT masksfeat_h (
int) – feature height.feat_w (
int) – feature width.
- Returns:
downsampled masks.
- Return type:
Tensor
- forward(images, masks=None, labels=None)#
SuperSimpleNet forward pass.
Extract and process features, adapt them, generate anomalies (train only) and predict anomaly map and score.
- Parameters:
images (
Tensor) – Input images.masks (
Tensor|None) – GT masks.labels (
Tensor|None) – GT labels.
- Returns:
anomaly map and score training: anomaly map, score and GT masks and labels
- Return type:
InferenceBatch|tuple[Tensor,Tensor,Tensor,Tensor]
- class anomalib.models.image.supersimplenet.torch_model.UpscalingFeatureExtractor(backbone, layers, patch_size=3)#
Bases:
ModuleFeature extractor module.
- Parameters:
backbone (
str) – backbone name.layers (
list[str]) – list of layers used for extraction.
- forward(input_tensor)#
Extract features from input tensor.
- Parameters:
input_tensor (
Tensor) – input tensor (images)- Returns:
extracted feature map.
- Return type:
Tensor
- get_channels_dim()#
Get feature channel dimension.
- Returns:
feature channel dimension.
- Return type:
int
- anomalib.models.image.supersimplenet.torch_model.init_weights(module)#
Init weight of the model.
- Parameters:
module (
Module) – torch module.- Return type:
None
Anomaly generator for the SuperSimplenet model implementation.
- class anomalib.models.image.supersimplenet.anomaly_generator.AnomalyGenerator(noise_mean, noise_std, threshold)#
Bases:
ModuleAnomaly generator for the SuperSimpleNet model.
- Parameters:
noise_mean (
float) – Mean of the Gaussian noise distribution.noise_std (
float) – Standard deviation of the Gaussian noise distribution.threshold (
float) – Threshold used to binarize Perlin noise.
- forward(input_features, adapted_features, masks, labels)#
Generate anomaly on features using thresholded perlin noise and Gaussian noise.
Also update GT masks and labels with new anomaly information.
- Parameters:
input_features (
Tensor|None) – input features. Set to None if we only need adapted.adapted_features (
Tensor) – adapted input features.masks (
Tensor) – GT masks.labels (
Tensor) – GT labels.
- Return type:
tuple[Tensor,Tensor,Tensor,Tensor]- Returns:
perturbed features (if not None), perturbed adapted, updated GT masks and labels.
- generate_perlin(batches, height, width)#
Generate 2d perlin noise masks with dims [b, 1, h, w].
- Parameters:
batches (
int) – number of batches (different masks)height (
int) – height of featureswidth (
int) – width of features
- Return type:
Tensor- Returns:
tensor with b perlin binarized masks
- static next_power_2(num)#
Get the next power of 2 for given number.
- Parameters:
num (
int) – value of interest- Return type:
int- Returns:
next power of 2 value for given number