C.W.K.
Stream
Lesson 06 of 12 · published

Vision Transformer

~18 min · vit, transformer, patches

Level 0Curious
0 XP0/73 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Transformer 가 vision 도 먹음 (2020)

Vision Transformer (ViT) 가 image 를 작은 patch (e.g. 16×16) grid 로 쪼개고, 각 patch 를 vector 로 embed, patch embedding sequence 를 standard transformer encoder 에 먹여. Convolution 없음. 충분한 data 에 pretrain 되면 ViT 가 vision benchmark 에서 CNN 매치하거나 이김.

충격이었어. CNN inductive bias (locality, translation invariance) 가 vision 에 essential 로 다뤄졌어. ViT 가 충분한 data 와 compute 면 attention 이 그 bias 를 baked in 대신 학습할 수 있다는 거 보여줌.

팁: ViT 는 큰 pretraining dataset (ImageNet-21k, JFT-300M) 에서 best work. 작은 dataset (ImageNet-1k from scratch) 에서는 CNN 이 자주 이김 — 그 inductive bias 가 data 부족할 때 유용한 regularizer.

Patches → tokens trick

16×16 patch 의 224×224 image 가 196 patch. 각 patch 가 linear projection 통해 768-d vector 로 flatten (또는, equivalently, kernel=16, stride=16 의 Conv2d). Learned position embedding 과 [CLS] token 추가. 이제 197 token sequence — standard transformer encoder 에 먹여.

Modern vision foundation model

ViT 는 architecture, 사람들이 실제 쓰는 weight 는 pretrained foundation model: CLIP (OpenAI; image-text contrastive), DINOv2 (Meta; self-supervised), SAM (Meta; segmentation), SigLIP (Google; image-text). Vision task 를 'image embed, head train' 으로 바꾸는 downloadable backbone — from scratch network train 하는 것보다 훨씬 쉬움.

원칙: 2026 년 어떤 vision task 든 첫 수: pretrained foundation model (general vision 에 DINOv2, vision-language 에 CLIP, segmentation 에 SAM) 골라, data embed, small head train. Architecture 선택이 backbone 선택보다 훨씬 덜 중요.

Code

ViT in five lines via torchvision·python
import torchvision.models as tvm
from torchvision.models import ViT_B_16_Weights

weights = ViT_B_16_Weights.IMAGENET1K_V1
vit = tvm.vit_b_16(weights=weights)
preprocess = weights.transforms()
# vit takes [B, 3, 224, 224], outputs [B, 1000] logits
Use DINOv2 as a frozen vision backbone·python
import torch

# DINOv2 — strong self-supervised vision backbone from Meta
backbone = torch.hub.load("facebookresearch/dinov2", "dinov2_vitb14")
backbone.eval()
for p in backbone.parameters():
    p.requires_grad = False

# Forward — outputs 768-d global embedding per image
img = preprocess(pil_image).unsqueeze(0)        # [1, 3, 224, 224]
with torch.inference_mode():
    z = backbone(img)                           # [1, 768]

External links

Exercise

DINOv2 와 CLIP image encoder 로 본인 도메인 image 100 개 embed. Pairwise cosine similarity 계산. Nearest neighbour visually 검사. 어느 encoder 가 본인 task 에 더 semantically sensible neighbour 주는지 봐.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.