v9.1.1
Menu

Ordinal Depth Ranking Loss

mediumPRODeep Depth EstimationDeep LearningCV: Depth Estimation

Compute the ordinal (ranking) depth loss.

Ordinal depth loss enforces that the relative ordering of depths is preserved, even if absolute depths are wrong. For each pair of pixels (i,j)(i, j) with i<ji < j:

Li,j=max⁡(0,−sign(Zi−Zj)⋅(Z^i−Z^j)+τ)L_{i,j} = \max(0, -\text{sign}(Z_i - Z_j) \cdot (\hat{Z}_i - \hat{Z}_j) + \tau)

where:

  • ZZ is ground truth depth
  • Z^\hat{Z} is predicted depth
  • τ\tau is a margin
  • sign()\text{sign}() returns -1, 0, or 1

Pairs with equal ground-truth depth (Zi=ZjZ_i = Z_j) are skipped and contribute no loss. The total loss is the sum of Li,jL_{i,j} over all remaining pairs, rounded to 4 decimal places.

The loss penalizes predictions that violate the ground truth ordering, or respect it by less than the margin τ.

Ordinal loss is useful when absolute depth is hard to obtain but relative depth (which object is closer) is known.

Example

Input
ordinal_loss([1, 2, 3], [1, 2, 3], 0.1)
Output
0.0
Reasoning
  • pred and gt hold one depth per pixel, and margin is τ=0.1\tau = 0.1. Every unordered pair of pixels is compared once: (0,1)(0,1), (0,2)(0,2), (1,2)(1,2).
  • Pair (0,1)(0,1): ground truth gives Z0−Z1=1−2=−1Z_0 - Z_1 = 1 - 2 = -1, so the sign is −1-1 (pixel 0 is closer). The predicted gap Z^0−Z^1=−1\hat{Z}_0 - \hat{Z}_1 = -1 agrees, so L0,1=max⁡(0,−(−1)(−1)+0.1)=max⁡(0,−0.9)=0L_{0,1} = \max(0, -(-1)(-1) + 0.1) = \max(0, -0.9) = 0.
  • Pair (0,2)(0,2): sign −1-1, predicted gap −2-2, so max⁡(0,−2+0.1)=0\max(0, -2 + 0.1) = 0. Pair (1,2)(1,2): sign −1-1, gap −1-1, so max⁡(0,−0.9)=0\max(0, -0.9) = 0.
  • The total is 0+0+00 + 0 + 0, printed as 0.0: every predicted gap points the right way and exceeds the margin 0.10.1.
  • Notice a pair only costs something when its predicted gap points the wrong way or is right by less than τ\tau. Reversing the prediction to [3,2,1][3, 2, 1] makes every pair violate: 1.1+2.1+1.1=4.31.1 + 2.1 + 1.1 = 4.3.
  • Pairs whose ground-truth depths are equal carry no ordering and add no loss.

Constraints

  • pred: list of predicted depths
  • gt: list of ground truth depths
  • margin: ranking margin (τ)
  • Return total ordinal loss rounded to 4 decimal places

Learn this in

More from CV: Depth Estimation

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.