Back to paper library
arXiv 2020CVPR 2021SegmentationPaper notes

SETR

All the Way Through

Original paper

Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers

Paper authors

Sixiao Zheng · Jiachen Lu · Hengshuang Zhao …

Show all 11 authors

Sixiao Zheng · Jiachen Lu · Hengshuang Zhao · Xiatian Zhu · Zekun Luo · Yabiao Wang · Yanwei Fu · Jianfeng Feng · Tao Xiang · Philip H. S. Torr · Li Zhang

Publication
CVPR 2021Conference
First submitted to arXiv
arXiv:2012.15840
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{segmentationsetr2020,
  title = {{Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformers}},
  author = {Sixiao Zheng and Jiachen Lu and Hengshuang Zhao and Xiatian Zhu and Zekun Luo and Yabiao Wang and Yanwei Fu and Jianfeng Feng and Tao Xiang and Philip H. S. Torr and Li Zhang},
  year = {2020},
  eprint = {2012.15840},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2012.15840}
}
On this page

In October 2020, ViT showed that you can cut an image into small patches, treat them as a string of tokens, feed them to a standard Transformer and still do very well on ImageNet classification. A little over two months later, on December 31, a team from Fudan University, the University of Oxford and Tencent Youtu had already carried it over to semantic segmentation.

The team includes a familiar name, Hengshuang Zhao, an author of PSPNet.

Their argument is that since every Transformer layer can see the whole image, a segmentation network no longer needs to shrink its feature maps step by step the way FCN does and look for ways to enlarge the receptive field. Replace the entire encoder with a pure Transformer, and the resolution stays the same from start to finish.

Defining the Problem

Since FCN, semantic segmentation has almost always used an encoder–decoder structure. The encoder is a stack of convolution layers whose feature maps keep getting smaller while the receptive field grows, learning ever more abstract semantics; the decoder then scales it back up to the original image resolution.

This design works because of convolution's translation equivariance and locality. Locality lets parameters be shared across space, but it also brings a fundamental limit, a finite receptive field that makes long-range dependencies hard to learn. Semantic segmentation happens to need long-range context badly; deciding whether a patch of green is a lawn or a field, for instance, often means looking at the whole image.

Earlier methods patched this in roughly two ways. One changes the convolution itself, with large kernels, atrous convolution, or image or feature pyramids; the other adds attention modules on top of FCN, such as DANet and CCNet. The authors point out that neither changes FCN's basic structure, where the encoder lowers the resolution first and the decoder raises it again. Even Axial-DeepLab, which drops convolution entirely and uses only attention, still follows this pattern.

So SETR tries a more radical alternative: treat segmentation as "sequence-to-sequence" prediction, with the encoder staying at the same resolution throughout and every layer doing global self-attention.

Solving the Problem

Turning the Image into a Sequence

Original Figure 1: (a) SETR's encoder: the image is cut into fixed-size patches, which are linearly projected, given position embeddings and fed into 24 Transformer layers, followed by a decoder that outputs the segmentation map; on the left, the inside of one Transformer layer is enlarged, showing Layer Norm, multi-head attention, Layer Norm and MLP in order, with two residual additions. (b) The progressive upsampling (PUP) decoder: H/16×W/16×1024 features are reshaped and then go through four rounds of "convolution plus 2× upsampling" to reach H×W×19. (c) The multi-level feature aggregation (MLA) decoder: features from the four layers Z⁶, Z¹², Z¹⁸ and Z²⁴ are each reshaped and convolved, added top-down, then concatenated and upsampled.
Original Figure 1, Zheng et al., from PDF page 3, CC BY 4.0.

A Transformer takes a one-dimensional sequence, while an image is two-dimensional, so the first step is turning the image into a sequence.

The most direct way is to treat each pixel as a token. By the authors' count, a 480×480 color image flattens to 691,200 values, and a Transformer's computation grows with the square of the sequence length, so that road is closed.

