Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"compute_scale_and_shift" using median + MAD vs least square #22

Open
3togo opened this issue Dec 29, 2022 · 1 comment
Open

"compute_scale_and_shift" using median + MAD vs least square #22

3togo opened this issue Dec 29, 2022 · 1 comment

Comments

@3togo
Copy link

3togo commented Dec 29, 2022

"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 3togo closed this as completed Jan 5, 2023
@3togo 3togo changed the title Any reference to explain the logic of "compute_scale_and_shift"? "compute_scale_and_shift" using median + MAD vs least square Feb 6, 2023
@3togo
Copy link
Author

3togo commented Feb 6, 2023

Have median + MAD been implemented yet?

@3togo 3togo reopened this Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant