Transformers documentation

ViTDet

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v5.22.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

该模型于 2022-03-30 在 HF papers 上发布,并于 2023-08-29 贡献至 Hugging Face Transformers。

ViTDet

概述

ViTDet 模型由 Yanghao Li、Hanzi Mao、Ross Girshick 和 Kaiming He 在 Exploring Plain Vision Transformer Backbones for Object Detection 中提出。 VitDet 利用纯 Vision Transformer 来执行目标检测任务。

该论文的摘要如下:

我们探索了朴素的、非层级化的 Vision Transformer(ViT)作为目标检测的骨干网络。这种设计使得原始的 ViT 架构无需重新设计用于预训练的非层级化骨干网络,即可直接微调用于目标检测。通过极少的微调适配,我们的朴素骨干检测器能够取得具有竞争力的结果。令人惊讶的是,我们观察到:(i) 仅从单尺度特征图构建简单的特征金字塔(无需常见的 FPN 设计)就足够了;(ii) 使用窗口注意力(无需移位)并辅以极少量的跨窗口传播块就足够了。使用以 Masked Autoencoders(MAE)方式预训练的朴素 ViT 骨干,我们名为 ViTDet 的检测器能够与以往所有基于层级化骨干的领先方法相媲美,仅使用 ImageNet-1K 预训练,在 COCO 数据集上即可达到 61.3 AP_box。我们希望本研究能引起对朴素骨干检测器研究的关注。

该模型由 nielsr 贡献。 原始代码可在此处找到。

提示:

  • 目前,仅提供了骨干网络。

VitDetConfig

类 transformers.VitDetConfig

< >

( 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 = Nonehidden_size: int = 768num_hidden_layers: int = 12num_attention_heads: int = 12mlp_ratio: int = 4hidden_act: str = 'gelu'dropout_prob: float | int = 0.0initializer_range: float = 0.02layer_norm_eps: float = 1e-06image_size: int | list[int] | tuple[int, int] = 224pretrain_image_size: int | list[int] | tuple[int, int] = 224patch_size: int | list[int] | tuple[int, int] = 16num_channels: int = 3qkv_bias: bool = Truedrop_path_rate: float | int = 0.0window_block_indices: list[int] | tuple[int, ...] = ()residual_block_indices: list[int] | tuple[int, ...] = ()use_absolute_position_embeddings: bool = Trueuse_relative_position_embeddings: bool = Falsewindow_size: int = 0_out_features: list[str] | None = None_out_indices: list[int] | None = None )

参数

  • hidden_size (int,可选,默认为 768) — 隐藏表示的维度。
  • num_hidden_layers (int, 可选, 默认为 12) — Transformer 解码器中的隐藏层数量。
  • num_attention_heads (int,可选,默认为 12) — Transformer 解码器中每个注意力层的注意力头数量。
  • mlp_ratio (int,可选,默认为 4) — MLP 隐藏层维度与嵌入维度的比值。
  • hidden_act (str,可选,默认为 gelu) — 解码器中的非线性激活函数(函数或字符串)。例如,"gelu"、 "relu"、"silu" 等。
  • dropout_prob (Union[float, int],可选,默认为 0.0) — 所有 dropout 层的比率。
  • initializer_range (float,可选,默认为 0.02) — 用于初始化所有权重矩阵的 truncated_normal_initializer 的标准差。
  • layer_norm_eps (float,可选,默认为 1e-06) — 层归一化层所使用的 epsilon 值。
  • image_size (Union[int, list[int], tuple[int, int]],可选,默认为 224) — 每张图片的尺寸(分辨率)。
  • pretrain_image_size (int,可选,默认为 224) — 预训练期间每张图片的尺寸(分辨率)。
  • patch_size (Union[int, list[int], tuple[int, int]],可选,默认为 16) — 每个 patch 的大小(分辨率)。
  • num_channels (int,可选,默认为 3) — 输入通道的数量。
  • qkv_bias (bool,可选,默认为 True) — 是否向查询、键和值添加偏置。
  • drop_path_rate (Union[float, int],可选,默认为 0.0) — 用于 patch fusion 的 drop path 比率。
  • window_block_indices (list[int],可选,默认为 []) — 应使用窗口注意力而非常规全局自注意力的块索引列表。
  • residual_block_indices (list[int],可选,默认为 []) — 需要在 MLP 之后添加额外残差块的块的索引列表。
  • use_absolute_position_embeddings (bool,可选,默认为 True) — 是否使用绝对位置嵌入。
  • use_relative_position_embeddings (bool,可选,默认为 False) — 是否向注意力图添加相对位置嵌入。
  • window_size (int,可选,默认为 0) — 注意力窗口的大小。

