Skip to content

Image understanding, generation, and 3D reconstruction · Monocular depth and dense prediction · VIS017 · Training method

MiDaS mixed-dataset monocular depth learning

MiDaS learns relative monocular depth from diverse datasets with different depth conventions. Its contribution centers on scale/shift-robust learning and mixing supervision sources to improve transfer;

Explained here Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

MiDaS learns relative monocular depth from diverse datasets with different depth conventions. Its contribution centers on scale/shift-robust learning and mixing supervision sources to improve transfer; backbone architectures, including later DPT models, are separate choices.

Question / hypotheses

Can scale/shift-robust mixed-dataset supervision improve relative-depth transfer to previously unseen domains? This is a modeling/evaluation question, not a formal null-hypothesis test.

Architecture / computational structure

A dense depth predictor is optimized across datasets using losses that reduce sensitivity to incompatible absolute depth scales and shifts. Training must respect dataset-specific validity masks and representation conventions; gradient-based detail terms and multi-objective dataset balancing are part of the documented formulation.

Assumptions

Relative depth or inverse depth has scale/shift ambiguity and must not be reported as calibrated meters. Degenerate constant predictions make scale fitting ill-conditioned. Mixing datasets requires consistent orientation, masks and conversion rules; alignment can conceal important deployment errors.

Formula / computational definition

A scale-and-shift-invariant component fits (s∗,t∗)=arg⁡min⁡s,t∑imi(spi+t−di)2(s^*,t^*)=\arg\min_{s,t}\sum_i m_i(sp_i+t-d_i)^2 and evaluates aligned residuals, where pip_i is predicted inverse depth, did_i the target and mim_i a validity mask. The paper also studies robust normalization and multiscale gradient terms; this component is not its entire training objective.

Parameters and notation

Notation: Here m_i is a binary valid-pixel mask, p_i predicted inverse depth, d_i target inverse depth, s a multiplicative scale and t an additive shift. The example solves the least-squares component; robust variants use separately declared centering/scaling and penalty choices.

Learned or estimated: The chosen dense predictor’s weights. Alignment scale and shift are nuisance quantities estimated per training/evaluation instance, not universal camera calibration parameters.

Hyperparameters and effects: Dataset mixture, sampling weights, loss normalization, gradient-loss weight/scales, valid-pixel policy and learning rate. Dataset balance changes which domains dominate; alignment rules change the meaning of reported error.

Fitting, fine-tuning, and adaptation

Start with a documented checkpoint and preserve its output convention. Adapt the head/fusion blocks first, then selected encoder layers at smaller rates using mixed or target-domain supervision. Retain a source-domain validation set to detect forgetting. Metric depth adaptation requires metric targets or calibration and a correspondingly evaluated output head.

How to use and calibrate

Split by independent image source, scene, subject or capture sequence before augmentation; keep near-duplicate frames and crops in the same split. Report cross-dataset transfer under the paper’s stated alignment protocol and also deployment-relevant unaligned errors when metric scale matters. Inspect edges, thin objects, ordinal-depth violations and failure on unfamiliar camera/content distributions.

Example application

Produce transferable relative-depth ordering from heterogeneous image-depth training sources. This is a proposed use case, not a claim of validated performance on supplied real data.

Interpretation / pitfalls

Watch out

A training loss, confidence score or attention map is not a p-value or causal explanation. Confidence calibration, annotation quality and distribution-shift performance need separate assessment. A correct component calculation does not validate the full architecture, checkpoint or deployment pipeline.

Python and R code examples

Scope: Masked least-squares scale/shift alignment of inverse-depth predictions; this is one MiDaS-related invariant loss component, not its complete robust mixed-dataset objective. Use the shared setup. Dependencies: NumPy / base R. Validation: Python executed; R unexecuted.

Full-model route: Python: official MiDaS implementation. R: bridge for checkpoints or custom native torch training. The example solves masked scale/shift alignment and demonstrates its invariance, not the full mixed-dataset procedure.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Masked least-squares scale/shift alignment of inverse-depth predictions; this is one MiDaS-related invariant loss component, not its complete robust mixed-dataset objective. Use the shared setup.

Shared setup for this chapter
import numpy as np
from itertools import permutations
def softmax(x):
    x=np.asarray(x,dtype=float)
    e=np.exp(x-x.max(axis=-1,keepdims=True))
    return e/e.sum(axis=-1,keepdims=True)
def bilinear(a,y,x):
    y=float(np.clip(y,0,a.shape[0]-1)); x=float(np.clip(x,0,a.shape[1]-1))
    y0=int(np.floor(y)); x0=int(np.floor(x))
    y1=min(y0+1,a.shape[0]-1); x1=min(x0+1,a.shape[1]-1)
    dy=y-y0; dx=x-x0
    return (1-dy)*((1-dx)*a[y0,x0]+dx*a[y0,x1])+dy*((1-dx)*a[y1,x0]+dx*a[y1,x1])
pred=np.array([.2,.5,.8,1.1,1.4]); target=np.array([1.4,2.,2.6,3.2,9.])
valid=np.array([True,True,True,True,False])
def align(p):
    design=np.column_stack([p[valid],np.ones(valid.sum())])
    st=np.linalg.lstsq(design,target[valid],rcond=None)[0]
    return st,p*st[0]+st[1]
st,aligned=align(pred); st2,aligned2=align(3*pred-2)
assert np.allclose(aligned,aligned2) and np.allclose(aligned[valid],target[valid])
print('scale/shift:',st,'valid MSE:',np.mean((aligned[valid]-target[valid])**2))
R

Component demo, not a trained model — Masked least-squares scale/shift alignment of inverse-depth predictions; this is one MiDaS-related invariant loss component, not its complete robust mixed-dataset objective. Use the shared setup.

Shared setup for this chapter
softmax <- function(x) {
  if(is.null(dim(x))) {e<-exp(x-max(x)); return(e/sum(e))}
  t(apply(x,1,function(v) {e<-exp(v-max(v)); e/sum(e)}))
}
bilinear <- function(a,y,x) {
  y<-min(max(y,0),nrow(a)-1); x<-min(max(x,0),ncol(a)-1)
  y0<-floor(y); x0<-floor(x); y1<-min(y0+1,nrow(a)-1); x1<-min(x0+1,ncol(a)-1)
  dy<-y-y0; dx<-x-x0
  (1-dy)*((1-dx)*a[y0+1,x0+1]+dx*a[y0+1,x1+1])+dy*((1-dx)*a[y1+1,x0+1]+dx*a[y1+1,x1+1])
}
pred<-c(.2,.5,.8,1.1,1.4); target<-c(1.4,2,2.6,3.2,9)
valid<-c(TRUE,TRUE,TRUE,TRUE,FALSE)
align<-function(p) {
 design<-cbind(p[valid],1); st<-qr.solve(design,target[valid])
 list(st=st,aligned=p*st[1]+st[2])
}
a<-align(pred); b<-align(3*pred-2)
stopifnot(max(abs(a$aligned-b$aligned))<1e-12,max(abs(a$aligned[valid]-target[valid]))<1e-12)
print(list(scale_shift=a$st,valid_MSE=mean((a$aligned[valid]-target[valid])^2)))

Sources

From the CodeSplash reference, entry VIS017.