# Vision Transformers for Dense Prediction

Dense vision transformers replace convolutional backbones for dense prediction tasks

René Ranftl, Alexey Bochkovskiy, Vladlen Koltun · arXiv · 2021-3-24

- **Problem** Convolutional backbones shrink the feature grid with depth, so fine resolution and detail are lost before the decoder can recover them. Remedies only soften this, because they still rest on the convolution.
- **Method** DPT uses a vision transformer as the encoder, which keeps a constant resolution and a global receptive field at every stage. It reassembles tokens from four layers into multi-resolution maps and fuses them with a convolutional decoder.
- **Results** DPT-Hybrid reaches 49.02 mIoU on ADE20K, above the 48.36 mIoU of DeeplabV3 with ResNeSt-200.

## Why convolutional backbones limit dense prediction

Most dense prediction networks pair a convolutional backbone with a decoder, and the backbone is usually an image classifier pretrained on ImageNet. The decoder can only work with what the encoder keeps, so the backbone sets a ceiling on the whole model. Convolutional backbones downsample the image to build features at several scales. This widens the receptive field and keeps memory and compute within reach.

The cost is that resolution and granularity fall off in the deeper stages, and the decoder then has to recover detail that is hard to bring back. Remedies such as higher input resolution, dilated convolutions, skip connections from several encoder stages, and parallel multi-resolution branches soften the problem. All of these still rest on the convolution, which is a linear operator with a limited receptive field, so depth must be stacked to see wide context and downsampling follows.

## From image patches to tokens

DPT keeps the encoder-decoder layout and uses a transformer as the basic computational block of the encoder, specifically the vision transformer, ViT. ViT cuts the image into non-overlapping square patches of side p, flattens each one and embeds it linearly. Each embedded patch is one token. The patch size is 16 for all experiments.

Each transformer layer keeps the number of tokens unchanged, so the encoder holds the spatial grid of the initial embedding through all stages. Self-attention is global, since any token can influence any other token. From the embedding onward, every stage therefore has a global receptive field. Because the transformer treats tokens as a set, the embedding adds a learnable position embedding.

A readout token that is not tied to any patch is also added as a global image representation. The embedding width is 768 for ViT-Base and 1024 for ViT-Large, which is larger than the number of pixels in a patch, so the embedding can keep information the task needs. In the hybrid variant, a ResNet50 produces the image embedding, so tokens sit at a finer resolution than the lowest-resolution features that convolutional backbones usually expose.

## Reassembling tokens into feature maps

The decoder starts with a three-step Reassemble operation that takes tokens from an arbitrary set of encoder layers and produces an image-like map at a chosen size ratio s. The first step, Read, maps the Np+1 tokens to Np tokens so that they can be placed on a grid, and it has to decide what happens to the readout token. Three options were compared: ignoring the readout token, adding it to every patch token, or concatenating it to each patch token and projecting the result back to D with a linear layer followed by GELU.

The projection option is the default, and the decoder then produces feature maps with 256 dimensions.

The second step, Concatenate, places each token at the position of its source patch, which gives a map of size H/p by W/p with D channels. The third step, Resample, first projects the channels to D̂ with a 1×1 convolution. It then uses a strided 3×3 convolution when s is at least p, and a strided 3×3 transpose convolution when s is less than p.

Four layers are tapped at four resolutions, with deeper layers placed at lower resolution. ViT-Large uses layers 5, 12, 18 and 24, while ViT-Base uses layers 3, 6, 9 and 12. The hybrid model takes features from the first and second ResNet blocks of the embedding network, together with stages 9 and 12 of the transformer.

## Fusing stages and handling image size

Consecutive reassembled maps are merged in fusion blocks based on RefineNet, which uses residual convolutional units. Each fusion stage doubles the resolution, so the final representation sits at half the input resolution, and a task-specific head produces the output. For depth the head uses 3 convolutional layers, and batch normalization is turned off in the decoder because it hurt regression results.

For segmentation the head predicts at half resolution and bilinear interpolation restores the full size.

The encoder accepts any image size that is divisible by p, and the number of tokens then changes with the image. The position embeddings are interpolated to the new grid for each image, and the convolutional decoder requires inputs aligned to its stride of 32 pixels.

## Monocular depth on the 1.4 million image meta-dataset

Training follows the protocol of Ranftl et al., with a scale- and shift-invariant trimmed loss on inverse depth and a gradient-matching loss. The training data is MIX 6, which extends the MIX 5 datasets with five more and contains about 1.4 million images. The encoder starts from ImageNet weights and is trained with Adam at a learning rate of 1e-5, while the decoder starts randomly and uses a learning rate of 1e-4.

Images are resized to a longer side of 384 pixels and trained on 384 by 384 crops for 60 epochs of 72,000 steps at batch size 16, after a pretraining stage on a curated subset.

The evaluation is zero-shot, so the test datasets are not seen in training, and relative numbers are computed against the original MiDaS model. On ETH3D, the AbsRel of DPT-Large is 0.089, against 0.116 for MiDaS retrained on MIX 6, and lower values are better. Averaged over the test datasets, the relative improvement over MiDaS is more than 23% for DPT-Hybrid and 28% for DPT-Large.

