PIXELBANKv9.1.0
Menu

Ordinal Depth Ranking Loss

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):

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

The loss penalizes predictions that violate the ground truth ordering by more than 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:
  • Checking all pairs with correct ordering: Pair (0,1): gt[0]=1 < gt[1]=2, pred[0]=1 < pred[1]=2 βœ“ (correct order)

  • sign = -1, pred_diff = -1, loss = max(0, -(-1)Γ—(-1) + 0.1) = max(0, -0.9) = 0 Pair (0,2): correct order, loss = 0 Pair (1,2): correct order, loss = 0 Total: 0.0

Constraints:

  • pred: list of predicted depths
  • gt: list of ground truth depths
  • margin: ranking margin (Ο„)
  • Return total ordinal loss rounded to 4 decimal places
πŸ”’

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

0/0
Run code to see test results.