SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
- Publication
- NeurIPS 2021Conference
- First submitted to arXiv
- arXiv:2105.15203
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{segmentationsegformer2021,
title = {{SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers}},
author = {Enze Xie and Wenhai Wang and Zhiding Yu and Anima Anandkumar and Jose M. Alvarez and Ping Luo},
year = {2021},
eprint = {2105.15203},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2105.15203}
}
After ViT showed in 2020 that Transformers could do image classification, people quickly wanted to move them over to segmentation. At the end of 2020, SETR took ViT-Large directly as its backbone, attached a few convolutional decoders, and reached 50.2% mIoU on ADE20K.
The results were good, but the model was hard to use. SETR has 318 million parameters and runs at less than one image per second on Cityscapes; ViT outputs only one resolution, and its positional encoding is tied to the image size used in training.
SegFormer, from Enze Xie and Ping Luo of the University of Hong Kong together with a team at NVIDIA, redesigns the encoder and the decoder together. Its largest model reaches 51.8% on ADE20K with just over a quarter of SETR's parameters, and its smallest model has fewer than 4 million parameters and runs in real time.
Defining the Problem
The CNN segmentation models in the previous notes were mostly a backbone plus a heavy decoder. DeepLab has ASPP and PSPNet has pyramid pooling, both there to make up for the CNN's receptive field not being large enough.
ViT's receptive field is not the problem, since self-attention sees the whole image from the very start. It has other weaknesses.
First, ViT cuts the image into patches and has only one resolution from start to finish. Segmentation needs high-resolution detail as well as low-resolution semantics, and ViT provides only the latter.
Second, the cost of self-attention grows with the square of the number of tokens. Segmentation inputs are often , and cut into small patches the token count explodes.
Third, ViT's positional encoding has a fixed length. When the test image size differs from training, the positional encoding has to be interpolated, and accuracy drops. Segmentation, of all tasks, often runs inference at a different resolution, for example feeding Cityscapes' full images straight in.
PVT, Swin, Twins and other hierarchical Transformers solved the first two problems; like CNNs, they output multi-scale features, and they keep the computation down. But the authors observed that these works changed almost nothing but the encoder, copying the decoder from CNN-era designs. Transformer features differ from CNN features, so shouldn't the decoder differ too?
Solving the Problem

SegFormer has just two parts. On the left is a hierarchical Transformer encoder, which the authors call the Mix Transformer (MiT), in six sizes from B0 to B5; on the right is a decoder made only of MLPs.
Let's walk through it with the input used for ADE20K training and the medium-sized MiT-B2. B2's four stages have 64, 128, 320 and 512 channels.
Overlapping Patches
The first step cuts the image into patches. ViT uses non-overlapping patches; SegFormer uses a convolution with stride 4 and padding 3, so neighboring patches overlap.
The image becomes tokens of 64 dimensions each, at of the original resolution. The next three stages each halve it again with a , stride-2 convolution, giving , and in turn.
The reason for the overlap is to keep continuity around patch borders. A non-overlapping cut can split an object's edge right between two patches, and the information on either side is separated from the very start.
Shrinking K and V
The first stage has tokens. Standard self-attention computes a matrix, about 270 million entries, once per layer and per head.
SegFormer keeps PVT's sequence reduction. The queries stay at the original 16,384, while keys and values are first shrunk spatially before the computation. The paper writes this as merging every neighboring tokens into one:
The first stage uses , which amounts to shrinking each side 8× in space. The official implementation uses a convolution whose kernel size and stride both equal 8, with the same effect. The features shrink to tokens for K and V, and the attention matrix becomes , 64 times smaller.
The four stages use reduction ratios of 64, 16, 4 and 1 (8, 4, 2 and 1 per side). Working through our example:
| Stage | Tokens | Per side | K, V tokens |
|---|---|---|---|
| 1 | 8 | 256 | |
| 2 | 4 | 256 | |
| 3 | 2 | 256 | |
| 4 | 1 | 256 |
With a input, every token in every stage attends over 256 keys and values. The reduction ratios are chosen so that K and V have the same length in all four stages.
No Positional Encoding
ViT needs positional encoding because self-attention on its own doesn't know where the tokens are. SegFormer removes it entirely and instead puts a convolution inside the feed-forward network:
This is called Mix-FFN. The convolution is depthwise and has very few parameters.
Without positional encoding, how does the model know about position? The authors cite Islam et al., who showed that when a convolution zero-pads at the borders, it leaks "how far from the border" to the network, so a single convolution is enough to supply the position information a Transformer needs. CPVT also generates position information with convolutions, but what it produces is still a positional encoding added back to the tokens; SegFormer drops that encoding altogether and puts the convolution straight into the FFN.
The benefit is that the resolution can change freely. With no fixed-length positional encoding, there's nothing to interpolate.
A Decoder in Four Steps
The encoder outputs four sets of features, at resolutions from to . The decoder does four things:
- Each set of features goes through its own linear layer, which brings the channel count to a common .
- Everything is upsampled to , which here is .
- The results are concatenated into channels, and one linear layer fuses them back to .
- A final linear layer outputs a score for each class; ADE20K has 150.
As equations,
No ASPP, no pyramid pooling, no convolutions. From B2 up, is 768 and the decoder has 3.3 million parameters in total; for B0 and B1, is 256, and B0's decoder has only 0.4 million parameters. In the official implementation the fusion layer is a convolution plus BatchNorm, equivalent to a linear layer.
Why is such a simple decoder enough? The authors explain it with the effective receptive field (ERF).