To separate the architecture from the larger training set, the authors retrained the MiDaS network on MIX 6. That network gains from the larger set, but both DPT variants still outperform it in the same zero-shot evaluation. The authors read this as evidence that DPT benefits more from more training data than the convolutional network does.

## Fine-tuning on NYUv2 and KITTI

Because the meta-dataset model is trained with an affine-invariant loss, its outputs can have arbitrary scale and shift, and direct fine-tuning would be dominated by that mismatch. Each training sample is first aligned to its ground truth, the resulting scales and shifts are averaged over the training set, and the average is applied before the loss.

The Eigen loss is used, and the gradient-matching loss is disabled for KITTI because the ground truth there is sparse.

On NYUv2, DPT-Hybrid reaches a δ\>1.25 of 0.904, against 0.885 for BTS, with an AbsRel of 0.110 for both and an RMSE of 0.357 against 0.392 for BTS. On KITTI (Eigen split), DPT-Hybrid reaches a δ\>1.25 of 0.959, against 0.956 for BTS, and an RMSE of 2.573 against 2.756. BTS has the lower AbsRel on KITTI, 0.059 against 0.062 for DPT-Hybrid, so the authors' statement that the architecture matches or improves on all metrics holds only for the other metrics in that table.

## Semantic segmentation on ADE20K and Pascal Context

The segmentation protocol follows Zhang et al., with a cross-entropy loss, an auxiliary head with loss weight 0.2 on the penultimate fusion layer, and dropout of 0.1 before the classifiers. Training uses SGD with momentum 0.9, a polynomial schedule with decay 0.9, batch size 48, a learning rate of 0.002 and 240 epochs on ADE20K, with multi-scale inference at test time.

The authors attribute the lower DPT-Large score to the much smaller ADE20K training set compared with their earlier depth experiments. Qualitatively, the authors observe cleaner and finer object boundaries and somewhat less clutter in the DPT predictions.

For Pascal Context, DPT-Hybrid is fine-tuned for 50 epochs with the other hyper-parameters unchanged.

## Which design choices matter

The ablations use monocular depth with a reduced meta-dataset of three datasets, about 41,000 images, and a small validation split of about 1,000 images. Results are relative absolute deviation after affine alignment, and ViT-Base is the default backbone unless stated otherwise.

For the readout token, ignoring it gives a mean of 0.0822, adding it gives 0.0831 and projecting it gives 0.0819, so projection is the best mean of the three. Adding the token is worse than ignoring it, and the authors chose projection for all later experiments. For skip connections on ViT-Base, tapping layers 3, 6, 9 and 12 gives a mean of 0.0822, which is the lowest of the three base settings, while the hybrid model benefits from tapping the low-level ResNet stages R0 and R1.

ViT-Large has a mean error of 0.0778, below the 0.0819 of ViT-Base and the 0.0783 of ViT-Hybrid. ViT-Base is comparable to ResNext101-WSL, which had a billion-scale weakly supervised pretraining, while ViT-Hybrid and ViT-Large improve on it with less pretraining data. DeIT-Base-Dist, which has the same architecture as ViT-Base but a distillation token, has a mean of 0.0796 against 0.0819 for ViT-Base, which suggests that better classification pretraining also helps dense prediction.

## Behaviour at inference resolutions above training

Fully-convolutional networks have large effective receptive fields only in their deepest layers, so their early layers stay local and performance drops when inference resolution differs from training. The authors test inference at resolutions from 416 to 640 pixels, above the 384 by 384 training resolution, and plot the relative performance decrease against the training-resolution result.

The DPT variants degrade more gracefully as resolution increases than the convolutional baselines do in these measurements. The authors conjecture that the global receptive field at every transformer layer is the reason, but this link was not isolated in a separate experiment.

## Parameter counts and inference time

Timings were measured on an Intel Xeon Platinum 8280 CPU and an Nvidia RTX 2080 GPU, with 384-pixel square inputs averaged over 400 runs. Inference times are 32 ms for MiDaS, 17 ms for DPT-Base, 38 ms for DPT-Hybrid and 35 ms for DPT-Large, so the largest model is about as fast as the convolutional baseline on this hardware.

## Limitations

The study covers two dense tasks, monocular depth and semantic segmentation. The authors state that vision transformers realize their potential when paired with a sufficient amount of training data, and the largest depth gains are reported with the 1.4 million image MIX 6 set. The ablations run on a reduced three-dataset setup with about 41,000 images and ViT-Base as the default, so they are not the same comparison as the full depth results.

The inference-resolution test covers resolutions from 416 to 640 pixels, and the link to the global receptive field is a conjecture. Timings come from one CPU and one GPU and are not tied to the accuracy experiments.

## Paper info

