Computer Vision Lab Models SegFormer

SegFormer

Hierarchical transformer encoder + MLP decoder

Knowledge card: semantic segmentation with a hierarchical transformer. Not loaded because the licence of the official weights does not allow distribution.

SegmentationKnowledge card

ArchitectureHierarchical transformer encoder + MLP decoder
TaskSegmentation
Training dataADE20K / Cityscapes
the number of parametersB0: ≈3.8M
FrameworkPyTorch
File size—
LicenceCheck the licence of the official weights (NVIDIA)
Where it runsKnowledge card only
Official sourcedoi.org

Architecture diagram

SegFormer architecture diagramInside an MiT blockInputH×W×3Overlap patch embed + MiT blockH/4 × W/4 × C1MLP → C, upsample to H/4Overlap patch embed + MiT blockH/8 × W/8 × C2MLP → C, upsample to H/4Overlap patch embed + MiT blockH/16 × W/16 × C3MLP → C, upsample to H/4Overlap patch embed + MiT blockH/32 × W/32 × C4MLP → C, upsample to H/4Cconcat 4 × CMLP fuseCMLP classifierH/4 × W/4 × N_clsEfficient self-attentionK, V reduced by ratio RMix-FFNMLP + 3×3 depthwise conv
SegFormer by Xie et al. (2021): a hierarchical transformer encoder (Mix Transformer) without positional encoding, outputting features at four scales, and a light decoder made entirely of MLP layers.
  • Input / output
  • Convolutional layer
  • Attention / transformer
  • Output head
  • Operation (add, multiply, merge)

Detailed model explanation

Idea and intuition

SegFormer combines a hierarchical transformer encoder with a very simple decoder made only of MLP layers. The paper argues that the hierarchical transformer produces strong multi-scale features with wide receptive fields, so no complex decoder is needed. The encoder also abandoned the fixed positional encoding, replacing it with a 3×3 depthwise convolution inside the FFN layer (Mix-FFN) that provides position information implicitly, so the model works at test resolutions that differ from the training resolution without degradation.

The Mix Transformer (MiT) encoder

  • Four stages at resolutions 1/4, 1/8, 1/16 and 1/32.
  • Overlapped patch merging (overlapped patch merging) with a strided convolution, to preserve local continuity.
  • Efficient self-attention: the sequence length of the keys and values is reduced by a ratio R so the cost falls from O(N^2) into O(N^2/R).
  • Mix-FFN: x_{out}=\mathrm{MLP}(\mathrm{GELU}(\mathrm{DWConv}_{3\times3}(\mathrm{MLP}(x_{in}))))+x_{in}.

The All-MLP decoder

  1. Unify the channels of the four feature maps with a linear layer.
  2. Upsample them all to 1/4 resolution and concatenate them.
  3. A linear layer for fusion, then a linear layer for classification.

Mathematical formulation

\hat K = \mathrm{Linear}(C\cdot R, C)\big(\mathrm{Reshape}(N/R,\ C\cdot R)(K)\big)

Strengths and limitations

  • Strength: an excellent balance between accuracy and size; the smallest version (B0) has about 3.8 million parameters.
  • Strength: robustness to resolution changes and image corruptions, according to the paper's experiments on Cityscapes-C.
  • Limitation: the licence of NVIDIA's official weights does not allow redistribution here, so it is a knowledge card only.
  • Limitation: attention is still costly on very large images.

Uses

Semantic segmentation.

Runtime requirements

—

How to load

Not loaded.

References

  1. E. Xie et al. (2021).SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers. arXiv:2105.15203 (NeurIPS 2021). doi:10.48550/arXiv.2105.15203

Related algorithms