# Segmenter: Transformer for Semantic Segmentation

Segmenter uses a transformer to label every image patch

Robin Strudel, Ricardo Garcia, Ivan Laptev, Cordelia Schmid · arXiv · 2021-5-12

- **Problem** Convolutional segmentation models see global context only indirectly, so their local filters bias them toward local interactions.
- **Method** Segmenter splits an image into patches, encodes them with ViT, and decodes them with a linear layer or mask transformer.
- **Results** Seg-L-Mask/16 reaches 53.63% mIoU on ADE20K with multi-scale inference, and 81.3% mIoU on Cityscapes.

## Why convolutional segmenters miss global context

Segmentation assigns a class to every pixel, and the label of a small patch often depends on the wider scene, so a model needs information beyond its immediate neighbourhood. The dominant designs are fully convolutional encoder-decoders, where the encoder produces low-resolution features and the decoder upsamples them into per-pixel class scores.

Stacked local filters reach global information only indirectly, which is a weakness here because labels of local patches depend on the global context.

Dilated convolutions, spatial pyramid pooling and attention modules placed on top of CNN backbones were added to widen the receptive field, but these models still rely on convolutional backbones and so remain biased toward local interactions. The need for many specialised layers to counter this bias is read as a sign that convolutional architectures have limits for segmentation.

Segmenter instead treats segmentation as a sequence-to-sequence problem, using a transformer to bring context into every stage of the model. Global attention scales quadratically with the number of elements, so applying it to raw pixels is prohibitively expensive; the design therefore splits the image into patches and feeds the linear patch embeddings to the transformer as tokens, following ViT.

## Encoding image patches as tokens

An image of height H, width W and C channels is cut into N = HW/P² square patches of side P, so the patch tensor has shape N×P²×C. Each flattened patch is multiplied by a learnable matrix E to give a patch embedding of dimension D, and a learnable position embedding is added to each one, which yields the input tokens z₀. Segmenter contains no convolutional layers, so global context enters through self-attention at every layer.

Each encoder layer applies layer norm before a multi-headed self-attention block and then before a two-layer point-wise MLP, with a residual connection after each block. The residual updates are a\_{i-1} = MSA(LN(z\_{i-1})) + z\_{i-1} and z\_i = MLP(LN(a\_{i-1})) + a\_{i-1}, for i from 1 to L. Inside the attention block, queries Q, keys K and values V of size N×d are combined as \\\[ \\mathrm{MSA}(Q,K,V)=\\mathrm{softmax}\\left(\\frac{QK^{T}}{\\sqrt{d}}\\right)V, \\\] so every patch can weigh every other patch.

The encoder output z\_L is a contextualised sequence of N encodings of dimension D, which is the input to the decoder.

## Linear and mask transformer decoders

The linear decoder applies one point-wise linear layer to each patch encoding, giving patch-level class logits of size N×K. These logits are reshaped to a grid of size H/P by W/P by K, bilinearly upsampled to H by W, and a softmax over the class dimension gives the segmentation map.

The mask transformer adds K learnable class embeddings, randomly initialised, one per semantic class. Patch encodings and class embeddings are passed together through a transformer encoder with M layers, so each class embedding can attend to the image patches and the patches can attend to the classes. The mask for class k is the scalar product between the L2-normalised patch embeddings z′\_M and the class embedding c\_k, so the set of masks is Masks(z′\_M, c) = z′\_M c^T with size N×K.

Each mask column is reshaped to H/P by W/P, bilinearly upsampled to the image size, and a softmax over classes is followed by a layer norm to give pixel-wise scores. The resulting masks are softly exclusive, so across classes the scores at each pixel sum to 1. The design draws on DETR, MaxDeepLab and SOLO-v2, which use object embeddings to produce masks.

Unlike MaxDeepLab, which splits pixel and class embeddings into two streams because of compute limits, Segmenter processes patch and class embeddings jointly in one pure transformer, so the filters are formed from the input.

## Pre-training and fine-tuning recipe

The encoder variants Tiny, Small, Base and Large follow ViT, and DeiT is used as a further encoder; the number of layers and the token size differ across variants. The head size is fixed at 64, the number of heads equals the token size divided by 64, and the MLP hidden size is four times the token size. Patch sizes of 8×8, 16×16 and 32×32 are used, and models are named by variant and patch size, such as Seg-B/16.

The authors start from ViT weights pre-trained on ImageNet-21k with the regularisation and augmentation recipe of \[47\], which adds dropout, stochastic depth, Mixup and RandAugment to what the original ViT used. On ADE20K, fine-tuning ViT-B/16 from the original ViT weights gives 45.69% mIoU and from the improved weights gives 48.06%, so the improved initialisation is used for all Segmenter models.

Pre-trained position embeddings are bilinearly interpolated to the sequence length of the fine-tuning resolution, and the decoders start from random weights drawn from a truncated normal distribution.

