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, optional) – Preprocessor instance or bool flag. Defaults to True.
post_processor (PostProcessor | bool, optional) – Postprocessor instance or bool flag. Defaults to True.
evaluator (Evaluator | bool, optional) – Evaluator instance or bool flag. Defaults to True.
visualizer (Visualizer | bool, optional) – Visualizer instance or bool flag. Defaults to True.
- configure_optimizers()#
Configure optimizers for training.
- Returns:
Optimizers for student and target teacher.
- Return type:
- 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.
- forward(images)#
Forward pass of the teachers.
- Parameters:
images (torch.Tensor) – Input images.
- Returns:
Source features or source and target features.
- Return type:
- 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:
- forward(images, masks=None, labels=None)#
Forward pass of the UniNet model.
- Parameters:
images (torch.Tensor) – Input images.
masks (torch.Tensor | None) – Ground truth masks.
labels (torch.Tensor | None) – Ground truth labels.
- Returns:
Loss or InferenceBatch.
- Return type:
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.
- Parameters:
student_features (list[torch.Tensor]) – Student features.
teacher_features (list[torch.Tensor]) – Teacher features.
margin (int) – Hyperparameter for controlling the boundary.
mask (torch.Tensor | None) – Mask for the prediction. Mask is of shape Bx1xHxW
stop_gradient (bool) – Whether to stop the gradient into teacher features.
- Return type:
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:
batch_size (int) – Batch size.
output_list (list[torch.Tensor]) – List of output tensors, each with shape [batch_size, H, W].
alpha (float) – Alpha parameter. Used for controlling the upper limit
beta (float) – Beta parameter. Used for controlling the lower limit
- 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 have
planes * expansionchannels.stride (int, optional) – Stride for convolution layers. Defaults to
1.downsample (nn.Module | None, optional) – Module for downsampling the residual connection when dimensions don’t match. Defaults to
None.groups (int, optional) – Number of blocked connections from input to output channels. Defaults to
1.base_width (int, optional) – Base width for calculating intermediate channel width. Defaults to
64.norm_layer (Callable[..., nn.Module] | None, optional) – Normalization layer to use. If
None, usesBatchNorm2d. Defaults toNone.attention (bool, optional) – Whether to use attention mechanism. Defaults to
True.halve (int, optional) – 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.
- Parameters:
x (torch.Tensor) – Input tensor.
- Returns:
Output tensor.
- Return type:
- 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:
block (Type[AttentionBottleneck]) – Attention bottleneck.
layers (int) – Number of layers.
groups (int) – Number of groups.
width_per_group (int) – Width per group.
norm_layer (Callable[..., nn.Module] | None) – Normalization layer.
halve (int) – Number of halved channels.
- forward(x)#
Forward pass of the bottleneck.
- Parameters:
x (torch.Tensor) – Input tensor.
- Returns:
Output tensor.
- Return type:
- 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.
- Parameters:
conv (nn.Module) – Convolution layer.
bn (nn.Module) – Batch normalization layer.
- Returns:
Fused convolution and batch normalization layers.
- Return type:
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:
source_features (list[torch.Tensor]) – Source features.
target_features (list[torch.Tensor]) – Target features.
conv (bool) – Whether to use convolutional domain-related feature selection. Defaults to False.
maximize (bool) – Used for weights computation. If True, the weights are computed by subtracting the max value from the target feature. Defaults to True.
- Returns:
Domain related features.
- Return type: