Feature Extractors#
Feature extractors for deep learning models.
This module provides feature extraction utilities and classes for extracting features from images using various backbone architectures.
- Classes:
TimmFeatureExtractor: Feature extractor using timm models. BackboneParams: Configuration parameters for backbone models.
- Functions:
dryrun_find_featuremap_dims: Utility to find feature map dimensions.
Example
>>> from anomalib.models.components.feature_extractors import (
... TimmFeatureExtractor
... )
>>> # Create feature extractor
>>> feature_extractor = TimmFeatureExtractor(
... backbone="resnet18",
... layers=['layer1', 'layer2']
... )
>>> # Extract features
>>> features = feature_extractor(images)
- class anomalib.models.components.feature_extractors.TimmFeatureExtractor(backbone, layers, pre_trained=True, requires_grad=False)#
Bases:
ModuleExtract intermediate features from timm models or any nn.Module torch model.
- Parameters:
backbone (str | nn.Module) – Name of the timm model architecture or any torch model to use as backbone.
layers (Sequence[str]) – Names of layers from which to extract features.
pre_trained (bool, optional) – Whether to use pre-trained weights. Defaults to
True.requires_grad (bool, optional) – Whether to compute gradients for the backbone. Required for training models like STFPM. Defaults to
False.
- feature_extractor#
The underlying timm model.
- Type:
nn.Module
Example
>>> import torch >>> import torchvision >>> from torchvision.models import efficientnet_b5, EfficientNet_B5_Weights
>>> from anomalib.models.components.feature_extractors import ( ... TimmFeatureExtractor ... ) >>> # Create extractor >>> model = TimmFeatureExtractor( ... backbone="resnet18", ... layers=["layer1", "layer2"] ... ) >>> # Extract features >>> inputs = torch.randn(1, 3, 224, 224) >>> features = model(inputs) >>> # Print shapes >>> for name, feat in features.items(): ... print(f"{name}: {feat.shape}") layer1: torch.Size([1, 64, 56, 56]) layer2: torch.Size([1, 128, 28, 28])
>>> # Custom backbone model >>> custom_backbone = efficientnet_b5(weights=EfficientNet_B5_Weights.IMAGENET1K_V1) >>> model = TimmFeatureExtractor( ... backbone=custom_backbone, ... layers=["features.6.8"]) >>> features = model(inputs) >>> # Print shapes >>> for name, feat in features.items(): ... print(f"{name}: {feat.shape}") features.6.8: torch.Size([32, 304, 8, 8])
- forward(inputs)#
Extract features from the input tensor.
- Parameters:
inputs (torch.Tensor) – Input tensor of shape
(batch_size, channels, height, width).- Returns:
Dictionary mapping layer names to their feature tensors.
- Return type:
Example
>>> import torch >>> from anomalib.models.components.feature_extractors import ( ... TimmFeatureExtractor ... ) >>> model = TimmFeatureExtractor( ... backbone="resnet18", ... layers=["layer1"] ... ) >>> inputs = torch.randn(1, 3, 224, 224) >>> features = model(inputs) >>> features["layer1"].shape torch.Size([1, 64, 56, 56])
- anomalib.models.components.feature_extractors.dryrun_find_featuremap_dims(feature_extractor, input_size, layers)#
Get feature map dimensions by running an empty tensor through the model.
Performs a forward pass with an empty tensor to determine the output dimensions of specified feature maps.
- Parameters:
- Return type:
- Returns:
Dictionary mapping layer names to dimension information. For each layer, returns a dictionary with:
num_features: Number of feature channels (int)resolution: Spatial dimensions as(height, width)tuple
Example
>>> extractor = TimmFeatureExtractor("resnet18", layers=["layer1"]) >>> dims = dryrun_find_featuremap_dims( ... extractor, ... input_size=(256, 256), ... layers=["layer1"] ... ) >>> print(dims["layer1"]["num_features"]) # channels 64 >>> print(dims["layer1"]["resolution"]) # (height, width) (64, 64)