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年3月30日にHF papersで公開され、2023年8月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) — 各パッチのサイズ(解像度)です。
  • num_channels (int、任意、デフォルトは3) — 入力チャネル数。
  • qkv_bias (bool、省略可、デフォルトは True) — クエリ、キー、バリューにバイアスを追加するかどうか。
  • drop_path_rate (Union[float, int]、任意、デフォルトは 0.0) — パッチ融合におけるドロップパスの率。
  • 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

クラス transformers.VitDetModel

< >

( config: VitDetConfig )

パラメータ

  • config (VitDetConfig) — モデルのすべてのパラメータを含むモデル設定クラスです。設定ファイルで初期化しても、モデルに関連付けられた重みは読み込まれず、設定のみが読み込まれます。モデルの重みをロードするには、from_pretrained() メソッドを参照してください。

特定のヘッドを付加せず、生のhidden-statesを出力する素のVitdet Model。

このモデルは 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.FloatTensor)

パラメータ

  • pixel_values (torch.Tensor、形状 (batch_size, num_channels, image_size, image_size)、任意) — 入力画像に対応するテンソルです。ピクセル値は image_processor_class を使用して取得できます。詳細は image_processor_class.__call__ を参照してください(processor_class は画像の処理に image_processor_class を使用します)。
  • output_attentions (bool, 省略可) — すべてのアテンション層のアテンションテンソルを返すかどうかを指定します。詳細は、返されるテンソルの attentions を参照してください。
  • output_hidden_states (bool, 省略可) — すべての層の hidden states を返すかどうかを指定します。詳細は、返されるテンソルの hidden_states を参照してください。
  • return_dict (bool, 任意) — プレーンなタプルではなく ModelOutput を返すかどうか。

戻り値

BaseModelOutput または tuple(torch.FloatTensor)

torch.FloatTensor のタプル、または BaseModelOutput(return_dict=False が渡された場合、または config.return_dict=False の場合)。設定(VitDetConfig)や入力に応じて、さまざまな要素で構成されます。

VitDetModelのforwardメソッドは、__call__特殊メソッドをオーバーライドします。

この関数内でフォワードパスの手順を定義する必要がありますが、その後、この関数ではなくModuleインスタンスを呼び出すべきです。前者は前処理と後処理の実行を適切に処理するのに対し、後者はそれらを黙って無視するためです。

  • last_hidden_state (torch.FloatTensor、shape (batch_size, sequence_length, hidden_size)) — モデルの最終層の出力における隠れ状態のシーケンス。

  • hidden_states (tuple(torch.FloatTensor)、optional、output_hidden_states=True が渡された場合または config.output_hidden_states=True の場合に返されます)— torch.FloatTensor のタプル(埋め込み層を持つモデルの場合、埋め込みの出力が 1 つ、各層の出力がそれぞれ 1 つずつ)で、形状は (batch_size, sequence_length, hidden_size) です。

    各層の出力におけるモデルのHidden-states、および任意の初期埋め込み出力。

  • attentions (tuple(torch.FloatTensor)、optional、output_attentions=True が渡された場合または config.output_attentions=True の場合に返されます)— torch.FloatTensor のタプル(各レイヤーごとに1つ)、形状は (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