Lazy loaded image11.1 Vision Transformer

notion image
Vision Transformer (ViT) is a model that applies the Transformer architecture to image classification tasks. The core idea of ViT is to divide an image into multiple small blocks (patches) and treat these small blocks as a sequence input to the Transformer Encoder. The ViT model is mainly composed of the following three modules:
  1. Linear Projection of Flattened Patches (Embedding Layer)
  1. Transformer Encoder
  1. MLP Head (Classification Head)

Input

The input requirement for a Transformer is a 2D matrix [num_token, token_dim], without considering the batch for now, while the image format is a 3D matrix [H, W, C]. To convert the image into the format required by Transformer, ViT adopts the following steps:
  1. Image Patchification: Divide the input image (e.g., 224x224) into multiple small patches of fixed size (e.g., 16x16). For example, in ViT-B/16, an image is divided into patches.
  1. Linear Projection: Each patch (shape [16, 16, 3]) is flattened into a vector of length 768 (token) and then passed through a linear projection. That is, [16, 16, 3] -> [768].
    1. Note that this flattening and projection operations can be simplified as a single convolution operation in practical implementation. The convolution kernel size is 16x16, and the stride is 16. Input [224, 224, 3] passes through the convolutional layer to get [14, 14, 768], and then is flattened to [196, 768].
  1. [class] token: Similar to BERT, ViT inserts a learnable [class] token at the beginning of the token sequence for classification tasks. This token is concatenated with other tokens, resulting in [197, 768].
  1. Position Embedding: To retain the position information of the image, ViT introduces a learnable 1D Position Embedding, which is directly added to the tokens. Its shape is the same as the tokens, i.e., [197, 768].
Code Example

Transformer Encoder

The Transformer Encoder is stacked with multiple Encoder Blocks, and each Encoder Block contains the following components:
  1. Layer Norm: Normalizes each token.
  1. Multi-Head Attention: Multi-head self-attention mechanism, used to capture relationships between tokens.
  1. Dropout/DropPath: Used to prevent overfitting. The original paper used Dropout, while some implementations used DropPath (stochastic depth) such as in the Swin Transformer.
  1. MLP Block: Composed of a fully connected layer, GELU activation function, and Dropout. The first fully connected layer expands the input node count by 4 times ([197, 768] -> [197, 3072]), and the second fully connected layer restores it ([197, 3072] -> [197, 768]).
notion image
The output of the Transformer Encoder maintains the same shape as the input. Taking ViT-B/16 as an example, if the input is [197, 768], the output is still [197, 768]. There is also a Layer Norm layer at the end of the Encoder.
Code Example
  • MLP Block
    • Encoder

      MLP Head

      From the output of the Transformer Encoder, only the result corresponding to the [class] token needs to be extracted ([1, 768]). The MLP Head is used to map the [class] token to the final classification result. The structure of the MLP Head is as follows:
      1. When training on ImageNet21K: Linear + tanh activation function + Linear.
      1. When migrating to ImageNet1K or other datasets: Only one Linear layer is used.

      Model Parameters

      ViT (Vision Transformer) mainly includes three models: Base, Large, and Huge. Additionally, the source code also provides configurations for Patch Size of 16x16 and 32x32.
      • Layers: Determines the depth of the model. The more layers, the stronger the expressive power of the model, but the higher the computational complexity.
      • Hidden Size: Determines the dimension of the vector for each token. The larger the dimension, the stronger the expressive power of the model.
      • MLP Size: Determines the number of nodes in the first fully connected layer of the MLP Block, which is usually four times the Hidden Size.
      • Heads: Determines the number of heads in Multi-Head Attention. The more heads, the more feature information the model can capture.
      • Patch Size: Determines how many patches an image is divided into. The smaller the patch, the more detailed information the model can capture.
      notion image
      Prev
      11.0 Introduction
      Next
      11.2 CLIP
      Loading...
      Article List
      LLM Learning Roadmap
      ✨ Awesome-Anything
      🖼️ Digital Image Processing
      🍃 LLM Components
      🌱 LLM Pre-training
      ☘️ LLM Post-Training
      🍀 LLM Popular Models
      🪴 LLM Applications
      🌿 LLM Optimization
      🌾 LLM Compression
      🌵 LLM Hands-on Practice
      🌴 LLM Must-read Papers
      🌳 LLM Q&A
      🐝 VLM Image Encoders
      📝 MISC.