The Vision Transformer Model
- Get link
- X
- Other Apps
Last Updated on January 6, 2023
With the Transformer construction revolutionizing the implementation of consideration, and attaining very promising results in the pure language processing space, it was solely a matter of time sooner than we would see its utility throughout the laptop computer imaginative and prescient space too. This was lastly achieved with the implementation of the Vision Transformer (ViT).
In this tutorial, you will uncover the construction of the Vision Transformer model, and its utility to the responsibility of image classification.
After ending this tutorial, you will know:
- How the ViT works throughout the context of image classification.
- What the teaching technique of the ViT entails.
- How the ViT compares to convolutional neural networks in the case of inductive bias.
- How the ViT fares in direction of ResNets on completely totally different datasets.
- How the data is processed internally for the ViT to achieve its effectivity.
Kick-start your problem with my e-book Building Transformer Models with Attention. It provides self-study tutorials with working code to data you into developing a fully-working transformer model that will
translate sentences from one language to a unique…
Let’s get started.

The Vision Transformer Model
Photo by Paul Skorupskas, some rights reserved.
Tutorial Overview
This tutorial is break up into six parts; they’re:
- Introduction to the Vision Transformer (ViT)
- The ViT Architecture
- Training the ViT
- Inductive Bias in Comparison to Convolutional Neural Networks
- Comparative Performance of ViT Variants with ResNets
- Internal Representation of Data
Prerequisites
For this tutorial, we assume that you simply’re already accustomed to:
- The concept of consideration
- The Transfomer consideration mechanism
- The Transformer Model
Introduction to the Vision Transformer (ViT)
We had seen how the emergence of the Transformer construction of Vaswani et al. (2023) has revolutionized utilizing consideration, with out relying on recurrence and convolutions as earlier consideration fashions had beforehand carried out. In their work, Vaswani et al. had utilized their model to the exact disadvantage of pure language processing (NLP).
In laptop computer imaginative and prescient, nonetheless, convolutional architectures keep dominant …
– An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale, 2023.
Inspired by its success in NLP, Dosovitskiy et al. (2023) sought to make use of the same old Transformer construction to pictures, as we are going to see shortly. Their objective utility on the time was image classification.
Want to Get Started With Building Transformer Models with Attention?
Take my free 12-day e mail crash course now (with sample code).
Click to sign-up and as well as get a free PDF Ebook mannequin of the course.
The ViT Architecture
Recall that the same old Transformer model obtained a one-dimensional sequence of phrase embeddings as enter, as a result of it was initially meant for NLP. In distinction, when utilized to the responsibility of image classification in laptop computer imaginative and prescient, the enter data to the Transformer model is obtainable inside the kind of two-dimensional footage.
For the intention of structuring the enter image data in a approach that resembles how the enter is structured throughout the NLP space (throughout the sense of getting a sequence of explicit individual phrases), the enter image, of prime $H$, width $W$, and $C$ number of channels, is decrease up into smaller two-dimensional patches. This outcomes into $N = tfrac{HW}{P^2}$ number of patches, the place each patch has a call of ($P, P$) pixels.
Before feeding the data into the Transformer, the subsequent operations are utilized:
- Each image patch is flattened proper right into a vector, $mathbf{x}_p^n$, of dimension $P^2 cases C$, the place $n = 1, dots N$.
- A sequence of embedded image patches is generated by mapping the flattened patches to $D$ dimensions, with a trainable linear projection, $mathbf{E}$.
- A learnable class embedding, $mathbf{x}_{textual content material{class}}$, is prepended to the sequence of embedded image patches. The price of $mathbf{x}_{textual content material{class}}$ represents the classification output, $mathbf{y}$.
- The patch embeddings are lastly augmented with one-dimensional positional embeddings, $mathbf{E}_{textual content material{pos}}$, due to this fact introducing positional information into the enter, which might be found all through teaching.
The sequence of embedding vectors that outcomes from the aforementioned operations is the subsequent:
$$mathbf{z}_0 = [ mathbf{x}_{text{class}}; ; mathbf{x}_p^1 mathbf{E}; ; dots ; ; mathbf{x}_p^N mathbf{E}] + mathbf{E}_{textual content material{pos}}$$
Dosovitskiy et al. make use of the encoder part of the Transformer construction of Vaswani et al.
In order to hold out classification, they feed $mathbf{z}_0$ on the enter of the Transformer encoder, which consists of a stack of $L$ related layers. Then, they proceed to take the value of $mathbf{x}_{textual content material{class}}$ on the $L^{textual content material{th}}$ layer of the encoder output, and feed it proper right into a classification head.
The classification head is carried out by a MLP with one hidden layer at pre-training time and by a single linear layer at fine-tuning time.
– An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale, 2023.
The multilayer perceptron (MLP) that sorts the classification head implements Gaussian Error Linear Unit (GELU) non-linearity.
In summary, attributable to this truth, the ViT employs the encoder part of the distinctive Transformer construction. The enter to the encoder is a sequence of embedded image patches (along with a learnable class embedding prepended to the sequence), which might be augmented with positional information. A classification head hooked as much as the output of the encoder receives the value of the learnable class embedding, to generate a classification output based mostly totally on its state. All of that’s illustrated by the decide beneath:

The Architecture of the Vision Transformer (ViT)
Taken from “An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale“
One further phrase that Dosovitskiy et al. make, is that the distinctive image can, alternatively, be fed proper right into a convolutional neural group (CNN) sooner than being handed on to the Transformer encoder. The sequence of image patches would then be obtained from the perform maps of the CNN, whereas the next technique of embedding the perform map patches, prepending a class token, and augmenting with positional information stays the an identical.
Training the ViT
The ViT is pre-trained on greater datasets (akin to ImageInternet, ImageInternet-21k and JFT-300M) and fine-tuned to a smaller number of programs.
During pre-training, the classification head in use that is hooked as much as the encoder output, is carried out by a MLP with one hidden layer and GELU non-linearity, as has been described earlier.
During fine-tuning, the MLP is modified by a single (zero-initialized) feedforward layer of dimension, $D cases Ok$, with $Ok$ denoting the number of programs similar to the responsibility at hand.
Fine-tuning is carried out on footage which might be of higher determination than these used all through pre-training, nonetheless the patch dimension into which the enter footage are decrease is saved the an identical the least bit ranges of teaching. This results in an enter sequence of larger dimension on the fine-tuning stage, in comparison with that used all through pre-training.
The implication of getting a lengthier enter sequence is that fine-tuning requires further place embeddings than pre-training. To circumvent this disadvantage, Dosovitskiy et al. interpolate, in two-dimensions, the pre-training place embeddings in response to their location throughout the genuine image, to accumulate an prolonged sequence that matches the number of image patches in use all through fine-tuning.
Inductive Bias in Comparison to Convolutional Neural Networks
Inductive bias refers to any assumptions {{that a}} model makes to generalise the teaching data and research the objective function.
In CNNs, locality, two-dimensional neighborhood building, and translation equivariance are baked into each layer all by means of your entire model.
– An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale, 2023.
In convolutional neural networks (CNNs), each neuron is just linked to totally different neurons in its neighborhood. Furthermore, since neurons residing on the an identical layer share the an identical weight and bias values, any of these neurons will activate when a perform of curiosity falls inside its receptive self-discipline. This results in a perform map that is equivariant to perform translation, which suggests that if the enter image is translated, then the perform map might be equivalently translated.
Dosovitskiy et al. argue that throughout the ViT, solely the MLP layers are characterised by locality and translation equivariance. The self-attention layers, then once more, are described as world, because of the computations which might be carried out at these layers are normally not constrained to an space two-dimensional neighborhood.
They make clear that bias regarding the two-dimensional neighborhood building of the pictures is just used:
- At the enter to the model, the place each image is decrease into patches, due to this fact inherently retaining the spatial relationship between the pixels in each patch.
- At fine-tuning, the place the pre-training place embeddings are interpolated in two-dimensions in response to their location throughout the genuine image, to offer an prolonged sequence that matches the number of image patches in use all through fine-tuning.
Comparative Performance of ViT Variants with ResNets
Dosovitskiy et al. pitted three ViT fashions of accelerating dimension, in direction of two modified ResNets of varied sizes. Their experiments yield plenty of fascinating findings:
- Experiment 1 – Fine-tuning and testing on ImageInternet:
- When pre-trained on the smallest dataset (ImageInternet), the two greater ViT fashions underperformed in comparison with their smaller counterpart. The effectivity of all ViT fashions stays, normally, beneath that of the ResNets.
- When pre-trained on an even bigger dataset (ImageInternet-21k), the three ViT fashions carried out equally to at the least one one different, along with to the ResNets.
- When pre-trained on a very powerful dataset (JFT-300M), the effectivity of the larger ViT fashions overtakes the effectivity of the smaller ViT and the ResNets.
- Experiment 2 – Training on random subsets of varied sizes of the JFT-300M dataset, and testing on ImageInternet, to further study the influence of the dataset dimension:
- On smaller subsets of the dataset, the ViT fashions overfit higher than the ResNet fashions, and underperform considerably.
- On the larger subset of the dataset, the effectivity of the larger ViT model surpasses the effectivity of the ResNets.
This consequence reinforces the intuition that the convolutional inductive bias is useful for smaller datasets, nonetheless for greater ones, learning the associated patterns instantly from data is ample, even useful.
– An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale, 2023.
Internal Representation of Data
In analysing the inside illustration of the image data throughout the ViT, Dosovitskiy et al. uncover the subsequent:
- The found embedding filters which might be initially utilized to the image patches on the primary layer of the ViT, resemble basis capabilities that will extract the low-level choices inside each patch:

Learned Embedding Filters
Taken from “An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale“
- Image patches which might be spatially shut to at the least one one different throughout the genuine image, are characterised by found positional embeddings which might be associated:

Learned Positional Embeddings
Taken from “An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale“
- Several self-attention heads on the bottom layers of the model already attend to most of the image information (based mostly totally on their consideration weights), demonstrating the potential of the self-attention mechanism in integrating the info all through the whole image:
Further Reading
This half provides further sources on the topic when you’re in search of to go deeper.
Papers
- An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale, 2023.
- Attention Is All You Need, 2023.
Summary
In this tutorial, you discovered the construction of the Vision Transformer model, and its utility to the responsibility of image classification.
Specifically, you found:
- How the ViT works throughout the context of image classification.
- What the teaching technique of the ViT entails.
- How the ViT compares to convolutional neural networks in the case of inductive bias.
- How the ViT fares in direction of ResNets on completely totally different datasets.
- How the data is processed internally for the ViT to achieve its effectivity.
Do you should have any questions?
Ask your questions throughout the suggestions beneath and I’ll do my most interesting to answer.
Learn Transformers and Attention!
Teach your deep learning model to be taught a sentence
…using transformer fashions with consideration
Discover how in my new Ebook:
Building Transformer Models with Attention
It provides self-study tutorials with working code to data you into developing a fully-working transformer fashions that will
translate sentences from one language to a unique…
Give magical power of understanding human language for
Your Projects
See What’s Inside
8 Books for Getting Started With Computer Vision
A Gentle Introduction to Computer Vision
Building Transformer Models with Attention Crash…
How to Develop a CNN From Scratch for CIFAR-10 Photo…
Implementing the Transformer Decoder from Scratch in…
A Gentle Introduction to the Promise of Deep…
- Get link
- X
- Other Apps
Comments
Post a Comment