This figure is averaged over 100 Cityscapes images. Look at DeepLabv3+ in the top row. Even at the deepest stage, stage 4, its effective receptive field is still a small central patch, which is why it needs ASPP in the decoder head to make up the receptive field. The radial bright dots in the decoder head panel look like the traces of ASPP's several different sampling intervals.
SegFormer in the bottom row is different. The attention in the first two stages is very local and looks like convolution; by stage 4, the whole image is lit up. In the zoomed view at the far right, the decoder head has a more concentrated central bright spot than stage 4 does.
The authors' reading is that SegFormer's low-level features are naturally local and its high-level features naturally global, so the decoder only has to add the levels together to get both local detail and global context. A CNN's high-level receptive field is small to begin with, so the same decoder after a CNN falls short, which the experiments will confirm.
Discussion
Model Sizes
The experiments are trained with mmsegmentation on 8 V100s, with AdamW and a learning rate of , for 160K iterations on ADE20K and Cityscapes, and without common tricks such as OHEM, auxiliary losses or class balancing. Table 1a of the paper lists six sizes:
| Encoder | Params (enc.+dec.) | ADE20K mIoU | Cityscapes mIoU | COCO-Stuff mIoU |
|---|---|---|---|---|
| MiT-B0 | 3.4M+0.4M | 37.4 | 76.2 | 35.6 |
| MiT-B1 | 13.1M+0.6M | 42.2 | 78.5 | 40.2 |
| MiT-B2 | 24.2M+3.3M | 46.5 | 81.0 | 44.6 |
| MiT-B3 | 44.0M+3.3M | 49.4 | 81.7 | 45.5 |
| MiT-B4 | 60.8M+3.3M | 50.3 | 82.3 | 46.5 |
| MiT-B5 | 81.4M+3.3M | 51.0 | 82.4 | 46.7 |
All numbers are single-scale testing. As the encoder grows, all three datasets improve steadily; in B5 the decoder makes up only about 4% of the total parameters.
The decoder's channel count was tested too (Table 1b of the paper). On ADE20K, raising from 256 to 768 takes mIoU from 44.9 to 45.4, after which it levels off; gives 45.6, while the computation jumps from 25.7 to 304.4 GFLOPs.
Is Positional Encoding Really Unnecessary?
Table 1c of the paper compares two ways of running inference on Cityscapes. One uses a sliding window; the other feeds the whole image straight in.
| Encoder | window | whole |
|---|---|---|
| Positional encoding | 77.3 | 74.0 |
| Mix-FFN | 80.5 | 79.8 |
The positional-encoding version loses 3.3 points when switched to the whole image; Mix-FFN loses only 0.7. And with the same sliding window, Mix-FFN is already 3.2 points higher on its own.
Does the Decoder Really Only Work with a Transformer?
Table 1d of the paper attaches the same MLP decoder to different encoders and compares them on ADE20K:
| Encoder | GFLOPs | mIoU |
|---|---|---|
| ResNet-50 (stage 1–4) | 69.2 | 34.7 |
| ResNet-101 (stage 1–4) | 88.7 | 38.7 |
| ResNeXt-101 (stage 1–4) | 127.5 | 39.8 |
| MiT-B2 (stage 4 only) | 22.3 | 43.1 |
| MiT-B2 (stage 1–4) | 62.4 | 45.4 |
Behind a CNN, even the best encoder, ResNeXt-101, reaches only 39.8, at twice the computation of MiT-B2. This echoes the ERF analysis; the CNN's receptive field isn't large enough, and an MLP alone can't make up for it. MiT-B2 using only the last stage gets 43.1, and using all four stages gets 45.4, so the low-level local features really do help.
Against Other Methods
Table 2 of the paper is split into real-time and non-real-time groups. In the real-time group, SegFormer-B0 reaches 37.4% on ADE20K with 3.8M parameters and 8.4 GFLOPs, 3.4 points above DeepLabV3+ with a MobileNetV2 backbone, using only a quarter of its parameters.
The non-real-time group reports multi-scale test results on ADE20K. SETR gets 50.2% with 318.3M parameters, SegFormer-B4 51.1% with 64.1M, and B5 51.8% with 84.7M. On the Cityscapes validation set, B5 gets 84.0% and SETR 82.2%, with B5 running at 2.5 images per second and SETR at 0.5.
The introduction says B0 runs on Cityscapes "at 48 images per second with 71.9%, faster than ICNet and 4.2 points more accurate." Those numbers correspond to the setting in Table 2 that resizes the short side to 512, which is , only a quarter of the original image's pixels. The same B0 with a short side of 1024 gets 76.2% at 15.2 images per second. ICNet's 30.3 FPS, meanwhile, comes from ICNet's own paper, and the hardware may not be the same either. As with BiSeNet in the previous note, real-time segmentation FPS always has to be read together with the resolution and the hardware.
On the Cityscapes test set (Table 3 of the paper), B5 pretrained only on ImageNet-1K gets 82.2%, above SETR (81.6%), which used ImageNet-22K plus coarse annotations; adding Mapillary pretraining brings it to 83.1%.
Robustness
The last set of experiments is on Cityscapes-C. This is the Cityscapes validation set with 16 kinds of synthetic corruption, covering noise, blur, weather and digital distortion, used to test degradations the model has never seen.
A few of the columns (Table 5 of the paper, mIoU):
| Method | Clean | Gauss noise | Shot noise | Snow | Fog |
|---|---|---|---|---|---|
| DeepLabv3+ (ResNet-101) | 77.1 | 13.2 | 16.3 | 11.9 | 55.1 |
| PSPNet | 78.8 | 11.0 | 15.4 | 8.4 | 34.4 |
| SegFormer-B5 | 82.4 | 57.8 | 52.3 | 40.7 | 78.5 |
On clean images SegFormer is only 3.6 points above PSPNet, but with Gaussian noise added, PSPNet is left with 11.0 while SegFormer still has 57.8. The authors read this as the robustness of the Transformer encoder. The other methods' numbers in the table, however, all come from Kamann and Rother's benchmark paper, while SegFormer's were run by the authors themselves; the experiment also doesn't separate how much comes from "the Transformer architecture" and how much from "a bigger model and a better training setup."
At the end, the paper admits one limitation. B0 is smaller than any known CNN model, but whether it can fit on an edge chip with only 100k of memory, the authors don't know either.
Conclusion
The encoder handles multiple scales and context, and the decoder just adds the four levels of features together; that is how SegFormer divides the work. Overlapping patches, attention with reduced K/V and Mix-FFN let the encoder do without positional encoding, so even inference on whole images loses only 0.7 points; from B0 to B5, the same design scales from a real-time model all the way up to 51.8% on ADE20K.
If you want to use it right away, Hugging Face's transformers has SegformerForSemanticSegmentation, and weights for B0 through B5 are all available to download.
Just don't take that MLP decoder off and bolt it onto a ResNet. The paper tried; ResNeXt-101 with it gets only 39.8, more than 5 points below MiT-B2, and the decoder is only enough when it sits behind a Transformer encoder.