###### tags: `Paper Notes`
# ViT (Vision Transformer)
* 原文:[An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://arxiv.org/abs/2010.11929)
* 機構:Google Research, Brain Team
* 時間:2020 年
### Introduction
* 在征服了 NLP 領域後,transformer 也開始在 CV 領域大殺四方,有意取代 CNN 成為 CV 的下一個標準。
* Vision Transformer (ViT) 便是 transformer 應用在 classification 任務上的一個例子。
### Method
* 如 Figure 1 所示,ViT 的作法很簡單,其架構跟原始的 transformer 基本相同。
* 首先,將 image $x \in R^{H \times W \times C}$ 切分乘很多 patches $x_p \in R^{N \times (P^2 \times C)}$。
* $(H, W)$:resolution of image。$C$:number of channels。$(P, P)$:resolution of each patch。$N = \frac{H \times W}{P^2}$:number of patches, input sequence length for the transformer。
* 隨後,將這些 pacthes 做完 flatten 後丟入 linear projection。
* 將 linear projection 的結果與 position embedding 相加後,再丟入 transformer encoder。
* 與 BERT 中的 [class] token 相同,第 0 個位置是用來做分類的。
* 根據實驗,1D position embeddings 效果比 2D 好。
* 定義 transformer 中向量的維度為 $D$。
> 原文:a sinusoidal structure is sometimes apparent for larger grids (Appendix D). That the position embeddings learn to represent 2D image topology explains why hand-crafted 2D-aware embedding variants do not yield improvements (Appendix D.3).
> 問題:sinusoidal structure 是啥?
* 在 pretraining 時,classification head 使用擁有 1 個 hidden layer 的 MLP。而在 fine tune 時,classification head 則是只有 1 層的 linear layer。
<center><img src="https://i.imgur.com/odRf4Kt.png" width=550></center>
* 名詞定義 (Figure 1 右):
* Norm:Layer Normalization
* MLP:2 fully connected layer + GELU activation
* 在做 fine tune 時,作者用了一個小技巧 [1] [2]:
* 將 image 的尺寸放大。
* patches 的尺寸不變 → input sequence 變長。
> 原文:however, the pre-trained position embeddings may no longer be meaningful. We therefore perform 2D interpolation of the pre-trained position embeddings, according to their location in the original image.
> 問題:看不太懂。
### Experiments & Results
* 如 Table 1 所示,ViT 總共有 3 種型號 — Base、Large、Huge。
* 範例:ViT-L/16 表示使用 Large,且 patches 尺寸為 16x16。
<center><img src="https://i.imgur.com/Okkpo5q.png" width=400></center>
* Image Classification 資料集介紹:
* ImageNet:擁有 1k 個類別與 1.3M 張圖片
* ImageNet-21k:ImageNet 的母集,擁有 21k 個類別與 14M 張圖片
* JFT:擁有 18k 個類別與 303M 張高解析度的圖片
* Table 2 為 ViT 與其他 SOTA CNN-based classifier 的比較。基本就是 ViT 完勝。
* Big Transfer (BiT) [2]:a supervised transfer learning with large ResNets
* Noisy Student [3]:a large EfficientNet trained using semi-supervised learning on ImageNet and JFT-300M with the labels removed
<center><img src="https://i.imgur.com/sR9hbl8.png" width=550></center>
* Figure 3、Figure 4 為 ViT 在不同大小的資料集上做 pretraining 的效果。可以看到當可以看到當資料集比較小時,ViT 的效能是不如 CNN-based 的 BiT 的。然而,當資料集變大時,情況就翻轉了。
<center><img src="https://i.imgur.com/uPV2sLV.png" width=550></center>
* 超參數設置 (引用自原文):
> **Training & Fine-tuning.** We train all models, including ResNets, using Adam with $β_1 = 0.9,\ β_2 = 0.999$, a batch size of $4096$ and apply a high weight decay of $0.1$, which we found to be useful for transfer of all models (Appendix D.1 shows that, in contrast to common practices, Adam works slightly better than SGD for ResNets in our setting). We use a linear learning rate warmup and decay, see Appendix B.1 for details. For fine-tuning we use SGD with momentum, batch size $512$, for all models, see Appendix B.1.1. For ImageNet results in Table 2, we fine-tuned at higher resolution: $512$ for ViT-L/16 and $518$ for ViT-H/14, and also used Polyak & Juditsky (1992) averaging with a factor of $0.9999$ (Ramachandran et al., 2019; Wang et al., 2020b).
### References
[1] Fixing the train-test resolution discrepancy
[2] Big transfer (BiT): General visual representation learning
[3] Self-training with noisy student improves imagenet classification