๐ก ํ ์ค ์์ฝ#
CNN ๊ณ ์ ์ ์ง์ญ์ฑ(Locality) ๊ท๋ฉ์ ํธํฅ(Inductive Bias) ์์ด, $P \times P$ ํจ์น ๋จ์๋ก ๋ถํ ๋ ์ด๋ฏธ์ง๋ฅผ ํ ํฐ์ผ๋ก ์ง๋ ฌํํ๊ณ ์์ Transformer Encoder๋ฅผ ๋๊ท๋ชจ ๋ฐ์ดํฐ์ (JFT-300M)์ ์ฌ์ ํ์ตํ์ฌ CNN SOTA(ResNet/BiT)๋ฅผ 4๋ฐฐ ๋ฎ์ ์ฐ์ฐ ๋น์ฉ์ผ๋ก ์๋ํ๋ค.
๐ ๊ฐ์ ๋ฐ ํต์ฌ ๊ธฐ์ฌ (Key Contributions)#
- ์ ์: Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby (Google Brain)
- ๋ฐํ๋ ๋: 2021 (arXiv:2010.11929, ICLR 2021)
- ์ฃผ์ ๊ธฐ์ฌ์ :
- ์ด๋ฏธ์ง์ 1D ํจ์น ์ํ์ค ๋ณํ (Patch Embedding): $H \times W \times C$ ์ด๋ฏธ์ง๋ฅผ $P \times P$ 2D ํจ์น๋ก ๋ถํ ํ Linear Projection์ ๊ฑฐ์ณ 1D ํ ํฐ ์ํ์ค๋ก ๋ณํ.
- [CLS] ํ ํฐ & 1D Positional Embedding: BERT์
[CLS]ํ ํฐ ๊ฐ๋ ์ ์ ์ฉํ์ฌ ์ด๋ฏธ์ง ์ ์ญ ํํ์ ์ง๊ณํ๊ณ , ํ์ต ๊ฐ๋ฅํ 1D ์์น ์๋ฒ ๋ฉ ์ฃผ์ . - Pre-LayerNorm Transformer Encoder: MSA ๋ฐ MLP ๋ ์ด์ด ์ ๋จ์ Layer Normalization์ ๋ฐฐ์นํ์ฌ ๋๊ท๋ชจ ๋คํธ์ํฌ ํ๋ จ ์๋ ด์ฑ ํ๋ณด.
- ๊ณ ํด์๋ ํ์ธํ๋ ์ 2D ๋ณด๊ฐ (Bilinear Interpolation): ์ฌ์ ํ์ต ๋๋น ํด์๋๊ฐ ์ฆ๊ฐํ ๋ 1D ์์น ์๋ฒ ๋ฉ์ 2D Grid๋ก ์ฌ๋ฐฐ์นํ๊ณ ์์ ํ ๋ณด๊ฐ ์ ์ฉ.
๐ฏ ๊ด๋ จ ์ฐ๊ตฌ ํ๋ฆ ๋ฐ ๊ธฐ์กด ํ๊ณ (Related Work & Motivation)#
๊ด๋ จ ์ฐ๊ตฌ ํ๋ฆ#
- CNN (ResNet, EfficientNet): ์ธ์ ํฝ์ ์ด ๊ด๋ จ๋์ด ์๋ค๋ ๊ท๋ฉ์ ํธํฅ(Locality/Translation Equivariance)์ ๊ตญ์ ์ปค๋์ ํ๋์ฝ๋ฉํ์ฌ ์๊ท๋ชจ ๋ฐ์ดํฐ ํ์ต์ ๊ฐํ๋ ๋ฐ์ดํฐ ๊ท๋ชจ ํ์ฅ์ ํ๊ณ ์กด์ฌ.
- Vision Transformer (ViT): ๊ท๋ฉ์ ํธํฅ์ ๋ฐฐ์ ํ๊ณ ๋๊ท๋ชจ ๋ฐ์ดํฐ(JFT-300M) ์ฌ์ ํ์ต์ ํตํด ์ ์ญ ํจ์น ๊ฐ ๊ด๋ฒ์ Self-Attention ํจํด์ ์ค์ค๋ก ํ์ต.
๐ ๋ชฉ์ฐจ#
- Chapter 1: ํจ์น ์๋ฒ ๋ฉ (Patch Embedding) & [CLS] ํ ํฐ ์์
- Chapter 2: Pre-LayerNorm ViT Encoder ๋ธ๋ก ์์
- Chapter 3: Multi-Head Self-Attention (MSA) ์์
- Chapter 4: 2D ์์น ์๋ฒ ๋ฉ ๋ณด๊ฐ (Bilinear Interpolation)
- Chapter 5: ์ฃผ์ ์คํ ๋ฐ ๊ฒฐ๊ณผ
- Chapter 6: ๊ฒฐ๋ก ๋ฐ ์์ฌ์
๐ ๏ธ Chapter 1: ํจ์น ์๋ฒ ๋ฉ & [CLS] ํ ํฐ ์์#
1. ์์ฝ#
์ด๋ฏธ์ง $\mathbf{x} \in \mathbb{R}^{H \times W \times C}$๋ฅผ $N = \frac{HW}{P^2}$๊ฐ ํจ์น $\mathbf{x}_p^i$๋ก ๋๋์ด Linear Projection Matrix $\mathbf{E}$์ ๊ณฑํ ํ [CLS] ํ ํฐ $\mathbf{x}_{\text{class}}$ ๋ฐ ์์น ์๋ฒ ๋ฉ $\mathbf{E}_{\text{pos}}$๋ฅผ ๊ฒฐํฉํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$\mathbf{z}_0 = [\mathbf{x}_{\text{class}} \;;\; \mathbf{x}_p^1\mathbf{E} \;;\; \mathbf{x}_p^2\mathbf{E} \;;\; \dots \;;\; \mathbf{x}_p^N\mathbf{E}] + \mathbf{E}_{\text{pos}}$$$$\mathbf{E} \in \mathbb{R}^{(P^2 \cdot C) \times D}, \quad \mathbf{E}_{\text{pos}} \in \mathbb{R}^{(N+1) \times D}$$import torch
import torch.nn as nn
class PatchEmbedding(nn.Module):
"""
ViT 2D ์ด๋ฏธ์ง -> 1D Patch Embedding ํ ํฐ ๋ณํ ๋ชจ๋ (Conv2d 1x1 stride=P ๊ตฌํ)
"""
def __init__(self, in_channels: int = 3, patch_size: int = 16, embed_dim: int = 768, img_size: int = 224):
super().__init__()
self.patch_size = patch_size
self.num_patches = (img_size // patch_size) ** 2
# Conv2d๋ฅผ ์ด์ฉํ ๊ณ ์ Linear Patch Projection
self.proj = nn.Conv2d(in_channels, embed_dim, kernel_size=patch_size, stride=patch_size)
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
self.pos_embed = nn.Parameter(torch.zeros(1, self.num_patches + 1, embed_dim))
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
x: (B, C, H, W)
"""
B = x.shape[0]
patches = self.proj(x).flatten(2).transpose(1, 2) # (B, N_patches, D)
cls_tokens = self.cls_token.expand(B, -1, -1) # (B, 1, D)
z0 = torch.cat([cls_tokens, patches], dim=1) # (B, N+1, D)
z0 = z0 + self.pos_embed
return z0
# --- ์ฌ์ฉ ์์ ---
patch_embed = PatchEmbedding(patch_size=16, embed_dim=768, img_size=224)
img = torch.randn(2, 3, 224, 224)
print("ViT z0 ์
๋ ฅ ํ ํฐ ์ํ์ค Shape:", patch_embed(img).shape)๐ ๏ธ Chapter 2: Pre-LayerNorm ViT Encoder ๋ธ๋ก ์์#
1. ์์ฝ#
๊ฐ ์๋ธ๋ ์ด์ด ์ ๋จ์ Layer Normalization์ ์ ์ฉ(Pre-LN)ํ๊ณ ์์ฐจ ์ฐ๊ฒฐ(Residual Connection)์ ์ํํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$\mathbf{z}'_\ell = \text{MSA}(\text{LN}(\mathbf{z}_{\ell-1})) + \mathbf{z}_{\ell-1}, \quad \ell = 1 \dots L$$$$\mathbf{z}_\ell = \text{MLP}(\text{LN}(\mathbf{z}'_\ell)) + \mathbf{z}'_\ell, \quad \ell = 1 \dots L$$$$\mathbf{y} = \text{LN}(\mathbf{z}_L^0)$$import torch
import torch.nn as nn
class ViTEncoderBlock(nn.Module):
"""
Pre-LayerNorm ViT Encoder Block (MSA + MLP)
"""
def __init__(self, embed_dim: int = 768, num_heads: int = 12, mlp_ratio: float = 4.0):
super().__init__()
self.ln1 = nn.LayerNorm(embed_dim)
self.attn = nn.MultiheadAttention(embed_dim, num_heads, batch_first=True)
self.ln2 = nn.LayerNorm(embed_dim)
mlp_hidden_dim = int(embed_dim * mlp_ratio)
self.mlp = nn.Sequential(
nn.Linear(embed_dim, mlp_hidden_dim),
nn.GELU(),
nn.Linear(mlp_hidden_dim, embed_dim)
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
# 1. MSA with Residual
norm_x1 = self.ln1(x)
attn_out, _ = self.attn(norm_x1, norm_x1, norm_x1)
x = x + attn_out
# 2. MLP with Residual
norm_x2 = self.ln2(x)
x = x + self.mlp(norm_x2)
return x
# --- ์ฌ์ฉ ์์ ---
encoder_block = ViTEncoderBlock(embed_dim=768)
seq_in = torch.randn(2, 197, 768)
print("ViT Encoder Block ์ถ๋ ฅ Shape:", encoder_block(seq_in).shape)๐ ๏ธ Chapter 3: Multi-Head Self-Attention (MSA) ์์#
1. ์์ฝ#
์ํ์ค $\mathbf{z}$์์ ์ฌ์๋ $[\mathbf{q}, \mathbf{k}, \mathbf{v}]$ ๋ฒกํฐ ๊ฐ ์ ์ญ ์ดํ ์ ํ๋ ฌ $A \in \mathbb{R}^{(N+1) \times (N+1)}$์ ์์ฑํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$[\mathbf{q}, \mathbf{k}, \mathbf{v}] = \mathbf{z} \mathbf{U}_{qkv}, \quad \mathbf{U}_{qkv} \in \mathbb{R}^{D \times 3D}$$$$A = \text{Softmax}\left( \frac{\mathbf{q} \mathbf{k}^T}{\sqrt{D_h}} \right), \quad \text{SA}(\mathbf{z}) = A \mathbf{v}$$import torch
import torch.nn.functional as F
def compute_patch_self_attention_matrix(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor) -> torch.Tensor:
"""
ViT Single-Head Self-Attention ์ฐ์ฐ ๊ตฌํ
q, k, v: (B, N+1, D_h)
"""
d_h = q.size(-1)
attn_matrix = F.softmax(torch.matmul(q, k.transpose(-2, -1)) / (d_h ** 0.5), dim=-1)
output = torch.matmul(attn_matrix, v)
return output, attn_matrix
# --- ์ฌ์ฉ ์์ ---
q_p = torch.randn(2, 197, 64)
k_p = torch.randn(2, 197, 64)
v_p = torch.randn(2, 197, 64)
out_sa, A_mat = compute_patch_self_attention_matrix(q_p, k_p, v_p)
print("Self-Attention ์ถ๋ ฅ Shape:", out_sa.shape, "์ดํ
์
๋งต Shape:", A_mat.shape)๐ ๏ธ Chapter 4: 2D ์์น ์๋ฒ ๋ฉ ๋ณด๊ฐ (Bilinear Interpolation)#
1. ์์ฝ#
์ฌ์ ํ์ต ํด์๋ $224 \times 224$ ($14 \times 14$ Grid)์์ ํ์ธํ๋ ํด์๋ $384 \times 384$ ($24 \times 24$ Grid)๋ก ํ๋๋ ๋ 1D ์์น ์๋ฒ ๋ฉ์ 2D Grid๋ก ๋ณ๊ฒฝํ ํ Bilinear Interpolation ๋ณด๊ฐํฉ๋๋ค.
2. ์์ ๋ฐ ํ์ด์ฌ ์ฝ๋ ์ค๋ช #
$$\mathbf{E}_{\text{pos\_new}} = \text{BilinearInterpolate2D}\left( \mathbf{E}_{\text{pos\_orig\_2D}}, \ (H_{\text{new}}/P, W_{\text{new}}/P) \right)$$import torch
import torch.nn.functional as F
def interpolate_pos_embed_2d(
pos_embed_orig: torch.Tensor, # (1, N_orig+1, D)
orig_grid_size: int = 14,
new_grid_size: int = 24
) -> torch.Tensor:
"""
ViT ํ์ธํ๋ ๊ณ ํด์๋ ์ ์ฉ ์ Positional Embedding 2D Bilinear Interpolation
"""
D = pos_embed_orig.shape[-1]
cls_token_pos = pos_embed_orig[:, :1, :]
patch_pos = pos_embed_orig[:, 1:, :] # (1, 196, D)
# 2D Grid (1, D, 14, 14) ๋ณํ
patch_pos_2d = patch_pos.reshape(1, orig_grid_size, orig_grid_size, D).permute(0, 3, 1, 2)
# 2D Bilinear Interpolation -> (1, D, 24, 24)
new_patch_pos_2d = F.interpolate(patch_pos_2d, size=(new_grid_size, new_grid_size), mode='bilinear', align_corners=False)
# 1D ํ ํฐ ๋ณต์ (1, 576, D)
new_patch_pos = new_patch_pos_2d.permute(0, 2, 3, 1).reshape(1, new_grid_size * new_grid_size, D)
# [CLS] ์์น ์๋ฒ ๋ฉ ํฉ์ฒด
return torch.cat([cls_token_pos, new_patch_pos], dim=1) # (1, 577, D)
# --- ์ฌ์ฉ ์์ ---
pos_orig = torch.randn(1, 197, 768) # 14x14 grid
pos_new = interpolate_pos_embed_2d(pos_orig, orig_grid_size=14, new_grid_size=24)
print("๋ณด๊ฐ๋ ๊ณ ํด์๋ Positional Embedding Shape:", pos_new.shape)๐ ์ฃผ์ ์คํ ๋ฐ ๊ฒฐ๊ณผ (Experiments & Results)#
1. ImageNet-1K ํ-1 ๋ถ๋ฅ ์ ํ๋ ๋ฐ ํ์ต ์ฐ์ฐ ๋น์ฉ ๋น๊ต#
| ์๊ณ ๋ฆฌ์ฆ (Method) | ์ฌ์ ํ์ต ๋ฐ์ดํฐ์ | ImageNet ํ-1 Accuracy โ | ํ์ต ์ฐ์ฐ ๋น์ฉ (TPUv3-days) โ |
|---|---|---|---|
| ResNet-152x4 (BiT-L) | JFT-300M | 87.54% | 9.9k TPU-days |
| Noisy Student (EffNet) | JFT-300M | 88.50% | 12.3k TPU-days |
| ViT-L/16 (Ours) | JFT-300M | 87.76% | 0.68k TPU-days |
| ViT-H/14 (Ours) | JFT-300M | 88.55% (SOTA) | 2.5k TPU-days (4๋ฐฐ ์ ๊ฐ) |
- ๊ฒฐ๊ณผ: ๋๊ท๋ชจ JFT-300M ์ฌ์ ํ์ต ์ CNN ๊ธฐ๋ฐ SOTA๋ฅผ ์์ ํ ๋์ด์๋ฉฐ ํ์ต ์ปดํจํ ์ฐ์ฐ๋์ 4๋ฐฐ ๊ฐ๋ ์ ๊ฐ.
๐ก ๊ฒฐ๋ก ๋ฐ ์์ฌ์ (Conclusion & Insights)#
1. ๊ฒฐ๋ก #
ViT๋ “An Image is Worth 16x16 Words"๋ผ๋ ํ์ ์๋, ์ปดํจํฐ ๋น์ ๋ฐฑ๋ณธ์ CNN์์ Vision Transformer ์๋๋ก ์์ ํ ์ ํ์ํจ ํ๋ช ์ ๋ ผ๋ฌธ์ ๋๋ค.
2. ํ๊ณ์ ๋ฐ ์์ฌ์ด ์ #
- ์๊ท๋ชจ ๋ฐ์ดํฐ์์ CNN ๋๋น ์ด์ธ โ DeiT(๋ฐ์ดํฐ ํจ์จ ๊ฐ์ ), MAE(์๊ธฐ์ง๋ ์ฌ์ ํ์ต)๋ก ํด๊ฒฐ๋จ.
- ๊ณ ํด์๋ ์ด๋ฏธ์ง์์ $O(N^2)$ ๊ณ์ฐ ๋น์ฉ โ Swin Transformer(์๋์ฐ Attention)๋ก ํด๊ฒฐ๋จ.
- ํ์งยท๋ถํ ๋ฑ ๋ฐ์ง ์์ธก ํ์คํฌ ๋ฏธ์ง์ โ ViTDet, DINOv2 ๋ฑ์ผ๋ก ํ์ฅ๋จ.
- JFT-300M๊ณผ ๊ฐ์ ๋น๊ณต๊ฐ ์ด๋๊ท๋ชจ ๋ฐ์ดํฐ์ ์ ์์กดํ๋ค๋ ์ ์ ์ฌํ์ฑยท์ ๊ทผ์ฑ ์ธก๋ฉด์์ ์์ฌ์ด ๋ถ๋ถ.
์ฐธ๊ณ ์๋ฃ#
๊ด๋ จ ๋ ผ๋ฌธ: Attention Is All You Need, ResNet, DETR, BEVFormer