Back to paper library
arXiv 2021ICCV 2021SegmentationPaper notes

Segmenter

One Representative per Class

Original paper

Segmenter: Transformer for Semantic Segmentation

Paper authors

Robin Strudel · Ricardo Garcia · Ivan Laptev · Cordelia Schmid

Publication
ICCV 2021Conference
First submitted to arXiv
arXiv:2105.05633
Cite this paper · BibTeX

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

@misc{segmentationsegmenter2021,
  title = {{Segmenter: Transformer for Semantic Segmentation}},
  author = {Robin Strudel and Ricardo Garcia and Ivan Laptev and Cordelia Schmid},
  year = {2021},
  eprint = {2105.05633},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2105.05633}
}
On this page

SETR had already shown that ViT, used unchanged as the encoder of a segmentation network, can work very well. But its decoder was still convolutional: progressive upsampling or multi-level aggregation, either way it was a pile of convolution layers bolted onto the Transformer.

Robin Strudel, Ricardo Garcia, Ivan Laptev and Cordelia Schmid of Inria and the École normale supérieure in Paris wanted to go one step further and build the decoder with a Transformer too. Their approach borrows DETR's queries: prepare a learnable vector for each class, pass it through the Transformer together with the image patches, and finally get each class's mask with a dot product.

The paper also runs a systematic round of ablations, measuring one by one how model size, patch size, regularization and data volume each affect ViT when it's used for segmentation.

Defining the Problem

The locality of convolution limits its ability to see global context, and segmentation often depends precisely on global context to tell what a small region is. DeepLab enlarges the receptive field with atrous convolution and pyramid pooling, while DANet and OCRNet add various attention modules. The authors argue that the heavy use of these specially designed layers as patches is itself a sign of the limits of convolutional architectures for segmentation.

A Transformer sees the entire input from the first layer and has no built-in preference for locality. But its computation is proportional to the square of the sequence length, so you can't make every pixel a token. Segmenter therefore follows ViT and cuts the image into patches.

What remains is the decoder. The encoder outputs one vector per patch; how do you turn that into one class per pixel? The simplest way is to attach a linear layer directly, projecting each patch vector into scores for KK classes and then upscaling to the original image. The authors use this as the baseline first, then propose a mask decoder built around a Transformer.

Solving the Problem

Architecture

Original Figure 2: Segmenter overview. The left half is the encoder: the input image is cut into patches, flattened and projected, given position embeddings and fed into the Transformer encoder, which outputs an encoding for each patch. The right half is the decoder: the patch encodings and three class embeddings (Tree, Sidewalk, Person) go together into the mask transformer, and the output patch and class vectors are combined with a dot product to give a mask for each class, which is then upsampled and passed through argmax to produce the final segmentation map.
Original Figure 2, Strudel et al., from PDF page 3, CC BY 4.0.

The encoder in the left half is just ViT. The image is cut into P×PP \times P patches, N=HW/P2N = HW / P^2 in all; each patch is flattened, linearly projected to DD dimensions, given a learnable position embedding and fed into LL Transformer layers. The authors use four sizes of ViT:

NameLayersDim DDHeadsParams
Seg-Ti1219236M
Seg-S12384622M
Seg-B127681286M
Seg-L24102416307M

There are three patch sizes, 8, 16 and 32, and models are named "size/patch", for example Seg-B/16.

Let's walk through the ADE20K setting: 512×512 training crops with Seg-B/16. Each side is cut into 32 pieces, so N=32×32=1,024N = 32 \times 32 = 1{,}024 patches, and the encoder outputs a 1,024×7681{,}024 \times 768 sequence.

Linear Decoder

The baseline is simple. A linear layer is applied to each patch's 768-dimensional vector, outputting scores for ADE20K's 150 classes to give 1,024×1501{,}024 \times 150; this is reshaped to 32×32×15032 \times 32 \times 150, bilinearly upscaled to 512×512×150512 \times 512 \times 150, and passed through a softmax over classes.

Mask Transformer

Segmenter's headline decoder adds one more set of vectors: KK learnable class embeddings, each DD-dimensional, randomly initialized and each permanently assigned to one class. ADE20K has 150 classes, so there are 150 of them.

