DPT Transformer for Dense Depth
Convolutions see locally; depth needs the whole room. DPT reads the image with a vision transformer so far walls inform near chairs.
Why Does This Exist?
CNN depth backbones grow context slowly through stacked kernels, so thin structures and far layout stay ambiguous. DPT exists to give dense prediction a global view from the start: a vision-transformer encoder attends across the whole image at every layer, then a lightweight convolutional decoder reassembles tokens from several scales into a sharp high-resolution map. It powers MiDaS and the Depth Anything family.
Basics are monocular depth estimation; the mixed-dataset teacher is MiDaS.
Think of It Like This
A survey team with radios
A CNN crew passes notes only to neighbors, so news crosses the field slowly. A DPT crew carries radios: every member hears the far corner instantly, then draws the local map with full context.
Attention is the radio and the decoder is the local drawing. The analogy stops at cost: radios need batteries while global attention needs quadratic memory, tamed here by patch tokens.
How It Actually Works
The image splits into patches embedded as tokens with position codes. Transformer blocks apply globally, so each token reads all others. Tokens from four stages reassemble into image-like maps at to scale, then RefineNet-style fusion blocks upsample and merge coarse-to-fine into depth at half input resolution. The same head swaps to segmentation or normals by changing the loss, which is why DPT is a dense-task backbone rather than a depth-only trick.
Worked numbers
A image gives tokens. Attention over tokens costs pairwise scores per head, trivial next to per-pixel CNN stacks. On the demo edge split, boundary F1 rises from CNN to DPT, about a percent relative gain.
Code
def token_count(size: int, patch: int) -> int: return (size // patch) ** 2 # patches become transformer tokens
print(token_count(384, 16)) # -> 576Watch Out For
Running DPT at full megapixels naively
Token count grows with the square of resolution, so px images explode memory sixteenfold over px. The symptom is sudden out-of-memory on phone photos. Tile with overlap or cap the long side near px, then blend.
The Quick Version
- DPT encodes patches with a vision transformer for global context.
- A convolutional decoder fuses four scales into sharp depth.
- Global attention fixes thin structures that local CNNs smear.
- Token count is ; watch memory at high resolution.
- The same backbone serves depth, segmentation, and normals.
- MiDaS and Depth Anything build directly on DPT.