Filters#
Filters used by anomaly detection models.
This module provides filter implementations that can be used for image preprocessing and feature enhancement in anomaly detection models.
- Classes:
GaussianBlur2d: 2D Gaussian blur filter implementation.
Example
>>> import torch
>>> from anomalib.models.components.filters import GaussianBlur2d
>>> # Create a Gaussian blur filter
>>> blur = GaussianBlur2d(kernel_size=3, sigma=1.0)
>>> # Apply blur to input tensor
>>> input_tensor = torch.randn(1, 3, 256, 256)
>>> blurred = blur(input_tensor)
- class anomalib.models.components.filters.GaussianBlur2d(sigma, channels=1, kernel_size=None, normalize=True, border_type='reflect', padding='same')#
Bases:
Module
2D Gaussian blur filter with pre-computed kernel.
Unlike some implementations, this class pre-computes the Gaussian kernel during initialization rather than computing it during the forward pass. This approach is more efficient but requires specifying the number of input channels upfront.
- Parameters:
sigma (float | tuple[float, float]) – Standard deviation(s) for the Gaussian kernel. If a single float is provided, it’s used for both dimensions.
channels (int) – Number of input channels. Defaults to 1.
kernel_size (int | tuple[int, int] | None) – Size of the Gaussian kernel. If
None
, computed from sigma. Defaults toNone
.normalize (bool) – Whether to normalize the kernel so its elements sum to 1. Defaults to
True
.border_type (str) – Padding mode for border handling. Options are ‘reflect’, ‘replicate’, etc. Defaults to “reflect”.
padding (str) – Padding strategy. Either ‘same’ or ‘valid’. Defaults to “same”.
Example
>>> import torch >>> blur = GaussianBlur2d(sigma=1.0, channels=3) >>> x = torch.randn(1, 3, 64, 64) >>> output = blur(x) >>> output.shape torch.Size([1, 3, 64, 64])
- forward(input_tensor)#
Apply Gaussian blur to input tensor.
- Parameters:
input_tensor (torch.Tensor) – Input tensor of shape
(B, C, H, W)
.- Returns:
- Blurred output tensor. If padding is ‘same’,
output shape matches input. If ‘valid’, output is smaller.
- Return type:
Example
>>> blur = GaussianBlur2d(sigma=1.0, channels=1) >>> x = torch.ones(1, 1, 5, 5) >>> output = blur(x) >>> output.shape torch.Size([1, 1, 5, 5])