- **Authors**: René Ranftl, Alexey Bochkovskiy, Vladlen Koltun
- **Venue**: arXiv · Preprint
- **First published**: 2021-3-24
- **Added**: 2026-11-12
- **Updated**: 2021-3-24
- **Version**: v1
- **arXiv**: 2103.13413
- **Category**: cs.CV
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

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

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

René Ranftl, Alexey Bochkovskiy, Vladlen Koltun (2021). Vision Transformers for Dense Prediction. arXiv:2103.13413v1. https://arxiv.org/abs/2103.13413v1

**BibTeX**

```
@misc{ranftl2021vision,
  title = {{Vision Transformers for Dense Prediction}},
  author = {René Ranftl and Alexey Bochkovskiy and Vladlen Koltun},
  year = {2021},
  eprint = {2103.13413},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2103.13413v1}
}
```

## Other paper commentary

- [CyCADA: Cycle-Consistent Adversarial Domain Adaptation](https://refivu.com/en/papers/86c1af43-2e3a-459f-87e6-d7c0653694a6-cycada-cycle-consistent-adversarial-domain-adaptation.md)
- [The Open Images Dataset V4: Unified image classification, object detection, and visual relationship detection at scale](https://refivu.com/en/papers/a257d9ea-aed6-4a9a-9d02-5e4c50a42a90-the-open-images-dataset-v4-unified-image-classification.md)
- [Generative Image Inpainting with Contextual Attention](https://refivu.com/en/papers/aad66e29-0d9a-49ff-bbf1-91b1b036c9eb-generative-image-inpainting-with-contextual-attention.md)
- [Multimodal Unsupervised Image-to-Image Translation](https://refivu.com/en/papers/f66e468d-e3a2-4e1b-8490-2978cf3d55ff-multimodal-unsupervised-image-to-image-translation.md)
- [ViLBERT: Pretraining Task-Agnostic Visiolinguistic Representations for Vision-and-Language Tasks](https://refivu.com/en/papers/b77fbb3e-836f-44e3-bbac-9ff51edf4034-vilbert-pretraining-task-agnostic-visiolinguistic.md)
- [Attention Mechanisms in Computer Vision: A Survey](https://refivu.com/en/papers/d03d817b-607b-4760-8cb5-139501ae5964-attention-mechanisms-in-computer-vision-a-survey.md)
- [All-Optical Machine Learning Using Diffractive Deep Neural Networks](https://refivu.com/en/papers/818615cb-8c22-447c-a0a6-77954575b98a-all-optical-machine-learning-using-diffractive-deep-neural.md)
- [Beyond Part Models: Person Retrieval with Refined Part Pooling (and a Strong Convolutional Baseline)](https://refivu.com/en/papers/0d7a16fd-c14e-49e6-938f-2cdeb51ea5a4-beyond-part-models-person-retrieval-with-refined-part.md)
- [Digging Into Self-Supervised Monocular Depth Estimation](https://refivu.com/en/papers/501b9eb4-7fd4-43f0-8b68-eaaf0aab3b71-digging-into-self-supervised-monocular-depth-estimation.md)
- [Language Models (Mostly) Know What They Know](https://refivu.com/en/papers/ffe245df-5662-4bfb-8c0b-899a38b87412-language-models-mostly-know-what-they-know.md)
- [From Local to Global: A Graph RAG Approach to Query-Focused Summarization](https://refivu.com/en/papers/b90240e0-a6fe-4297-adb6-4ebd769e9c60-from-local-to-global-a-graph-rag-approach.md)

## Terms in this paper

- [Transformer](https://refivu.com/en/terms/48e8be2570bc-transformer)
- [Semantic segmentation](https://refivu.com/en/terms/00dfce7ec404-semantic-segmentation)
- [mIoU](https://refivu.com/en/terms/901dcc5384c2-miou)
- [KITTI](https://refivu.com/en/terms/300e3e1e2f51-kitti)
- [ADE20K](https://refivu.com/en/terms/5adea765479c-ade20k)
- [pixel accuracy](https://refivu.com/en/terms/623755cb8953-pixel-accuracy)
- [ViT](https://refivu.com/en/terms/c041b72c53b8-vit)
- [Monocular depth estimation](https://refivu.com/en/terms/8a2cee5cd76c-monocular-depth-estimation)
- [Pascal Context](https://refivu.com/en/terms/aa3abacfe722-pascal-context)
- [AbsRel](https://refivu.com/en/terms/ae522058aceb-absrel)
- [NYUv2](https://refivu.com/en/terms/f932a8234b75-nyuv2)
- [DPT](https://refivu.com/en/terms/d3a0db7af349-dpt)
- [Multi-Headed Self-Attention](https://refivu.com/en/terms/33e3c00dcaee-multi-headed-self-attention)
- [ViT-Base](https://refivu.com/en/terms/c3d1c551d5a3-vit-base)
- [zero-shot cross-dataset transfer](https://refivu.com/en/terms/9ea40a80195d-zero-shot-cross-dataset-transfer)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/9b71a7ee-e2ab-43e8-a6e8-d9dd244b9be7-vision-transformers-for-dense-prediction)