Fine-tuning uses pixel-wise cross-entropy without class reweighting and stochastic gradient descent with weight decay set to 0. The learning rate follows a poly decay γ = γ₀(1 − N\_iter/N\_total)^0.9, where N\_iter is the current iteration and N\_total the total number of iterations. The base rate γ₀ is 10⁻³ with 160K iterations and batch size 8 for ADE20K, 10⁻³ with 80K iterations and batch size 16 for Pascal Context, and 10⁻² with 80K iterations and batch size 8 for Cityscapes; these values are set per dataset and are not searched over in the text.

Training images are randomly resized, flipped left-right and cropped or padded to 512×512 for ADE20K, 480×480 for Pascal Context and 768×768 for Cityscapes, and the largest ADE20K model is trained at 640×640.

## Regularization and model size

Dropout and stochastic depth were compared on Seg-S/16 with ViT-S/16 backbone on the ADE20K validation set, over a grid of rates from 0.0 to 0.2. Stochastic depth at 0.1 with dropout at 0.0 gives 45.37 mIoU, against 45.01 with no regularization, a difference of 0.36 points. Every dropout setting scores below the setting with the same stochastic depth rate and no dropout, so the authors train all later models with stochastic depth 0.1 and without dropout.

With the patch size fixed at 16, the Tiny to Large variants show that capacity helps: doubling the token dimension from Seg-S/16 to Seg-B/16 raises mIoU by 2.69 points, from 45.37 to 48.06. Doubling the number of layers from Seg-B/16 to Seg-L/16 raises mIoU by 2.65 points, reaching 50.71 with single-scale inference.

## Patch size as a speed and accuracy trade-off

Seg-B/8 reaches 49.54 mIoU, at 7 images per second, against 92 images per second for Seg-B/16 at 48.06 mIoU. Throughput was measured on a V100 GPU at image resolution 512, with the batch size set to the largest that fits in memory for each model.

Reducing the patch size gives a robust gain without new parameters, but attention runs over longer sequences, so compute time and memory grow. The authors expect ViT-L/8 to be the best model if its compute were feasible, but no ViT-L/8 result is reported. Seg-B/8 narrows the gap to ViT-L/16 to 1.17 points while using substantially fewer parameters.

Qualitatively, a patch size of 32 yields a coarse segmentation with poor boundaries, and at patch size 8 the sharper contours and thin street poles become visible.

## Mask transformer gains and object size

On ADE20K with single-scale inference, the mask transformer raises Seg-B†/16 from 47.10 to 48.70, a gain of 1.6%, the largest decoder gain in the study. The gain is 1.1% for Seg-B-Mask/32 and 0.6% for Seg-L-Mask/16 against Seg-L/16, in the setting where the mask transformer has 2 layers.

Grouped by object size, Seg-B/8 improves on Seg-B/16 by 1.27 points on small objects and 1.74 points on medium objects. Seg-L-Mask/16 reaches 57.06 on large objects, 6.39 points above DeepLabv3+ with a ResNeSt-101 backbone at 50.67. The authors read the larger gains on large objects as evidence for the dynamic filters created by the mask transformer.

## Dependence on training set size

Seg-S/16 was trained on subsets of ADE20K of 4k, 8k, 12k, 16k and 20k images, scoring 38.31, 41.87, 43.42, 44.61 and 45.37 mIoU with single-scale inference. Performance drops noticeably when the training set falls below 8k images, which the authors read as transformers needing a sufficient amount of data even during fine-tuning.

## Comparison with state of the art

On ADE20K with multi-scale inference, Seg-B†/16 reaches 48.05 and Seg-L-Mask/16 reaches 53.63%, and the Seg-B†/16 result is reported as matching DeepLabv3+ ResNeSt-200 at 48.36. Adding the mask transformer raises Seg-B†/16 to 50.08 mIoU, and the best model is reported as outperforming DeepLabv3+ ResNeSt-200 by 5.27 points, as well as SETR and Swin-L UperNet.

On Pascal Context with multi-scale inference, Seg-L/16 reaches 56.5 and Seg-L-Mask/16 reaches 59.04% mIoU, an improvement of 2.5% over its linear decoder. The authors report 2.8% above OCR with HRNetV2-W48 and 3.2% above SETR-L MLA on the same benchmark.

On the Cityscapes validation set, Seg-L-Mask/16 reaches 81.3% mIoU with multi-scale inference, but it uses a mask transformer with one decoder layer, since two layers did not fit in memory at 768×768. Several listed methods score higher on Cityscapes, including SETR-L PUP at 82.2 and DeepLabv3+ ResNeSt-200 at 82.7, so Segmenter is described as competitive there rather than best.

In qualitative comparison with DeepLabv3+ ResNeSt-101, the authors see sharper object boundaries from DeepLabv3+ and more consistent labels on large instances from Segmenter.

## Limitations

The main cost is that global attention grows quadratically with the number of tokens, which is why small patches raise compute and memory use. The largest single-scale ablation models are measured only at the stated input size, and ViT-L/8 was not trained.

Dataset size matters: below 8k ADE20K images, Seg-S/16 drops clearly, so the approach needs a sufficient amount of segmentation data. Ablation tables use single-scale inference, while the headline ADE20K and Pascal Context numbers use multi-scale inference, so the two sets of scores should not be compared directly. The Cityscapes result uses a single decoder layer for Seg-L-Mask/16 because of memory limits at 768×768, which is a constraint on that comparison.

