class_docstr: 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.
class_docstr_tok: ["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","."]
code: def __init__(self, config, **kwargs):
      requires_backends(self, "timm")
      super().__init__(config)
      self.config = config
  
      if config.backbone is None:
          raise ValueError(
              "backbone is not set in the config. Please set it to a timm model name."
          )
  
      if config.backbone not in timm.list_models():
          raise ValueError(
              f"backbone {config.backbone} is not supported by timm."
          )
  
      if (
          hasattr(config, "out_features")
          and config.out_features is not None
      ):
          raise ValueError(
              "out_features is not supported by TimmBackbone. Please use out_indices instead."
          )
  
      pretrained = getattr(config, "use_pretrained_backbone", None)
      if pretrained is None:
          raise ValueError(
              "use_pretrained_backbone is not set in the config. Please set it to True or False."
          )
  
      # We just take the final layer by default. This matches the default for the transformers models.
      out_indices = (
          config.out_indices
          if getattr(config, "out_indices", None) is not None
          else (-1,)
      )
  
      self._backbone = timm.create_model(
          config.backbone,
          pretrained=pretrained,
          # This is currently not possible for transformer architectures.
          features_only=config.features_only,
          in_chans=config.num_channels,
          out_indices=out_indices,
          **kwargs,
      )
  
      # Converts all `BatchNorm2d` and `SyncBatchNorm` or `BatchNormAct2d` and `SyncBatchNormAct2d` layers of provided module into `FrozenBatchNorm2d` or `FrozenBatchNormAct2d` respectively
      if getattr(config, "freeze_batch_norm_2d", False):
          self.freeze_batch_norm_2d()
  
      # These are used to control the output of the model when called. If output_hidden_states is True, then
      # return_layers is modified to include all layers.
      self._return_layers = self._backbone.return_layers
      self._all_layers = {
          layer["module"]: str(i)
          for i, layer in enumerate(self._backbone.feature_info.info)
      }
      super()._init_backbone(config)
  
file_path: transformers/src/transformers/models/timm_backbone/modeling_timm_backbone.py
imports: from typing import Optional, Tuple, Union
  import torch
  from ...modeling_outputs import BackboneOutput
  from ...modeling_utils import PreTrainedModel
  from ...utils import is_timm_available, is_torch_available, requires_backends
  from ...utils.backbone_utils import BackboneMixin
  from .configuration_timm_backbone import TimmBackboneConfig
label_desc: Use image node_id 1 to create a new TimmBackbone object from inherited base classes: PreTrainedModel, BackboneMixin with example: obj = TimmBackbone(config)