PIXELBANKv9.1.0
Menu

Scale-Invariant Depth Loss

Compute the scale-invariant depth loss for training depth networks.

Monocular depth estimation is inherently scale-ambiguous - from a single image, we can only recover depth up to an unknown scale factor. The scale-invariant loss allows training without knowing absolute scale:

L=1n∑idi2−λn2(∑idi)2L = \frac{1}{n}\sum_i d_i^2 - \frac{\lambda}{n^2}\left(\sum_i d_i\right)^2

where:

  • di=log⁡Z^i−log⁡Zid_i = \log \hat{Z}_i - \log Z_i is the log-depth error at pixel ii
  • λ∈[0,1]\lambda \in [0, 1] controls scale-invariance (λ=1\lambda = 1 means fully scale-invariant)
  • nn is the number of valid pixels

The first term penalizes depth errors, while the second term allows for a global scale offset.

Example:

Input:
scale_invariant_loss([1, 2, 4], [1, 2, 4], 0.5)
Output:
0.0
Reasoning:

Computing loss for identical predictions:

  • d = [log(1)-log(1), log(2)-log(2), log(4)-log(4)]

  • d = [0, 0, 0] sum(d²) = 0, sum(d)² = 0

  • Loss = 0/3 - 0.5 × 0/9 = 0 Perfect prediction has zero loss.

Constraints:

  • pred: list of predicted depth values (positive)
  • gt: list of ground truth depth values (positive)
  • lambda_param: scale-invariance weight (default 0.5)
  • Return loss rounded to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.