## Paper info

- **Authors**: Robin Strudel, Ricardo Garcia, Ivan Laptev, Cordelia Schmid
- **Venue**: arXiv · Preprint
- **First published**: 2021-5-12
- **Added**: 2026-11-12
- **Updated**: 2021-9-2
- **Version**: v3
- **arXiv**: 2105.05633
- **Category**: cs.CV · cs.AI · cs.LG
- **Original paper terms**: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/2105.05633v3) · [PDF ↗](https://arxiv.org/pdf/2105.05633v3)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Robin Strudel, Ricardo Garcia, Ivan Laptev, Cordelia Schmid (2021). Segmenter: Transformer for Semantic Segmentation. arXiv:2105.05633v3. https://arxiv.org/abs/2105.05633v3

**BibTeX**

```
@misc{strudel2021segmenter,
  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.05633v3}
}
```

## Other paper commentary

- [MultiResUNet : Rethinking the U-Net Architecture for Multimodal Biomedical Image Segmentation](https://refivu.com/en/papers/e99144f0-6aca-4598-b66e-67a4575f7fe4-multiresunet-rethinking-the-u-net-architecture-for.md)
- [MLP-Mixer: An all-MLP Architecture for Vision](https://refivu.com/en/papers/bf0a9095-e638-4ae5-bab6-5abf2b479763-mlp-mixer-an-all-mlp-architecture-for-vision.md)
- [SemanticKITTI: A Dataset for Semantic Scene Understanding of LiDAR Sequences](https://refivu.com/en/papers/304a0de8-6214-4d64-8649-0176464fb2d4-semantickitti-a-dataset-for-semantic-scene-understanding-of.md)
- [Real-world Anomaly Detection in Surveillance Videos](https://refivu.com/en/papers/dd2c7247-4d8f-4a7a-8400-f4e9c8e74dca-real-world-anomaly-detection-in-surveillance-videos.md)
- [RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds](https://refivu.com/en/papers/5d37c138-f524-49a6-ba5e-e9ff9f584cdd-randla-net-efficient-semantic-segmentation-of-large-scale.md)
- [Pre-Trained Image Processing Transformer](https://refivu.com/en/papers/8475de2d-12d6-41b5-8348-16c772598a34-pre-trained-image-processing-transformer.md)
- [Multimodal Transformer for Unaligned Multimodal Language Sequences](https://refivu.com/en/papers/022f0bed-e82b-408d-a49e-8805e4f212e5-multimodal-transformer-for-unaligned-multimodal-language.md)
- [Obfuscated Gradients Give a False Sense of Security: Circumventing Defenses to Adversarial Examples](https://refivu.com/en/papers/6d5ee375-978f-4c78-87e2-0c8e55a1127f-obfuscated-gradients-give-a-false-sense-of-security.md)
- [Practical recommendations for gradient-based training of deep architectures](https://refivu.com/en/papers/0f41024c-5358-4bc2-be1e-7190db5f4d0c-practical-recommendations-for-gradient-based-training-of.md)

## Terms in this paper

- [dropout](https://refivu.com/en/terms/db73f75bbdd3-dropout)
- [Semantic segmentation](https://refivu.com/en/terms/00dfce7ec404-semantic-segmentation)
- [self-attention](https://refivu.com/en/terms/426589b81077-self-attention)
- [mIoU](https://refivu.com/en/terms/901dcc5384c2-miou)
- [Cityscapes](https://refivu.com/en/terms/1067bb006500-cityscapes)
- [cross-entropy loss](https://refivu.com/en/terms/14067bdf8b9f-cross-entropy-loss)
- [ADE20K](https://refivu.com/en/terms/5adea765479c-ade20k)
- [Stochastic Gradient Descent (SGD)](https://refivu.com/en/terms/5c2447143b09-stochastic-gradient-descent-sgd)
- [Vision Transformer (ViT)](https://refivu.com/en/terms/5676f2cc1c70-vision-transformer-vit)
- [DeiT](https://refivu.com/en/terms/24b9f164881b-deit)
- [DETR](https://refivu.com/en/terms/86857f2f379d-detr)
- [Swin Transformer](https://refivu.com/en/terms/957ac355698c-swin-transformer)
- [transformer encoder](https://refivu.com/en/terms/b8e4656f9228-transformer-encoder)
- [stochastic depth](https://refivu.com/en/terms/d05be8dc71c1-stochastic-depth)
- [Pascal Context](https://refivu.com/en/terms/aa3abacfe722-pascal-context)
- [patch embedding](https://refivu.com/en/terms/29de7fe43e42-patch-embedding)
- [DeepLabv3+](https://refivu.com/en/terms/26afd1971cb0-deeplabv3)
- [SETR](https://refivu.com/en/terms/6b0498f25609-setr)
- [Segmenter](https://refivu.com/en/terms/48d22c7db3c3-segmenter)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/f33fa10d-9d05-48d0-9506-83f7da0f5f1e-segmenter-transformer-for-semantic-segmentation)