A typical encoder for segmentation ends with a feature map about 1/16 the size of the input. SETR sets the sequence length to match: it cuts the image into 16×16 patches, giving a sequence length of L=H16×W16L = \frac{H}{16} \times \frac{W}{16}. That way, the Transformer's output can be reshaped straight back into a feature map at 1/16 size.

Let's walk through it with the 768×768 crops used for training on Cityscapes. Each side is cut into 768/16=48768 / 16 = 48 pieces, for 48×48=2,30448 \times 48 = 2{,}304 patches in total. Each patch is 16×16×3=76816 \times 16 \times 3 = 768 values and becomes a CC-dimensional vector after a linear projection; SETR defaults to T-Large, with C=1024C = 1024. Add a position embedding learned separately for each position, and you get a 2,304×10242{,}304 \times 1024 sequence.

A Pure Transformer Encoder

What follows is a standard Transformer: each layer is multi-head self-attention plus an MLP, with LayerNorm placed before each sublayer and residual connections around both. The authors use two sizes:

NameLayersHidden dimHeads
T-Base1276812
T-Large24102416

These are really ViT-Base and ViT-Large, initialized directly from the weights of ViT (pretrained on ImageNet-21K) or DeiT (ImageNet-1K). The position embeddings differ in size from pretraining and are adjusted with 2D interpolation.

The attention in every layer sees all 2,304 patches; in our example, each attention head's matrix is 2,304×2,3042{,}304 \times 2{,}304, about 5.3 million values. From the very first layer, the receptive field is the whole image, which is exactly what the authors mean by "solving the limited receptive field problem of existing FCN encoder once and for all". The price is that the resolution can't get any finer; every SETR uses output stride 16, which the authors attribute to GPU memory limits, while the dilated FCN they compare against uses 8.

Three Decoders

The encoder outputs an HW256×C\frac{HW}{256} \times C sequence, which is first reshaped into an H16×W16×C\frac{H}{16} \times \frac{W}{16} \times C feature map and then handed to the decoder. The authors design three:

  1. Naïve. Two 1×11 \times 1 convolutions squeeze the channels straight down to the number of classes (19 for Cityscapes), followed by a single 16× bilinear upsampling.
  2. PUP (progressive upsampling). Upsampling by 16× in one go can produce noise, so convolution and 2× upsampling alternate instead, four times over. In our example, that's 48 → 96 → 192 → 384 → 768.
  3. MLA (multi-level feature aggregation). Similar in spirit to FPN, except that every Transformer layer has the same resolution, so there is no pyramid. The authors pick 4 evenly spaced layers out of the 24 (layers 6, 12, 18 and 24), each going through its own branch of three convolution layers; the branches are added element-wise from top to bottom, and finally concatenated and upsampled 4×.

SETR also adds auxiliary losses at several intermediate Transformer layers, for example layers 10, 15, 20 and 24 for PUP.

Discussion

Experimental Setup

All experiments are built on mmsegmentation, with its default data augmentation and training schedules: random scaling from 0.5 to 2×, random cropping (768 for Cityscapes, 512 for ADE20K, 480 for Pascal Context) and horizontal flipping. The optimizer is SGD with momentum 0.9 and weight decay set to 0. The authors stress that they use no common training tricks such as OHEM.

Which Decoder Is Better

On the Cityscapes validation set (single-scale testing, Table 2 of the paper):

ModelPretrainParams40k iter80k iter
Dilated FCN (R-101)1K68.6M73.9375.52
SETR-Naïve21K305.7M77.3777.90
SETR-MLA21K310.6M76.6577.24
SETR-PUP21K318.3M78.3979.34
SETR-PUP-DeiT (T-Base)1K97.6M78.7979.45
SETR-PUP (random init)—318.3M42.27—

On Cityscapes, PUP is the best. The authors guess that MLA loses out because the features of every Transformer layer share one resolution, with no coarse-to-fine pyramid like FPN's to exploit. On ADE20K, though, MLA edges out PUP, and both are far better than Naïve with its single 16× upsampling.

