Skip to content

Backbone

Backbone creation and discovery via timm.

BackboneConfig

Configuration dataclass for timm backbones.

API Reference

autotimm.BackboneConfig dataclass

Configuration for a timm backbone.

Attributes:

Name Type Description
model_name str

Name recognized by timm.create_model (e.g. "resnet50").

pretrained bool

Whether to load pretrained weights.

num_classes int

Set to 0 to get the feature extractor without the classification head (the head is provided separately by the task).

drop_rate float

Dropout rate applied inside the model.

drop_path_rate float

Stochastic depth rate.

extra_kwargs dict[str, Any]

Forwarded verbatim to timm.create_model.

Source code in src/autotimm/core/backbone.py
@dataclass
class BackboneConfig:
    """Configuration for a timm backbone.

    Attributes:
        model_name: Name recognized by ``timm.create_model`` (e.g. ``"resnet50"``).
        pretrained: Whether to load pretrained weights.
        num_classes: Set to 0 to get the feature extractor without the
            classification head (the head is provided separately by the task).
        drop_rate: Dropout rate applied inside the model.
        drop_path_rate: Stochastic depth rate.
        extra_kwargs: Forwarded verbatim to ``timm.create_model``.
    """

    model_name: str = "resnet50"
    pretrained: bool = True
    num_classes: int = 0
    drop_rate: float = 0.0
    drop_path_rate: float = 0.0
    extra_kwargs: dict[str, Any] = field(default_factory=dict)

Usage Examples

Basic Configuration

from autotimm import BackboneConfig

cfg = BackboneConfig(
    model_name="resnet50",
    pretrained=True,
)

With Regularization

cfg = BackboneConfig(
    model_name="vit_base_patch16_224",
    pretrained=True,
    drop_rate=0.1,           # Dropout
    drop_path_rate=0.1,      # Stochastic depth
)

With Extra kwargs

cfg = BackboneConfig(
    model_name="efficientnet_b3",
    pretrained=True,
    extra_kwargs={
        "global_pool": "avg",
        "in_chans": 3,
    },
)

Parameters

Parameter Type Default Description
model_name str "resnet50" timm model name
pretrained bool True Load pretrained weights
num_classes int 0 0 for feature extractor
drop_rate float 0.0 Dropout rate
drop_path_rate float 0.0 Stochastic depth rate
extra_kwargs dict {} Extra timm.create_model kwargs

create_backbone

Create a timm backbone from a config or model name.

API Reference

autotimm.create_backbone

create_backbone(cfg: BackboneConfig | str) -> nn.Module

Create a timm backbone from a config or model name string.

When cfg is a plain string it is treated as a model name with pretrained=True and num_classes=0 (headless).

Supports both timm models and Hugging Face Hub models
  • Timm models: Use the model name directly (e.g., "resnet50").
  • HF Hub models: Use the hf-hub: prefix (e.g., "hf-hub:timm/resnet50.a1_in1k").

Examples::

create_backbone("resnet50")  # Standard timm model
create_backbone("hf-hub:timm/resnet50.a1_in1k")  # HF Hub model

Raises ValueError if the model name is not found.

Source code in src/autotimm/core/backbone.py
def create_backbone(cfg: BackboneConfig | str) -> nn.Module:
    """Create a timm backbone from a config or model name string.

    When *cfg* is a plain string it is treated as a model name with
    ``pretrained=True`` and ``num_classes=0`` (headless).

    Supports both timm models and Hugging Face Hub models:
        - Timm models: Use the model name directly (e.g., ``"resnet50"``).
        - HF Hub models: Use the ``hf-hub:`` prefix (e.g., ``"hf-hub:timm/resnet50.a1_in1k"``).

    Examples::

        create_backbone("resnet50")  # Standard timm model
        create_backbone("hf-hub:timm/resnet50.a1_in1k")  # HF Hub model

    Raises ``ValueError`` if the model name is not found.
    """
    if isinstance(cfg, str):
        cfg = BackboneConfig(model_name=cfg)

    # Skip validation for HF Hub models as they're not in timm.list_models()
    if not _is_hf_hub_model(cfg.model_name):
        available = timm.list_models()
        if cfg.model_name not in available:
            raise ValueError(
                f"Model '{cfg.model_name}' not found in timm. "
                f"Use autotimm.list_backbones('*pattern*') to search, "
                f"or use 'hf-hub:' prefix for Hugging Face Hub models."
            )

    return timm.create_model(
        cfg.model_name,
        pretrained=cfg.pretrained,
        num_classes=cfg.num_classes,
        drop_rate=cfg.drop_rate,
        drop_path_rate=cfg.drop_path_rate,
        **cfg.extra_kwargs,
    )

Usage Examples

From String

import autotimm as at  # recommended alias

backbone = at.create_backbone("resnet50")
print(f"Output features: {backbone.num_features}")

From Config

from autotimm import BackboneConfig, create_backbone

cfg = BackboneConfig(
    model_name="vit_base_patch16_224",
    pretrained=True,
    drop_path_rate=0.1,
)
backbone = create_backbone(cfg)

Parameters

Parameter Type Description
cfg BackboneConfig \| str Config or model name

Returns

Type Description
nn.Module Headless timm model

Raises

Exception Condition
ValueError Model name not found in timm

FeatureBackboneConfig

Configuration dataclass for feature extraction backbones used in object detection.

API Reference

autotimm.FeatureBackboneConfig dataclass

Configuration for a timm backbone with multi-scale feature extraction.

Used for tasks like object detection that require feature maps at multiple scales (e.g., C2, C3, C4, C5 from ResNet).

Attributes:

Name Type Description
model_name str

Name recognized by timm.create_model (e.g. "resnet50").

pretrained bool

Whether to load pretrained weights.

out_indices tuple[int, ...]

Tuple of stage indices to extract features from. Default (1, 2, 3, 4) extracts C2, C3, C4, C5 for most architectures.

drop_rate float

Dropout rate applied inside the model.

drop_path_rate float

Stochastic depth rate.

extra_kwargs dict[str, Any]

Forwarded verbatim to timm.create_model.

Source code in src/autotimm/core/backbone.py
@dataclass
class FeatureBackboneConfig:
    """Configuration for a timm backbone with multi-scale feature extraction.

    Used for tasks like object detection that require feature maps at multiple
    scales (e.g., C2, C3, C4, C5 from ResNet).

    Attributes:
        model_name: Name recognized by ``timm.create_model`` (e.g. ``"resnet50"``).
        pretrained: Whether to load pretrained weights.
        out_indices: Tuple of stage indices to extract features from.
            Default (1, 2, 3, 4) extracts C2, C3, C4, C5 for most architectures.
        drop_rate: Dropout rate applied inside the model.
        drop_path_rate: Stochastic depth rate.
        extra_kwargs: Forwarded verbatim to ``timm.create_model``.
    """

    model_name: str = "resnet50"
    pretrained: bool = True
    out_indices: tuple[int, ...] = (1, 2, 3, 4)
    drop_rate: float = 0.0
    drop_path_rate: float = 0.0
    extra_kwargs: dict[str, Any] = field(default_factory=dict)

Usage Examples

Basic Feature Extraction

from autotimm import FeatureBackboneConfig, create_feature_backbone

cfg = FeatureBackboneConfig(
    model_name="resnet50",
    pretrained=True,
    out_indices=(2, 3, 4),  # Extract C3, C4, C5
)

backbone = create_feature_backbone(cfg)
features = backbone(images)  # Returns [C3, C4, C5] features

With Regularization

cfg = FeatureBackboneConfig(
    model_name="swin_tiny_patch4_window7_224",
    pretrained=True,
    out_indices=(1, 2, 3),
    drop_rate=0.1,
    drop_path_rate=0.1,
)

Parameters

Parameter Type Default Description
model_name str "resnet50" timm model name
pretrained bool True Load pretrained weights
out_indices tuple[int, ...] (2, 3, 4) Feature levels to extract (C3, C4, C5)
drop_rate float 0.0 Dropout rate
drop_path_rate float 0.0 Stochastic depth rate
extra_kwargs dict {} Extra timm.create_model kwargs

Feature Levels

Different backbones have different feature hierarchies:

CNN Backbones (ResNet, EfficientNet, ConvNeXt):

