UniNet#
Lightning model for UniNet.
- class anomalib.models.image.uninet.lightning_model.UniNet(student_backbone='wide_resnet50_2', teacher_backbone='wide_resnet50_2', temperature=0.1, pre_processor=True, post_processor=True, evaluator=True, visualizer=True)#
Bases:
AnomalibModuleUniNet model for anomaly detection.
- Parameters:
student_backbone (
str) – The backbone model to use for the student network. Defaults to “wide_resnet50_2”.teacher_backbone (
str) – The backbone model to use for the teacher network. Defaults to “wide_resnet50_2”.temperature (
float) – Temperature parameter used for contrastive loss. Controls the temperature of the student and teacher similarity computation. Defaults to 0.1.pre_processor (
PreProcessor|bool) – Preprocessor instance or bool flag. Defaults to True.post_processor (
PostProcessor|bool) – Postprocessor instance or bool flag. Defaults to True.evaluator (
Evaluator|bool) – Evaluator instance or bool flag. Defaults to True.visualizer (
Visualizer|bool) – Visualizer instance or bool flag. Defaults to True.
- configure_optimizers()#
Configure optimizers for training.
- property learning_type: LearningType#
The model uses one-class learning.
- training_step(batch, *args, **kwargs)#
Perform a training step of UniNet.
PyTorch model for UniNet.
See also
anomalib.models.image.uninet.lightning_model.UniNet:UniNet Lightning model.
- class anomalib.models.image.uninet.torch_model.Teachers(teacher_backbone)#
Bases:
ModuleTeachers module for UniNet.
- Parameters:
source_teacher (nn.Module) – Source teacher model.
target_teacher (nn.Module | None) – Target teacher model.
- class anomalib.models.image.uninet.torch_model.UniNetModel(student_backbone, teacher_backbone, loss)#
Bases:
ModuleUniNet PyTorch model.
It consists of teachers, student, and bottleneck modules.
- Parameters:
Loss functions for UniNet.
- class anomalib.models.image.uninet.components.loss.UniNetLoss(lambda_weight=0.7, temperature=2.0)#
Bases:
ModuleLoss function for UniNet.
- Parameters:
- forward(student_features, teacher_features, margin=1, mask=None, stop_gradient=False)#
Compute the loss.
Utilities for computing anomaly maps.
- anomalib.models.image.uninet.components.anomaly_map.weighted_decision_mechanism(batch_size, output_list, alpha, beta, output_size=(256, 256))#
Compute anomaly maps using weighted decision mechanism.
- Parameters:
- Returns:
Anomaly score and anomaly map.
- Return type:
Attention Bottleneck for UniNet.
- class anomalib.models.image.uninet.components.attention_bottleneck.AttentionBottleneck(inplanes, planes, stride=1, downsample=None, groups=1, base_width=64, norm_layer=None, attention=True, halve=1)#
Bases:
ModuleAttention Bottleneck block for UniNet with dual-branch processing.
This module implements a specialized bottleneck block that can operate in two modes: - Standard bottleneck (when halve=1) - Dual-branch attention mechanism (when halve=2)
The dual-branch mode splits input channels and processes them through different kernel sizes (3x3 and 7x7) to capture features at different receptive field scales, then fuses them before the final expansion layer.
- Architecture:
Input:
inplaneschannelsIntermediate:
widthchannels (compressed for efficiency)Output:
planes * expansionchannels (expanded representation)
- Parameters:
inplanes (
int) – Number of input channels.planes (
int) – Base number of channels for intermediate processing. Final output will haveplanes * expansionchannels.stride (
int) – Stride for convolution layers. Defaults to1.downsample (
Module|None) – Module for downsampling the residual connection when dimensions don’t match. Defaults toNone.groups (
int) – Number of blocked connections from input to output channels. Defaults to1.base_width (
int) – Base width for calculating intermediate channel width. Defaults to64.norm_layer (
Callable[...,Module] |None) – Normalization layer to use. IfNone, usesBatchNorm2d. Defaults toNone.attention (
bool) – Whether to use attention mechanism. Defaults toTrue.halve (
int) – Controls processing mode: -1: Standard bottleneck processing -2: Dual-branch processing with 3x3 and 7x7 kernels Defaults to1.
- channel_expansion#
Channel expansion factor. Final output channels will be
planes * channel_expansion. Set to4following ResNet conventions.- Type:
Example
>>> import torch >>> from anomalib.models.image.uninet.components.attention_bottleneck import ( ... AttentionBottleneck ... ) >>> # Standard bottleneck >>> block = AttentionBottleneck(256, 64, halve=1) >>> x = torch.randn(32, 256, 28, 28) >>> output = block(x) >>> output.shape # Output: 32, 256, 28, 28 (64 * 4 = 256) torch.Size([32, 256, 28, 28])
>>> # Dual-branch attention bottleneck >>> block = AttentionBottleneck(512, 128, halve=2) >>> x = torch.randn(32, 512, 14, 14) >>> output = block(x) >>> output.shape # Output: 32, 512, 14, 14 (128 * 4 = 512) torch.Size([32, 512, 14, 14])
Notes
When
halve=2, input is split into two branches processed by 3x3 and 7x7 convolutions respectively to capture multi-scale featuresThe
merge_kernelmethod can fuse batch norm parameters into convolution weights for inference optimizationResidual connections are used following ResNet design principles
The
widthcalculation:int(planes * (base_width / 64.0)) * groupsThe
channel_expansionis set to4following ResNet conventions.
See also
BottleneckLayer: Container for multiple AttentionBottleneck blocksfuse_bn(): Function for fusing batch normalization into convolution layers
- forward(x)#
Forward pass of the bottleneck.
- get_same_kernel_bias()#
Get same kernel and bias of the bottleneck.
- class anomalib.models.image.uninet.components.attention_bottleneck.BottleneckLayer(block, layers, groups=1, width_per_group=64, norm_layer=None, halve=2)#
Bases:
ModuleBatch Normalization layer for UniNet.
- Parameters:
- anomalib.models.image.uninet.components.attention_bottleneck.conv1x1(in_planes, out_planes, stride=1)#
1x1 convolution.
- anomalib.models.image.uninet.components.attention_bottleneck.conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1)#
3x3 convolution with padding.
- anomalib.models.image.uninet.components.attention_bottleneck.fuse_bn(conv, bn)#
Fuse convolution and batch normalization layers.
Domain Related Feature Selection.
- class anomalib.models.image.uninet.components.dfs.DomainRelatedFeatureSelection(num_channels=256, learnable=True)#
Bases:
ModuleDomain Related Feature Selection.
It is used to select the domain-related features from the source and target features.
- Parameters:
- forward(source_features, target_features, conv=False, maximize=True)#
Domain related feature selection.
- Parameters:
- Returns:
Domain related features.
- Return type: