Transformers documentation

Backbone

You are viewing v4.39.0 version. A newer version v4.46.0 is available.
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Backbone

A backbone is a model used for feature extraction for higher level computer vision tasks such as object detection and image classification. Transformers provides an AutoBackbone class for initializing a Transformers backbone from pretrained model weights, and two utility classes:

  • BackboneMixin enables initializing a backbone from Transformers or timm and includes functions for returning the output features and indices.
  • BackboneConfigMixin sets the output features and indices of the backbone configuration.

timm models are loaded with the TimmBackbone and TimmBackboneConfig classes.

Backbones are supported for the following models:

AutoBackbone

class transformers.AutoBackbone

< >

( *args **kwargs )

BackboneMixin

class transformers.utils.BackboneMixin

< >

( )

to_dict

< >

( )

Serializes this instance to a Python dictionary. Override the default to_dict() from PretrainedConfig to include the out_features and out_indices attributes.

BackboneConfigMixin

class transformers.utils.BackboneConfigMixin

< >

( )

A Mixin to support handling the out_features and out_indices attributes for the backbone configurations.

to_dict

< >

( )

Serializes this instance to a Python dictionary. Override the default to_dict() from PretrainedConfig to include the out_features and out_indices attributes.

TimmBackbone

class transformers.TimmBackbone

< >

( config **kwargs )

Wrapper class for timm models to be used as backbones. This enables using the timm models interchangeably with the other models in the library keeping the same API.

TimmBackboneConfig

class transformers.TimmBackboneConfig

< >

( backbone = None num_channels = 3 features_only = True use_pretrained_backbone = True out_indices = None freeze_batch_norm_2d = False **kwargs )

Parameters

  • backbone (str, optional) — The timm checkpoint to load.
  • num_channels (int, optional, defaults to 3) — The number of input channels.
  • features_only (bool, optional, defaults to True) — Whether to output only the features or also the logits.
  • use_pretrained_backbone (bool, optional, defaults to True) — Whether to use a pretrained backbone.
  • out_indices (List[int], optional) — If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). Will default to the last stage if unset.
  • freeze_batch_norm_2d (bool, optional, defaults to False) — Converts all BatchNorm2d and SyncBatchNorm layers of provided module into FrozenBatchNorm2d.

This is the configuration class to store the configuration for a timm backbone TimmBackbone.

It is used to instantiate a timm backbone model according to the specified arguments, defining the model.

Configuration objects inherit from PretrainedConfig and can be used to control the model outputs. Read the documentation from PretrainedConfig for more information.

Example:

>>> from transformers import TimmBackboneConfig, TimmBackbone

>>> # Initializing a timm backbone
>>> configuration = TimmBackboneConfig("resnet50")

>>> # Initializing a model from the configuration
>>> model = TimmBackbone(configuration)

>>> # Accessing the model configuration
>>> configuration = model.config