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: Module

Extract 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.

backbone#

Name of the backbone model or actual torch backbone model.

Type:

str | nn.Module

layers#

Layer names for feature extraction.

Type:

list[str]

idx#

Indices mapping layer names to model outputs.

Type:

list[int]

requires_grad#

Whether gradients are computed.

Type:

bool

feature_extractor#

The underlying timm model.

Type:

nn.Module

out_dims#

Output dimensions for each extracted layer.

Type:

list[int]

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:

dict[str, torch.Tensor]

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:
  • feature_extractor (GraphModule) – Feature extraction model, either a TimmFeatureExtractor or GraphModule.

  • input_size (tuple[int, int]) – Tuple of (height, width) specifying input image dimensions.

  • layers (list[str]) – List of layer names from which to extract features.

Return type:

dict[str, dict[str, int | tuple[int, int]]]

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)