Transformers documentation

骨干

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

骨干

Backbone(骨干网络)是一种用于更高级计算机视觉任务(如目标检测和图像分类)的特征提取模型。Transformers 提供了一个 AutoBackbone 类,用于从预训练模型权重初始化 Transformers 骨干网络,以及两个实用类:

  • BackboneMixin 支持从 Transformers 或 timm 初始化 backbone,并包含用于返回输出特征和索引的函数。
  • BackboneConfigMixin 用于设置骨干网络配置的输出特征和索引。

timm 模型通过 TimmBackbone 和 TimmBackboneConfig 类进行加载。

以下模型支持骨干网络:

AutoBackbone

class transformers.AutoBackbone

< >

( *args**kwargs )

BackboneMixin

class transformers.BackboneMixin

< >

( *args**kwargs )

post_init

< >

( )

重写 post_init 以始终安装捕获钩子,因为 backbone 总是会捕获输出。我们需要在 post_init 中执行此操作,因为模块必须已经实例化。 这样可以避免与 torch.compile 产生一些混淆,因为首次安装钩子时需要/创建一个图断点,这可能会与外部用户调用(例如 model = torch.compile(model...))发生冲突。

BackboneConfigMixin

class transformers.BackboneConfigMixin

< >

( )

一个 Mixin,用于支持处理骨干网络配置中的 out_features 和 out_indices 属性。

set_output_features_output_indices

< >

( out_features: list | Noneout_indices: list | None )

参数

  • out_features (list[str],可选) — 主干网络要输出的特征名称。如果未提供,则默认为 config._out_features。
  • out_indices (list[int] 或 tuple[int],可选) — 骨干网络需要输出的特征索引。如果未提供,则默认为 config._out_indices。

将输出索引和特征设置为新值,并与给定的 stage_names 对齐。 如果未提供其中一个输入,则根据给定的 stage_names 查找对应的 out_features 或 out_indices。

to_dict

< >

( )

将此实例序列化为 Python 字典。重写 PreTrainedConfig 中默认的 to_dict(),以包含 out_features 和 out_indices 属性。

verify_out_features_out_indices

< >

( )

验证 out_indices 和 out_features 对于给定的 stage_names 是否有效。

TimmBackbone

class transformers.TimmBackbone

< >

( config: TimmBackboneConfig**kwargs )

用于作为骨干网络的 timm 模型的包装类。这使得 timm 模型可以与库中的其他模型互换使用,同时保持相同的 API。

TimmBackboneConfig

类 transformers.TimmBackboneConfig

< >

( transformers_version: str | None = Nonearchitectures: list[str] | None = Noneoutput_hidden_states: bool | None = Falsereturn_dict: bool | None = Truedtype: str | torch.dtype | None = Nonechunk_size_feed_forward: int = 0is_encoder_decoder: bool = Falseid2label: dict[int, str] | dict[str, str] | None = Nonelabel2id: dict[str, int] | dict[str, str] | None = Noneproblem_type: Literal['regression', 'single_label_classification', 'multi_label_classification'] | None = Nonebackbone: str | None = Nonenum_channels: int = 3features_only: bool = True_out_indices: list[int] | None = Nonefreeze_batch_norm_2d: bool = Falseoutput_stride: int | None = None )

参数

  • backbone (str,可选) — 要加载的 timm 检查点。
  • num_channels (int,可选,默认为 3) — 输入通道的数量。
  • features_only (bool,可选,默认为 True) — 是否仅输出特征,还是同时输出 logits。
  • freeze_batch_norm_2d (bool,可选,默认为 False) — 将所提供模块中的所有 BatchNorm2d 和 SyncBatchNorm 层转换为 FrozenBatchNorm2d。
  • output_stride (int, 可选) — 输入与输出特征图的空间分辨率之比。

这是用于存储 TimmBackbone 配置的类。它根据指定的参数实例化 Timm Backbone 模型,并定义模型架构。使用默认值实例化配置将生成与 类似的配置。

配置对象继承自 PreTrainedConfig,可用于控制模型输出。有关更多信息,请参阅 PreTrainedConfig 的文档。

示例:

>>> 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
Update on GitHub