Index Name Stride Resolution (640px input)
0 C1 2 320×320
1 C2 4 160×160
2 C3 8 80×80
3 C4 16 40×40
4 C5 32 20×20

Transformer Backbones (ViT, Swin, DeiT):

Index Name Description
0-3 Stage 0-3 Hierarchical features (Swin) or patch embeddings (ViT)

Common Configurations

For FCOS Detection

# CNN backbone
cfg = FeatureBackboneConfig(
    model_name="resnet50",
    out_indices=(2, 3, 4),  # C3, C4, C5 for FPN
)

# Swin Transformer
cfg = FeatureBackboneConfig(
    model_name="swin_tiny_patch4_window7_224",
    out_indices=(1, 2, 3),  # Stage 1, 2, 3
)

create_feature_backbone

Create a feature extraction backbone from a config or model name.

API Reference

autotimm.create_feature_backbone

create_feature_backbone(cfg: FeatureBackboneConfig | str) -> nn.Module

Create a timm backbone for multi-scale feature extraction.

Uses timm's features_only=True mode to extract intermediate feature maps, typically used for object detection, segmentation, etc.

When cfg is a plain string it is treated as a model name with pretrained=True and default out_indices=(1, 2, 3, 4).

Supports both timm models and Hugging Face Hub models
  • Timm models: Use the model name directly (e.g., "resnet50").
  • HF Hub models: Use the hf-hub: prefix (e.g., "hf-hub:timm/resnet50.a1_in1k").

Returns a model that outputs a list of feature tensors, one per stage.

Examples::

create_feature_backbone("resnet50")  # Standard timm model
create_feature_backbone("hf-hub:timm/convnext_base.fb_in22k_ft_in1k")  # HF Hub model

Raises ValueError if the model name is not found.

Source code in src/autotimm/core/backbone.py
def create_feature_backbone(cfg: FeatureBackboneConfig | str) -> nn.Module:
    """Create a timm backbone for multi-scale feature extraction.

    Uses timm's ``features_only=True`` mode to extract intermediate feature maps,
    typically used for object detection, segmentation, etc.

    When *cfg* is a plain string it is treated as a model name with
    ``pretrained=True`` and default ``out_indices=(1, 2, 3, 4)``.

    Supports both timm models and Hugging Face Hub models:
        - Timm models: Use the model name directly (e.g., ``"resnet50"``).
        - HF Hub models: Use the ``hf-hub:`` prefix (e.g., ``"hf-hub:timm/resnet50.a1_in1k"``).

    Returns a model that outputs a list of feature tensors, one per stage.

    Examples::

        create_feature_backbone("resnet50")  # Standard timm model
        create_feature_backbone("hf-hub:timm/convnext_base.fb_in22k_ft_in1k")  # HF Hub model

    Raises ``ValueError`` if the model name is not found.
    """
    if isinstance(cfg, str):
        cfg = FeatureBackboneConfig(model_name=cfg)

    # Skip validation for HF Hub models as they're not in timm.list_models()
    if not _is_hf_hub_model(cfg.model_name):
        available = timm.list_models()
        if cfg.model_name not in available:
            raise ValueError(
                f"Model '{cfg.model_name}' not found in timm. "
                f"Use autotimm.list_backbones('*pattern*') to search, "
                f"or use 'hf-hub:' prefix for Hugging Face Hub models."
            )

    return timm.create_model(
        cfg.model_name,
        pretrained=cfg.pretrained,
        features_only=True,
        out_indices=cfg.out_indices,
        drop_rate=cfg.drop_rate,
        drop_path_rate=cfg.drop_path_rate,
        **cfg.extra_kwargs,
    )

Usage Examples

From String

import autotimm as at  # recommended alias

backbone = at.create_feature_backbone("resnet50")
features = backbone(images)
print(f"Feature levels: {len(features)}")  # 3 (C3, C4, C5)

From Config

from autotimm import FeatureBackboneConfig, create_feature_backbone

cfg = FeatureBackboneConfig(
    model_name="resnet50",
    pretrained=True,
    out_indices=(2, 3, 4),
)
backbone = create_feature_backbone(cfg)

Parameters

Parameter Type Description
cfg FeatureBackboneConfig \| str Config or model name

