FRE#
Feature Reconstruction Error (FRE) Algorithm Implementation.
FRE is an anomaly detection model that uses feature reconstruction error to detect anomalies. The model extracts features from a pre-trained CNN backbone and learns to reconstruct them using a tied autoencoder. Anomalies are detected by measuring the reconstruction error between the original and reconstructed features.
Example
>>> from anomalib.data import MVTecAD
>>> from anomalib.models import Fre
>>> from anomalib.engine import Engine
>>> datamodule = MVTecAD()
>>> model = Fre()
>>> engine = Engine()
>>> engine.fit(model, datamodule=datamodule)
>>> predictions = engine.predict(model, datamodule=datamodule)
- Paper:
- Title: FRE: Feature Reconstruction Error for Unsupervised Anomaly Detection
and Segmentation
See also
anomalib.models.image.fre.torch_model.FREModel:PyTorch implementation of the FRE model architecture.
- class anomalib.models.image.fre.lightning_model.Fre(backbone='resnet50', layer='layer3', pre_trained=True, pooling_kernel_size=2, input_dim=65536, latent_dim=220, pre_processor=True, post_processor=True, evaluator=True, visualizer=True)#
Bases:
AnomalibModuleFRE: Feature-reconstruction error using Tied AutoEncoder.
The FRE model extracts features from a pre-trained CNN backbone and learns to reconstruct them using a tied autoencoder. Anomalies are detected by measuring the reconstruction error between original and reconstructed features.
- Parameters:
backbone (
str) – Backbone CNN network architecture. Defaults to"resnet50".layer (
str) – Layer name to extract features from the backbone CNN. Defaults to"layer3".pre_trained (
bool) – Whether to use pre-trained backbone weights. Defaults toTrue.pooling_kernel_size (
int) – Kernel size for pooling features extracted from the CNN. Defaults to2.input_dim (
int) – Dimension of features at output of specified layer. Defaults to65536.latent_dim (
int) – Reduced feature dimension after applying dimensionality reduction via shallow linear autoencoder. Defaults to220.pre_processor (
PreProcessor|bool) – Pre-processor to transform inputs before passing to model. Defaults toTrue.post_processor (
PostProcessor|bool) – Post-processor to generate predictions from model outputs. Defaults toTrue.evaluator (
Evaluator|bool) – Evaluator to compute metrics. Defaults toTrue.visualizer (
Visualizer|bool) – Visualizer to display results. Defaults toTrue.
Example
>>> from anomalib.models import Fre >>> model = Fre( ... backbone="resnet50", ... layer="layer3", ... pre_trained=True, ... pooling_kernel_size=2, ... input_dim=65536, ... latent_dim=220, ... )
See also
anomalib.models.image.fre.torch_model.FREModel:PyTorch implementation of the FRE model architecture.
- configure_optimizers()#
Configure optimizers.
- Returns:
Adam optimizer for training the model.
- Return type:
- property learning_type: LearningType#
Return the learning type of the model.
- Returns:
Learning type of the model (
ONE_CLASS).- Return type:
LearningType
- training_step(batch, *args, **kwargs)#
Perform the training step of FRE.
For each batch, features are extracted from the CNN backbone and reconstructed using the tied autoencoder. The loss is computed as the MSE between original and reconstructed features.
- validation_step(batch, *args, **kwargs)#
Perform the validation step of FRE.
Similar to training, features are extracted and reconstructed. The reconstruction error is used to compute anomaly scores and maps.
PyTorch model for the Feature Reconstruction Error (FRE) algorithm implementation.
The FRE model extracts features from a pre-trained CNN backbone and learns to reconstruct them using a tied autoencoder. Anomalies are detected by measuring the reconstruction error between original and reconstructed features.
Example
>>> from anomalib.models.image.fre.torch_model import FREModel
>>> model = FREModel(
... backbone="resnet50",
... layer="layer3",
... input_dim=65536,
... latent_dim=220,
... pre_trained=True,
... pooling_kernel_size=4
... )
>>> input_tensor = torch.randn(32, 3, 256, 256)
>>> output = model(input_tensor)
>>> output.pred_score.shape
torch.Size([32])
>>> output.anomaly_map.shape
torch.Size([32, 1, 256, 256])
- Paper:
- Title: FRE: Feature Reconstruction Error for Unsupervised Anomaly Detection
and Segmentation
See also
anomalib.models.image.fre.lightning_model.Fre:PyTorch Lightning implementation of the FRE model.
- class anomalib.models.image.fre.torch_model.FREModel(backbone, layer, input_dim=65536, latent_dim=220, pre_trained=True, pooling_kernel_size=4)#
Bases:
ModuleFeature Reconstruction Error (FRE) model implementation.
The model extracts features from a pre-trained CNN backbone and learns to reconstruct them using a tied autoencoder. Anomalies are detected by measuring the reconstruction error between original and reconstructed features.
- Parameters:
backbone (
str) – Pre-trained CNN backbone architecture (e.g."resnet18","resnet50", etc.).layer (
str) – Layer name from which to extract features (e.g."layer2","layer3", etc.).input_dim (
int) – Dimension of features at output of specified layer. Defaults to65536.latent_dim (
int) – Reduced feature dimension after applying dimensionality reduction via shallow linear autoencoder. Defaults to220.pre_trained (
bool) – Whether to use pre-trained backbone weights. Defaults toTrue.pooling_kernel_size (
int) – Kernel size for pooling features extracted from the CNN. Defaults to4.
Example
>>> model = FREModel( ... backbone="resnet50", ... layer="layer3", ... input_dim=65536, ... latent_dim=220 ... ) >>> input_tensor = torch.randn(32, 3, 256, 256) >>> output = model(input_tensor) >>> output.pred_score.shape torch.Size([32]) >>> output.anomaly_map.shape torch.Size([32, 1, 256, 256])
- forward(batch)#
Generate anomaly predictions for input images.
The method: 1. Extracts and reconstructs features using the tied autoencoder 2. Computes reconstruction error as anomaly scores 3. Generates pixel-wise anomaly maps 4. Upsamples anomaly maps to input image size
- Parameters:
batch (
Tensor) – Input image batch of shape(N, C, H, W).- Returns:
- Batch containing:
Anomaly scores of shape
(N,)Anomaly maps of shape
(N, 1, H, W)
- Return type:
- get_features(batch)#
Extract and reconstruct features from the pretrained network.
- class anomalib.models.image.fre.torch_model.TiedAE(input_dim, latent_dim)#
Bases:
ModuleTied Autoencoder used for feature reconstruction error calculation.
The tied autoencoder uses shared weights between encoder and decoder to reduce the number of parameters while maintaining reconstruction capability.
- Parameters:
Example
>>> tied_ae = TiedAE(input_dim=1024, latent_dim=128) >>> features = torch.randn(32, 1024) >>> reconstructed = tied_ae(features) >>> reconstructed.shape torch.Size([32, 1024])
- forward(features)#
Run input features through the autoencoder.
The features are first encoded to a lower dimensional latent space and then decoded back to the original feature space using transposed weights.