FocusOnDepth icon indicating copy to clipboard operation
FocusOnDepth copied to clipboard

"compute_scale_and_shift" using median + MAD vs least square

Open 3togo opened this issue 3 years ago • 1 comments

"compute_scale_and_shift" can be found in Loss.py and is based on least square loss. Given that MiDaS-v2 was trained using the median + MAD and MiDaS-v1 was trained using the least squares-based loss (https://arxiv.org/abs/1907.01341v1)

Have median + MAD been implemented yet?

def compute_scale_and_shift(prediction, target, mask):
    # system matrix: A = [[a_00, a_01], [a_10, a_11]]
    a_00 = torch.sum(mask * prediction * prediction, (1, 2))
    a_01 = torch.sum(mask * prediction, (1, 2))
    a_11 = torch.sum(mask, (1, 2))

    # right hand side: b = [b_0, b_1]
    b_0 = torch.sum(mask * prediction * target, (1, 2))
    b_1 = torch.sum(mask * target, (1, 2))

    # solution: x = A^-1 . b = [[a_11, -a_01], [-a_10, a_00]] / (a_00 * a_11 - a_01 * a_10) . b
    x_0 = torch.zeros_like(b_0)
    x_1 = torch.zeros_like(b_1)

    det = a_00 * a_11 - a_01 * a_01
    valid = det.nonzero()

    x_0[valid] = (a_11[valid] * b_0[valid] - a_01[valid] * b_1[valid]) / det[valid]
    x_1[valid] = (-a_01[valid] * b_0[valid] + a_00[valid] * b_1[valid]) / det[valid]

    return x_0, x_1

https://gist.github.com/dvdhfnr/732c26b61a0e63a0abc8a5d769dbebd0

https://arxiv.org/pdf/1907.01341v3.pdf

3togo avatar Dec 29 '22 09:12 3togo

Have median + MAD been implemented yet?

3togo avatar Feb 06 '23 01:02 3togo