The Vision Transformer (ViT) architecture adapts the transformer encoder, originally designed for linguistic sequences, to image processing by treating image patches as tokens. The process begins by partitioning an image into a grid of non-overlapping 16x16 pixel patches; for a 224x224 input, this results in 196 patches. Each patch is flattened into a 768-dimensional vector and transformed via a linear projection—implemented as a 16x16 convolution with a stride of 16—into a D-dimensional token. To provide spatial context and classification capabilities, a learnable [CLS] token is prepended to the sequence, and positional embeddings are added to each token to encode their original grid locations. Unlike Convolutional Neural Networks (CNNs) that rely on inductive biases like locality and translation-equivariance, ViT utilizes multi-head self-attention, providing an immediate global receptive field where every token attends to every other token from the first layer. However, this comes at a quadratic computational cost, $O(L^2)$, where $L$ is the sequence length; halving the patch size quadruples the token count and increases attention complexity roughly sixteen-fold. While ViT lacks the built-in spatial priors of CNNs, making it highly data-hungry, its ability to model long-range dependencies directly allows it to outperform traditional models when pre-trained on massive datasets like JFT-300M.
Read original
dev.to