πŸ€– AI/ML

Vision Transformer

ViT

A vision model that applies the Transformer to images. Splits images into patches, used as an alternative to CNNs.

This term is the same concept as ViT. Please refer to the ViT page for details.

πŸ“– Detailed Explanation

Vision Transformer (ViT) is an image classification model announced by Google Research in 2020, which was the first to successfully apply the Transformer architecture, which had succeeded in natural language processing, to computer vision. In the paper "An Image is Worth 16x16 Words," it presented an innovative approach of splitting an image into 16x16 pixel patches and processing them like words.

Before ViT, computer vision was dominated by CNNs (Convolutional Neural Networks). CNNs are effective at extracting local features, but they had limitations in capturing global relationships across an entire image. ViT solved this problem by simultaneously learning the relationships among all patches of an image through the Self-Attention mechanism.

ViT works as follows. The input image is split into fixed-size patches (usually 16x16), and each patch is converted into a linear embedding. After positional information is added, the embeddings are fed into the Transformer encoder, which learns the relationships between patches through Multi-Head Self-Attention. The output of the CLS token is used to perform the final classification.

In practice, ViT outperforms CNNs on large-scale datasets such as ImageNet. It is particularly strong in the pre-training followed by fine-tuning paradigm, and it is widely used as the vision encoder of multimodal models such as CLIP, DALL-E, and Stable Diffusion.

πŸ’» Code Example

import torch
from transformers import ViTImageProcessor, ViTForImageClassification
from PIL import Image
import requests

# λͺ¨λΈκ³Ό ν”„λ‘œμ„Έμ„œ λ‘œλ“œ
model_name = "google/vit-base-patch16-224"
processor = ViTImageProcessor.from_pretrained(model_name)
model = ViTForImageClassification.from_pretrained(model_name)

# 이미지 λ‘œλ“œ
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)

# μΆ”λ‘ 
inputs = processor(images=image, return_tensors="pt")
with torch.no_grad():
    outputs = model(**inputs)

# 예츑 결과
predicted_class_idx = outputs.logits.argmax(-1).item()
print(f"예츑: {model.config.id2label[predicted_class_idx]}")

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting

"We're trying to build an image classification model with Vision Transformer, but if the data is limited, fine-tuning a pre-trained model is more effective."

πŸ’¬ In an Interview

"ViT is short for Vision Transformer. It splits an image into patches and processes them like a Transformer in NLP. Unlike CNNs, it can learn global relationships."

πŸ’¬ In a Technical Discussion

"A Vision Transformer trained with self-supervised methods like CLIP or DINO has excellent versatility on downstream tasks."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌
Training from scratch on small datasets

ViT requires large-scale data. With small datasets, fine-tune a pre-trained model.

❌
Ignoring resolution mismatch

Performance degrades when you feed in images with a resolution different from the one used during training. Use the processor's automatic resizing.

βœ…
Choose a variant suited to the task

For object detection, consider task-specific optimized models such as Swin Transformer, and for segmentation, SegFormer.

πŸ”— Related Terms

πŸ“š Learn More