Depth from one photo: DPT, MiDaS and relative vs metric depth
How a network guesses distance from a single image, why most depth models only get it up to an unknown scale and shift, and how DPT and MiDaS made dense depth work across many datasets.
What runs in CodeSplash
- VIS016Dense Prediction Transformer (DPT) Explained here
- VIS017MiDaS mixed-dataset monocular depth learning Explained here
You will be able to
- Explain why one image can't fix absolute distance
- Describe how DPT turns ViT tokens into a dense map
- Fit the scale and shift that align relative depth to measurements
- Report aligned and metric depth errors separately
Why one photo isn't enough
ReadA toy car near the lens and a real car far away can fill the same pixels. From a single image, distance is ambiguous; a network can only learn cues: familiar sizes, perspective lines, texture getting finer, objects hiding others, haze.
Those cues fix the order and shape of a scene much better than its size. That's why most monocular depthMonocular depth estimationPredicting how far each pixel is from one photo. Most models give relative depth: order and shape, not meters. models predict relative depth (often inverse depth): correct up to an unknown scale and shift. Getting meters takes extra information, such as a known distance or the camera's height.
DPT: dense maps from a Transformer
ReadDPT puts a vision transformerVision transformer (ViT)Cuts an image into fixed-size patches, turns each into a token with a position embedding, and runs a Transformer on them. behind a dense-prediction head. It takes the patch tokens from several encoder layers, reassembles each set into an image-shaped grid at its own resolution, and fuses them from coarse to fine with convolutional blocks, much like a feature pyramidFeature pyramid networkAdds a top-down path that sends coarse, meaningful features back to finer maps, so objects of every size are found..
In the program, four tokens are projected to one value each and laid out as a 2 × 2 grid, 0.130 to 1.03; adding an upsampled coarse value gives the fused grid, 0.530 to 1.43. Because every token sees the whole image, the map stays globally consistent.
MiDaS: train across datasets
ReadDepth datasets disagree: some measure meters with a laser, some give relative depth from stereo films, some inverse depth. MiDaS trains on all of them with a scale- and shift-invariant lossScale- and shift-invariant lossA depth loss computed after fitting the best scale and shift by least squares, so datasets with different units can be mixed.: for each image, fit the scale and shift that best map the prediction onto the target by least squaresLeast-squares fitThe fit that makes the sum of squared differences smallest; geometrically, it projects the target onto what the model can express., then score what's left.
The program aligns five predicted depths with targets, one marked invalid. The best scale is 2 and the shift 1, after which the valid error is zero to machine precision. A prediction multiplied by 3 and shifted by −2 aligns to exactly the same result.
Fit the scale and shift yourself
ExploreAligning relative depth is a least-squares line: predicted depth on one axis, measured depth on the other. Drag the line and watch the squared errors.
DExplorable · Fit the line yourself
What makes the least-squares line 'best'?
Add one point far below the trend at high x. What happens to the least-squares slope?
Commit your prediction above to see the result
Make your prediction to see the result.
What the loss can't see
PredictPredict first
A model's depths are all exactly half the true depth, minus one meter. What is its scale- and shift-invariant error?
Reveal
Zero. A scale and a shift absorb the mistake completely. That's the point for training across datasets, and the danger in reporting: an aligned error says nothing about getting meters right.
Reporting depth honestly
ReadReport two numbers: the error after aligning scale and shift per image (shape), and the error with one fixed calibration for all images, or none (meters). Say which pixels were valid, the depth range, and whether you aligned depth or inverse depth. A model that looks perfect aligned can be off by meters without it.
Check: monocular depth
CheckpointCheck yourself
How does DPT turn a ViT's tokens into a dense depth map?
Show the answer
Answer: a
Reassembly turns the token sequence back into maps; fusion adds detail from finer levels.
Check yourself
What does a Transformer encoder give a depth model that a small CNN struggles to give?
Show the answer
Answer: a
Global attention helps relate distant parts of a scene, such as a floor and the far wall.
Check yourself
Why does MiDaS fit a scale and shift before computing its training loss?
Show the answer
Answer: a
Mixing laser scans, stereo films and inverse depth is only possible if the loss ignores each image's scale and shift.
Check yourself
A relative-depth model scores a tiny aligned error on your warehouse photos. Can a robot use its output to stop 1 meter from a shelf?
Show the answer
Answer: b
Aligned error hides every mistake a scale and shift can absorb. Meters need calibration and a metric check.
Check yourself
Predicted relative depths are 1, 2 and 3; measured depths are 5, 8 and 11 meters. Fitting measured = scale × predicted + shift, what is the scale?
Show the answer
Answer: 3
The points lie on a line: each step of 1 in prediction adds 3 meters, so scale = 3 and shift = 2.
Recap
Recap- One image fixes depth order and shape far better than distance; most models predict relative depth.
- DPT reassembles ViT tokens into multi-resolution grids and fuses them into a dense map.
- MiDaS mixes datasets by aligning scale and shift per image before scoring.
- Report aligned and metric errors separately; only calibration turns relative depth into meters.
Concepts in this lesson