Returns

Type Description
nn.Module Feature extraction backbone

Feature Utilities

get_feature_info

Get feature information (channels, strides, reductions) from a backbone.

autotimm.get_feature_info

get_feature_info(backbone: Module) -> list[dict[str, Any]]

Return feature info for a backbone created with features_only=True.

Each dict contains
  • num_chs: Number of output channels
  • reduction: Spatial reduction factor (stride) relative to input
  • module: Name of the module producing this feature

Only returns info for features that are actually output (respecting out_indices).

Raises AttributeError if the backbone was not created with features_only=True.

Source code in src/autotimm/core/backbone.py
def get_feature_info(backbone: nn.Module) -> list[dict[str, Any]]:
    """Return feature info for a backbone created with features_only=True.

    Each dict contains:
        - ``num_chs``: Number of output channels
        - ``reduction``: Spatial reduction factor (stride) relative to input
        - ``module``: Name of the module producing this feature

    Only returns info for features that are actually output (respecting out_indices).

    Raises ``AttributeError`` if the backbone was not created with features_only=True.
    """
    if not hasattr(backbone, "feature_info"):
        raise AttributeError(
            "Backbone does not have 'feature_info'. "
            "Ensure it was created with features_only=True."
        )
    # Use get_dicts() to get info only for the output indices
    return backbone.feature_info.get_dicts()
import autotimm as at  # recommended alias

backbone = at.create_feature_backbone("resnet50")
info = at.get_feature_info(backbone)
print(info)
# [{'num_chs': 512, 'reduction': 8, ...},
#  {'num_chs': 1024, 'reduction': 16, ...},
#  {'num_chs': 2048, 'reduction': 32, ...}]

get_feature_channels

Extract feature channels for each level.

autotimm.get_feature_channels

get_feature_channels(backbone: Module) -> list[int]

Return the number of channels for each feature level.

Convenience function that extracts just the channel counts from feature_info. Only returns channels for features that are actually output (respecting out_indices).

Source code in src/autotimm/core/backbone.py
def get_feature_channels(backbone: nn.Module) -> list[int]:
    """Return the number of channels for each feature level.

    Convenience function that extracts just the channel counts from feature_info.
    Only returns channels for features that are actually output (respecting out_indices).
    """
    if not hasattr(backbone, "feature_info"):
        raise AttributeError(
            "Backbone does not have 'feature_info'. "
            "Ensure it was created with features_only=True."
        )
    return backbone.feature_info.channels()
import autotimm as at  # recommended alias

backbone = at.create_feature_backbone("resnet50")
channels = at.get_feature_channels(backbone)
print(channels)  # [512, 1024, 2048]

get_feature_strides

Get stride information for FPN construction.

autotimm.get_feature_strides

get_feature_strides(backbone: Module) -> list[int]

Return the stride (spatial reduction) for each feature level.

Convenience function that extracts just the strides from feature_info. Only returns strides for features that are actually output (respecting out_indices).

Source code in src/autotimm/core/backbone.py
def get_feature_strides(backbone: nn.Module) -> list[int]:
    """Return the stride (spatial reduction) for each feature level.

    Convenience function that extracts just the strides from feature_info.
    Only returns strides for features that are actually output (respecting out_indices).
    """
    if not hasattr(backbone, "feature_info"):
        raise AttributeError(
            "Backbone does not have 'feature_info'. "
            "Ensure it was created with features_only=True."
        )
    return backbone.feature_info.reduction()
import autotimm as at  # recommended alias

backbone = at.create_feature_backbone("resnet50")
strides = at.get_feature_strides(backbone)
print(strides)  # [8, 16, 32]

list_backbones

List available timm backbones with optional filtering.

API Reference

autotimm.list_backbones

list_backbones(pattern: str = '', pretrained_only: bool = False, with_source: bool = False) -> list[str] | list[tuple[str, ModelSource]]

List available timm backbones, optionally filtered by glob pattern.

This lists models available in the timm library. To search Hugging Face Hub models, use list_hf_hub_backbones().

Parameters:

Name Type Description Default
pattern str

Glob pattern to filter model names (e.g., "*resnet*").

''
pretrained_only bool

