๐Ÿ’ก ํ•œ ์ค„ ์š”์•ฝ#

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)
  • ์ฃผ์š” ๊ธฐ์—ฌ์ :
    1. ์ด๋ฏธ์ง€์˜ 1D ํŒจ์น˜ ์‹œํ€€์Šค ๋ณ€ํ™˜ (Patch Embedding): $H \times W \times C$ ์ด๋ฏธ์ง€๋ฅผ $P \times P$ 2D ํŒจ์น˜๋กœ ๋ถ„ํ•  ํ›„ Linear Projection์„ ๊ฑฐ์ณ 1D ํ† ํฐ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜.
    2. [CLS] ํ† ํฐ & 1D Positional Embedding: BERT์˜ [CLS] ํ† ํฐ ๊ฐœ๋…์„ ์ ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ์ „์—ญ ํ‘œํ˜„์„ ์ง‘๊ณ„ํ•˜๊ณ , ํ•™์Šต ๊ฐ€๋Šฅํ•œ 1D ์œ„์น˜ ์ž„๋ฒ ๋”ฉ ์ฃผ์ž….
    3. Pre-LayerNorm Transformer Encoder: MSA ๋ฐ MLP ๋ ˆ์ด์–ด ์ „๋‹จ์— Layer Normalization์„ ๋ฐฐ์น˜ํ•˜์—ฌ ๋Œ€๊ทœ๋ชจ ๋„คํŠธ์›Œํฌ ํ›ˆ๋ จ ์ˆ˜๋ ด์„ฑ ํ™•๋ณด.
    4. ๊ณ ํ•ด์ƒ๋„ ํŒŒ์ธํŠœ๋‹ ์‹œ 2D ๋ณด๊ฐ„ (Bilinear Interpolation): ์‚ฌ์ „ํ•™์Šต ๋Œ€๋น„ ํ•ด์ƒ๋„๊ฐ€ ์ฆ๊ฐ€ํ•  ๋•Œ 1D ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ 2D Grid๋กœ ์žฌ๋ฐฐ์น˜ํ•˜๊ณ  ์Œ์„ ํ˜• ๋ณด๊ฐ„ ์ ์šฉ.

๊ด€๋ จ ์—ฐ๊ตฌ ํ๋ฆ„#

  1. CNN (ResNet, EfficientNet): ์ธ์ ‘ ํ”ฝ์…€์ด ๊ด€๋ จ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ท€๋‚ฉ์  ํŽธํ–ฅ(Locality/Translation Equivariance)์„ ๊ตญ์†Œ ์ปค๋„์— ํ•˜๋“œ์ฝ”๋”ฉํ•˜์—ฌ ์†Œ๊ทœ๋ชจ ๋ฐ์ดํ„ฐ ํ•™์Šต์— ๊ฐ•ํ•˜๋‚˜ ๋ฐ์ดํ„ฐ ๊ทœ๋ชจ ํ™•์žฅ์˜ ํ•œ๊ณ„ ์กด์žฌ.
  2. 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-300M87.54%9.9k TPU-days
Noisy Student (EffNet)JFT-300M88.50%12.3k TPU-days
ViT-L/16 (Ours)JFT-300M87.76%0.68k TPU-days
ViT-H/14 (Ours)JFT-300M88.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