这是用于存储 VitDetModel 配置的类。它根据指定的参数实例化 Vitdet 模型,并定义模型架构。使用默认值实例化配置将生成与 google/vitdet-base-patch16-224 类似的配置。

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

示例:

>>> from transformers import VitDetConfig, VitDetModel

>>> # Initializing a VitDet configuration
>>> configuration = VitDetConfig()

>>> # Initializing a model (with random weights) from the configuration
>>> model = VitDetModel(configuration)

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

VitDetModel

class transformers.VitDetModel

< >

( config: VitDetConfig )

参数

  • config (VitDetConfig) — 模型的配置类,包含模型的所有参数。使用配置文件进行初始化时,不会加载与模型关联的权重,仅加载配置。请查看 from_pretrained() 方法以加载模型权重。

未添加任何特定头部的裸 Vitdet 模型,直接输出原始隐藏状态。

该模型继承自 PreTrainedModel。请查阅父类文档,了解该库为所有模型实现的通用方法(例如下载或保存、调整输入嵌入大小、剪枝头等)。

该模型也是 PyTorch torch.nn.Module 的子类。 请将其作为常规的 PyTorch Module 使用,有关通用用法和行为的所有事项,请参阅 PyTorch 文档。

forward

< >

( pixel_values: typing.Optional[torch.Tensor] = Noneoutput_attentions: bool | None = Noneoutput_hidden_states: bool | None = Nonereturn_dict: bool | None = None**kwargs ) → BaseModelOutput or tuple(torch.Tensor)

参数

  • pixel_values(形状为 (batch_size, num_channels, image_size, image_size) 的 torch.Tensor,可选) — 与输入图像对应的张量。可以使用 image_processor_class 获取像素值。有关详细信息,请参阅 image_processor_class.__call__(processor_class 使用 image_processor_class 来处理图像)。
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关详细信息,请参阅返回张量中的 attentions。
  • output_hidden_states (bool,可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参阅返回张量中的 hidden_states。
  • return_dict (bool, 可选) — 是否返回 ModelOutput 而不是普通元组。

返回值

BaseModelOutput 或 tuple(torch.Tensor)

一个 BaseModelOutput 或 torch.Tensor 元组(如果传入了 return_dict=False 或 config.return_dict=False),其包含的元素取决于配置(VitDetConfig)和输入。

VitDetModel 的 forward 方法重写了 __call__ 特殊方法。

尽管前向传播的实现需要在此函数中定义,但之后应调用 Module 实例,而不是直接调用此函数,因为前者会负责执行预处理和后处理步骤,而后者则会静默地忽略这些步骤。

  • last_hidden_state(形状为 (batch_size, sequence_length, hidden_size) 的 torch.Tensor)— 模型最后一层输出的隐藏状态序列。

  • hidden_states (tuple(torch.Tensor),可选,当传入 output_hidden_states=True 或 config.output_hidden_states=True 时返回) — 一个 torch.Tensor 元组(如果模型具有嵌入层,则包含嵌入层的输出,以及每一层的输出),形状为 (batch_size, sequence_length, hidden_size)。

    模型在每一层输出处的隐藏状态,以及可选的初始嵌入输出。

  • attentions (tuple(torch.Tensor),可选,当传入 output_attentions=True 或 config.output_attentions=True 时返回) — 一个 torch.Tensor 元组(每层一个),形状为 (batch_size, num_heads, sequence_length, sequence_length)。

    注意力权重,即经过注意力 softmax 之后的权重,用于在自注意力头中计算加权平均值。

示例:

>>> from transformers import VitDetConfig, VitDetModel
>>> import torch

>>> config = VitDetConfig()
>>> model = VitDetModel(config)

>>> pixel_values = torch.randn(1, 3, 224, 224)

>>> with torch.no_grad():
...     outputs = model(pixel_values)

>>> last_hidden_states = outputs.last_hidden_state
>>> list(last_hidden_states.shape)
[1, 768, 14, 14]
Update on GitHub