Vision Transformers
Module: Computer Vision | Difficulty: Advanced
Vision Transformer (ViT)
Patch Embedding
Split image into patches:
where is the patch embedding matrix.
Self-Attention
Multi-Head Attention
where
Swin Transformer
Hierarchical transformer with shifted windows:
import torch
import torch.nn as nn
class PatchEmbedding(nn.Module):
def __init__(self, img_size=224, patch_size=16, in_ch=3, embed_dim=768):
super().__init__()
self.num_patches = (img_size // patch_size) ** 2
self.proj = nn.Conv2d(in_ch, embed_dim, kernel_size=patch_size, stride=patch_size)
self.cls_token = nn.Parameter(torch.randn(1, 1, embed_dim))
self.pos_embed = nn.Parameter(torch.randn(1, self.num_patches + 1, embed_dim))
def forward(self, x):
B = x.size(0)
x = self.proj(x).flatten(2).transpose(1, 2)
cls = self.cls_token.expand(B, -1, -1)
x = torch.cat([cls, x], dim=1)
return x + self.pos_embed
class TransformerBlock(nn.Module):
def __init__(self, dim, heads, mlp_ratio=4.0):
super().__init__()
self.norm1 = nn.LayerNorm(dim)
self.attn = nn.MultiheadAttention(dim, heads, batch_first=True)
self.norm2 = nn.LayerNorm(dim)
self.mlp = nn.Sequential(
nn.Linear(dim, int(dim * mlp_ratio)),
nn.GELU(),
nn.Linear(int(dim * mlp_ratio), dim)
)
def forward(self, x):
x = x + self.attn(self.norm1(x), self.norm1(x), self.norm1(x))[0]
return x + self.mlp(self.norm2(x))
Key Takeaways
- ViT treats images as sequences of patches for transformer processing
- Swin Transformer introduces hierarchical computation with shifted windows
- Transformers achieve SOTA when pre-trained on large datasets