If True, only list models with pretrained weights.

False
with_source bool

If True, return tuples of (model_name, ModelSource.TIMM).

False

Returns:

Type Description
list[str] | list[tuple[str, ModelSource]]

List of model names, or list of (model_name, source) tuples if

list[str] | list[tuple[str, ModelSource]]

with_source=True.

Examples::

list_backbones("*resnet*")
list_backbones("*efficientnet*", pretrained_only=True)
list_backbones("*vit*", with_source=True)  # [(name, ModelSource.TIMM), ...]
Source code in src/autotimm/core/backbone.py
def list_backbones(
    pattern: str = "",
    pretrained_only: bool = False,
    with_source: bool = False,
) -> list[str] | list[tuple[str, ModelSource]]:
    """List available timm backbones, optionally filtered by glob *pattern*.

    This lists models available in the timm library. To search Hugging Face Hub
    models, use ``list_hf_hub_backbones()``.

    Parameters:
        pattern: Glob pattern to filter model names (e.g., ``"*resnet*"``).
        pretrained_only: If True, only list models with pretrained weights.
        with_source: If True, return tuples of (model_name, ModelSource.TIMM).

    Returns:
        List of model names, or list of (model_name, source) tuples if
        ``with_source=True``.

    Examples::

        list_backbones("*resnet*")
        list_backbones("*efficientnet*", pretrained_only=True)
        list_backbones("*vit*", with_source=True)  # [(name, ModelSource.TIMM), ...]
    """
    models = timm.list_models(pattern or "*", pretrained=pretrained_only)
    if with_source:
        return [(name, ModelSource.TIMM) for name in models]
    return models

Usage Examples

List All Backbones

import autotimm as at  # recommended alias

all_models = at.list_backbones()
print(f"Total models: {len(all_models)}")  # 1000+

Search by Pattern

# ResNet variants
resnet_models = at.list_backbones("*resnet*")
print(resnet_models[:10])

# EfficientNet variants
efficientnet_models = at.list_backbones("*efficientnet*")

# Vision Transformers
vit_models = at.list_backbones("*vit*")

# ConvNeXt
convnext_models = at.list_backbones("*convnext*")

Pretrained Only

pretrained = at.list_backbones("*resnet*", pretrained_only=True)

Parameters

Parameter Type Default Description
pattern str "" Glob pattern (e.g., "*resnet*")
pretrained_only bool False Only pretrained models

Returns

Type Description
list[str] List of model names

CNNs

Family Models Notes
ResNet resnet18, resnet34, resnet50, resnet101, resnet152 Classic
ResNeXt resnext50_32x4d, resnext101_32x8d Grouped convolutions
EfficientNet efficientnet_b0 to efficientnet_b7 Compound scaling
EfficientNetV2 efficientnetv2_s, efficientnetv2_m, efficientnetv2_l Faster training
ConvNeXt convnext_tiny, convnext_small, convnext_base, convnext_large Modern CNN
RegNet regnetx_002, regnety_040 Efficient design
MobileNet mobilenetv3_small_100, mobilenetv3_large_100 Mobile-friendly

Transformers

Family Models Notes
ViT vit_tiny_patch16_224, vit_small_patch16_224, vit_base_patch16_224, vit_large_patch16_224 Vision Transformer
DeiT deit_tiny_patch16_224, deit_small_patch16_224, deit_base_patch16_224 Data-efficient ViT
Swin swin_tiny_patch4_window7_224, swin_small_patch4_window7_224, swin_base_patch4_window7_224 Hierarchical ViT
BEiT beit_base_patch16_224, beit_large_patch16_224 BERT-style pretraining

Hybrids

Family Models Notes
CoAtNet coatnet_0_rw_224, coatnet_1_rw_224 CNN + Attention
MaxViT maxvit_tiny_rw_224, maxvit_small_rw_224 Multi-axis attention

Backbone Info

import autotimm as at  # recommended alias

# Create backbone
backbone = at.create_backbone("resnet50")

# Get output features
print(f"Output features: {backbone.num_features}")  # 2048

# Count parameters
total = at.count_parameters(backbone, trainable_only=False)
print(f"Total parameters: {total:,}")  # 23,508,032