Vision Transformers Explained
Introduction to Vision Transformers
Applying Transformers to Images
For a long time, Convolutional Neural Networks (CNNs) were the undisputed champions of computer vision. Their design, inspired by the human visual cortex, is brilliant for recognizing patterns. A CNN scans an image with small filters, detecting edges, textures, and simple shapes. It then combines these simple features into more complex ones in deeper layers. This hierarchical approach is powerful but has a key limitation: its field of view is inherently local. A single filter only sees a small patch of the image at a time. To understand the whole picture, the network has to build up context layer by layer.
Meanwhile, in the world of Natural Language Processing (NLP), Transformer models were causing a revolution. Their secret weapon is the self-attention mechanism, which allows the model to weigh the importance of all words in a sentence simultaneously, no matter how far apart they are. This is great for capturing long-range dependencies and understanding context. Researchers began to wonder: could this powerful, context-aware architecture be adapted for vision tasks?
The ViT model splits each image into a sequence of tokens with fixed length and then applies multiple Transformer layers to model their global relation for classification.
The answer was the Vision Transformer (ViT). Instead of processing pixels with sliding filters, a ViT treats an image like a sentence.
How a ViT Sees the World
A ViT doesn't look at an image pixel by pixel. Instead, it breaks the image down into a grid of fixed-size patches, like a mosaic. Each patch is then 'flattened' from a 2D square into a long, 1D sequence of numbers called a vector. You can think of each patch as a 'word' and the whole collection of patches as a 'sentence'.
Just like words in a sentence, the position of each patch matters. To retain this spatial information, the model adds 'positional encodings' to each vector. This tells the Transformer where each patch originally came from in the image (e.g., 'top-left', 'bottom-right').
Finally, this sequence of patch vectors is fed into a standard Transformer encoder. The self-attention mechanism gets to work, comparing every patch to every other patch. It learns which parts of the image are most relevant to each other to understand the overall scene. For example, it can directly relate a patch from a dog's ear at the top of an image to a patch of its tail at the bottom, without needing to pass through many intermediate layers.
ViTs vs. CNNs
The core difference between ViTs and CNNs lies in how they 'see'. A CNN has built-in assumptions about images, known as inductive biases. The two main ones are:
- Locality: The idea that pixels close to each other are related. This is why CNNs use small filters to look at local neighborhoods.
- Translation Equivariance: The idea that if you move an object in an image, its identity doesn't change. A cat is a cat whether it's in the top-left or bottom-right corner.
These biases are very helpful, allowing CNNs to learn efficiently from relatively small datasets. ViTs, on the other hand, have almost no such built-in knowledge. They don't inherently assume that nearby patches are more important than distant ones. They must learn all visual patterns, including the very concept of locality, entirely from the data.
This lack of inductive bias makes ViTs more flexible, but also much more data-hungry.
| Feature | Convolutional Neural Network (CNN) | Vision Transformer (ViT) |
|---|---|---|
| Core Operation | Convolution (sliding local filters) | Self-Attention (global comparison) |
| Data Processing | Hierarchical, from local to global features | Parallel, all patches at once |
| Inductive Bias | Strong (locality, translation equivariance) | Weak (few built-in assumptions) |
| Data Requirement | Can work well on smaller datasets | Requires very large datasets to perform well |
Advantages and Challenges
The greatest advantage of ViTs is their ability to see the bigger picture. By weighing the relationships between all parts of an image, they can capture global context effortlessly. This is especially useful for images where understanding the relationship between distant objects is key.
However, this power comes at a cost. Without the head start provided by inductive biases, ViTs need to be trained on enormous datasets (think millions of images) to learn the fundamental rules of vision that CNNs take for granted. When trained on smaller datasets, a well-tuned CNN often outperforms a ViT. But when scaled up with enough data and computing power, ViTs have shown they can surpass the performance of even the best CNNs.
The introduction of Vision Transformers marked a significant shift, proving that the architecture that transformed language processing could also redefine the cutting edge of computer vision.
What is the core mechanism that Vision Transformers (ViTs) borrow from Natural Language Processing models to understand relationships between different parts of an image?
How does a Vision Transformer (ViT) handle the spatial information of an image after breaking it into patches?