Original Figure 5: two Pascal Context images (an airplane and a cow) and visualizations of the output features of SETR's layers 1, 9, 17 and 24. The four feature maps have the same resolution and all clearly show the outlines of the airplane and the cow; they differ in the feature distribution the colors represent, which gets smoother in deeper layers.
Original Figure 5, Zheng et al., from PDF page 8, CC BY 4.0.

This figure helps explain why MLA is less useful than you might expect. The four layers have exactly the same resolution, and the object outlines are about equally sharp, unlike FPN, where the shallow levels can bring back details at higher resolution.

Pretraining Is Its Lifeline

Look again at the last row of the table above. The same SETR-PUP, with the Transformer randomly initialized and trained only on Cityscapes, gets just 42.27 mIoU, more than 30 points below FCN. For SETR, pretraining isn't a bonus; it's the line between usable and unusable.

Interestingly, the DeiT-pretrained T-Base (ImageNet-1K, 97.6 million parameters) does slightly better on Cityscapes than the ViT-pretrained T-Large (ImageNet-21K, 318 million parameters).

Could SETR's advantage simply come from more pretraining data? The authors ran a control for this (Table 3 of the paper), pretraining FCN's ResNet-101 on ImageNet-21K as well. FCN does improve, from 39.91 to 42.17 on ADE20K and from 73.93 to 76.38 on Cityscapes; but SETR gets 48.6 on ADE20K, still far ahead. From this the authors conclude that the advantage comes mainly from sequence-to-sequence modeling rather than from more pretraining data.

Against Other Methods

On the ADE20K validation set (Table 4 of the paper; multi-scale testing in parentheses):

MethodBackbonemIoU
DANetResNet-10145.30
OCRNetResNet-10145.70
DeepLabV3+ResNet-10146.40
SETR-PUPT-Large48.58 (50.09)
SETR-MLAT-Large48.64 (50.28)

With multi-scale testing, SETR-MLA reaches 50.28, nearly 4 points above DeepLabV3+. A model trained on the training plus validation sets took first place on the ADE20K test server leaderboard on the day of submission. The same holds on Pascal Context, where SETR-MLA gets 55.83 with multi-scale testing, against 54.70 for the previous best, APCNet.

Cityscapes is less impressive. On the validation set, SETR-PUP gets 82.15 with multi-scale testing, a bit above Axial-DeepLab's 81.50; on the test set it gets 81.08 with fine annotations only and 81.64 with coarse annotations added, about the same as DANet (81.50) and CCNet (81.90). The authors themselves call the Cityscapes results "competitive".

Original Figure 6: SETR's attention maps on five Pascal Context images, of an airplane, a dog, a bus, a horse and a person, with the bright regions concentrated on the main object in each image.
Original Figure 6, Zheng et al., from PDF page 8, CC BY 4.0.

The authors also include attention maps to show that SETR focuses its attention on semantically meaningful foreground regions.

The Price

SETR's biggest problem is one the paper barely mentions. The best SETR-PUP has 318 million parameters, more than four times the dilated FCN (69 million), and its output stride is still stuck at 16. SegFormer, half a year later, went after exactly this; on Cityscapes, SETR manages only 0.5 images per second, while SegFormer-B5 runs at 2.5 images per second with a higher mIoU.

Conclusion

SETR is among the first works to bring ViT into semantic segmentation unchanged. It proved that with all convolution and downsampling removed, a stack of global self-attention alone can still deliver top segmentation results, and it took the best scores of its time on ADE20K and Pascal Context.

It also exposed several problems of a pure ViT. A single resolution dulls the effect of multi-level feature aggregation; without pretraining it is barely usable; and 300 million parameters make it hard to deploy in practice. SegFormer, Segmenter and MaskFormer that followed each patched these problems from a different angle.

If you want to train a pure Transformer segmentation model yourself, make sure you have good pretrained weights first. SETR's experiments already showed that 300 million randomly initialized parameters earn only 42 points on Cityscapes.