第11章注意機構とTransformer
11.8. Transformers for Vision11.8 視覚のためのTransformer
The Transformer architecture was initially proposed for sequence-to-sequence learning, with a focus on machine translation. Subsequently, Transformers emerged as the model of choice in various natural language processing tasks (Brown et al., 2020, Devlin et al., 2018, Radford et al., 2018, Radford et al., 2019, Raffel et al., 2020). However, in the field of computer vision the dominant architecture has remained the CNN (Section 8). Naturally, researchers started to wonder if it might be possible to do better by adapting Transformer models to image data. This question sparked immense interest in the computer vision community. Recently, Ramachandran et al. (2019) proposed a scheme for replacing convolution with self-attention. However, its use of specialized patterns in attention makes it hard to scale up models on hardware accelerators. Then, Cordonnier et al. (2020) theoretically proved that self-attention can learn to behave similarly to convolution. Empirically, patches were taken from images as inputs, but the small patch size makes the model only applicable to image data with low resolutions.
英語のヒント
Transformerの構造は、当初、機械翻訳に焦点を当てた系列から系列への学習のために提案された。その後、Transformerはさまざまな自然言語処理の課題で選ばれるモデルとして台頭した(Brown et al., 2020、Devlin et al., 2018、Radford et al., 2018、Radford et al., 2019、Raffel et al., 2020)。しかし、コンピュータビジョンの分野では、支配的な構造はCNN(第8章)のままだった。当然、研究者たちは、Transformerのモデルを画像データに合わせれば、もっとよくできるのではないかと考え始めた。この問いは、コンピュータビジョンのコミュニティで大きな関心を呼んだ。最近、Ramachandran et al. (2019)は、畳み込みを自己注意に置き換える仕組みを提案した。しかし、注意に特別なパターンを使うので、ハードウェアの加速器の上でモデルの規模を大きくするのが難しい。次に、Cordonnier et al. (2020)は、自己注意が畳み込みと似たようにふるまうことを学習できることを理論的に証明した。実験的には、画像からのパッチを入力としてとったが、パッチの大きさが小さいので、モデルは解像度の低い画像データにしか当てはめられない。
Without specific constraints on patch size, vision Transformers (ViTs) extract patches from images and feed them into a Transformer encoder to obtain a global representation, which will finally be transformed for classification (Dosovitskiy et al., 2021). Notably, Transformers show better scalability than CNNs: and when training larger models on larger datasets, vision Transformers outperform ResNets by a significant margin. Similar to the landscape of network architecture design in natural language processing, Transformers have also become a game-changer in computer vision.
英語のヒント
パッチの大きさに特別な制約を置かずに、視覚のTransformer(ViT)は画像からパッチを取り出し、Transformerのエンコーダに入れて大域的な表現を得る。それは最終的に分類のために変換される(Dosovitskiy et al., 2021)。特に、TransformerはCNNよりスケーラビリティがよい。もっと大きなデータセットでもっと大きなモデルを訓練すると、視覚のTransformerはResNetを大差で上回る。自然言語処理のネットワーク構造の設計の状況と同じように、Transformerはコンピュータビジョンでも局面を一変させるものになった。
import torch
from torch import nn
from d2l import torch as d2l