These 150 class embeddings are joined to the 1,024 patch encodings to form 1,174 tokens, which go together into an MM-layer Transformer (the experiments use M=2M = 2, with the same dimension as the encoder). In these two layers, the class embeddings and the patches exchange information through self-attention: the class embeddings see which patches in this image look like them, and the patches see which classes are present.

At the output, the patch vectors (1,024×7681{,}024 \times 768) and the class vectors (150×768150 \times 768) are each L2-normalized, and then their dot products are taken:

Masks(zM′,c)=zM′c⊤\text{Masks}(z'_M, c) = z'_M c^\top

This gives 1,024×1501{,}024 \times 150, where each column is one class's mask and each row is one patch's scores for the 150 classes. The remaining steps are the same as in the linear version: reshape, upscale, softmax over classes, and finally one more LayerNorm. Because the softmax runs across classes, each pixel's 150 scores sum to 1, and the authors describe these masks as "softly exclusive" of each other.

The authors say the design was inspired by DETR, Max-DeepLab and SOLOv2. The biggest difference from the linear decoder is that the class vectors change with the input image. A linear layer's weights are fixed, the same set for every image; after two Transformer layers, the class embeddings have absorbed information from this image, so every image effectively gets its own set of dynamic filters.

The difference from Max-DeepLab is that Max-DeepLab is a hybrid of CNN and Transformer that processes pixels and classes in two separate streams; Segmenter is a pure Transformer that processes patches and classes together in one sequence. The authors also mention that replacing the class embeddings with object embeddings would let the same architecture do panoptic segmentation.

Discussion

Training Setup

There are two choices of pretrained ViT weights. The original ViT on ImageNet-21K used only random cropping; later, Steiner et al. (AugReg) retrained it with dropout, stochastic depth, Mixup and RandAugment, gaining about 2 points of ImageNet accuracy. The authors fine-tuned Seg-B/16 on ADE20K from both sets of weights and got 45.69 and 48.06 respectively, so from then on they use the AugReg version throughout.

Data augmentation follows the standard MMSegmentation pipeline, with crops of 512 for ADE20K, 480 for Pascal Context and 768 for Cityscapes. The optimizer is SGD with weight decay set to 0, and the loss is unweighted per-pixel cross-entropy.

Regularization

CNNs get regularization on the side from BatchNorm; Transformers usually use LayerNorm plus dropout. On Seg-S/16, the authors compared combinations of dropout and stochastic depth (randomly skipping whole blocks):

stochastic depth 00.10.2
dropout 045.0145.3745.10
dropout 0.142.0242.3041.14
dropout 0.236.4936.6335.67

(From Table 2 of the paper, ADE20K validation set.)

Stochastic depth of 0.1 helps a little, and dropout only hurts. Adding 0.2 dropout costs nearly 9 points. From then on, every model uses stochastic depth 0.1 and no dropout.

Model Size and Patch Size

Results on the ADE20K validation set (Table 3 of the paper, linear decoder, single scale; speed measured on a V100 at 512 resolution with the largest batch memory allows):

ModelPatchImages/smIoU
Seg-Ti/161639639.03
Seg-S/32321,03240.64
Seg-S/161619645.37
Seg-B/323251643.07
Seg-B/16169248.06
Seg-B/88749.54
Seg-L/16163350.71

As the model grows, the score rises steadily: doubling the token dimension (S → B) adds 2.69, and doubling the layers (B → L) adds 2.65. The largest, Seg-L/16, gets 50.71 decoding with just one linear layer.

Patch size matters as much as model size. Seg-B going from 32 to 16 gains 5 points; going on to 8 reaches 49.54, only 1.17 below Seg-L/16 with far fewer parameters. But patch 8 gives a sequence four times as long as patch 16, with sixteen times the attention computation, and speed falls from 92 images per second to 7. The authors say that, if the computation were affordable, ViT-L/8 might be the best model.

Original Figure 3: segmentation results for the same street scene with patch sizes 32, 16 and 8, plus the annotation. Each image enlarges the region of the two pedestrians on the left: at patch 32 the two people merge into one blob, at patch 16 their outlines separate, and at patch 8 even the thin lamppost in the background can be picked out.
Original Figure 3, Strudel et al., from PDF page 5, CC BY 4.0.

At patch 32, the two people on the left are predicted as one solid blob; at patch 16 the boundary is much clearer; the thin lamppost is caught only at patch 8.

Broken Down by Object Size

The authors break mIoU down by object size and compare it with the convolutional DeepLabv3+ (ResNeSt-101) (Table 4 of the paper):

ModelDecoderSmallMediumLargeOverall
DeepLabv3+UNet37.8550.8950.6746.47
Seg-B/16Linear39.5751.3253.2848.06
Seg-B/16Mask40.1652.6152.6648.48
Seg-B/8Linear41.4354.3552.8549.54
Seg-L/16Linear42.0854.6755.3950.71
Seg-L/16Mask42.0254.8357.0651.30

The biggest gap between Segmenter and DeepLab is on large objects, where Seg-L with the mask decoder leads by 6.39, which the authors credit to the Transformer's grasp of global context. On small and medium objects, Seg-B/16, with a similar parameter count, is about level with DeepLab.

The mask decoder is consistently better than the linear decoder: 1.6 more for Seg-B†/16 (DeiT), 1.1 more for Seg-B/32 and 0.6 more for Seg-L/16, with most of the gain coming from large objects. The authors also analyze the learned class embeddings and find that similar classes sit close together in the vector space.

Data Volume

ViT is known for needing a lot of data. The authors fine-tuned Seg-S/16 on only part of the ADE20K training set: 4,000 images give 38.31, 8,000 give 41.87, and the full 20,000 give 45.37. Below 8,000 images the drop is especially steep, so even for fine-tuning, a Transformer needs enough data.

Speed and Accuracy

Original Figure 4: scatter plot of mIoU against images per second (log scale) on the ADE20K validation set. Red marks Segmenter with patch 16 (Seg-S-Mask, Seg-B-Mask, Seg-L-Mask), and orange the patch-32 versions; also marked are Swin-T, Swin-B, Swin-L, SETR-L and three DeepLabV3+ models (ResNet-50, ResNet-101, ResNeSt-101). The red line lies entirely above and to the right of DeepLabV3+'s green line.
Original Figure 4, Strudel et al., from PDF page 6, CC BY 4.0.

The horizontal axis is images processed per second (log scale) and the vertical axis is mIoU; further up and to the right is better. The patch-16 Segmenter (red line) lies entirely above and to the right of DeepLabV3+ (green line). The authors single out Seg-B-Mask/16, which matches SETR-MLA at twice the speed and with fewer parameters; the patch-32 Seg-B-Mask/32 runs at 400 images per second, four times faster than the ResNet-50 DeepLab, with about the same score.

Against Other Methods

On the ADE20K validation set (Table 6 of the paper, multi-scale testing), the largest model, Seg-L-Mask/16, gets 53.63, 5.27 above DeepLabv3+ ResNeSt-200 (48.36), also better than SETR-MLA (50.28) and on par with Swin-L UperNet (53.50). Seg-B†-Mask/16 (DeiT), pretrained only on ImageNet-1K, still gets 50.08, beating every convolutional method.

On Pascal Context, Seg-L-Mask/16 gets 59.0, 2.8 above OCRNet (HRNet) and 3.2 above SETR-MLA.

Cityscapes is a different story. Seg-L-Mask/16 gets 81.3, below DeepLabv3+ ResNeSt-200's 82.7 and SETR-PUP's 82.2. The authors mention that Cityscapes' 768×768 input is so large that the mask decoder fits only one layer. Another possible reason (our guess) is that Cityscapes images are high-resolution and full of small, thin objects, and at patch 16 the model works at only 1/16 resolution, which puts it at a disadvantage here.

Conclusion

Segmenter replaces both ends of the segmentation network with Transformers. The encoder is ViT, and the decoder passes a set of class embeddings through two Transformer layers together with the patches, then computes each class's mask with a dot product. On ADE20K and Pascal Context it was the best method of its time, and its scores keep climbing with smaller patches or bigger models.

Its ablations are more practical than the method itself. If you want to use ViT for segmentation, this paper has already done the measuring for you. Use an enhanced pretrained version like AugReg, regularize with stochastic depth and not dropout, treat patch size as seriously as model size, and brace yourself when you have fewer than a few thousand images.

The structure of "one query per class, with query–pixel dot products giving masks" became "one query per region" in MaskFormer two months later, no longer tied to a class, so the same model could also do instance and panoptic segmentation. A few years after that, EoMT simply stuffed the queries straight into the last few layers of ViT, removing the separate decoder altogether.