Vision Transformer
ViT
A vision model that applies the Transformer to images. Splits images into patches, used as an alternative to CNNs.
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.
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.
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]}")
"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."
"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."
"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
ViT requires large-scale data. With small datasets, fine-tune a pre-trained model.
Performance degrades when you feed in images with a resolution different from the one used during training. Use the processor's automatic resizing.
For object detection, consider task-specific optimized models such as Swin Transformer, and for segmentation, SegFormer.