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

Make _get_perspective_coeffs more numerically stable #8249

Merged
merged 5 commits into from
Feb 6, 2024

Conversation

Parskatt
Copy link
Contributor

@Parskatt Parskatt commented Feb 2, 2024

See #8248

cc @vfdev-5

Copy link

pytorch-bot bot commented Feb 2, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/8249

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (17 Unrelated Failures)

As of commit fb78e35 with merge base ae14789 (image):

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link

Hi @Parskatt!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@Parskatt
Copy link
Contributor Author

Parskatt commented Feb 2, 2024

Signed the CLA

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the report and for the fix @Parskatt . This looks good, just some minor comments below, LMK what you think

@@ -676,14 +676,16 @@ def _get_perspective_coeffs(startpoints: List[List[int]], endpoints: List[List[i
Returns:
octuple (a, b, c, d, e, f, g, h) for transforming each pixel.
"""
# TODO: this should raise an error if < 4 points are provided (and probably also if more than 4) //Parskatt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to address this here!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made a simple check for 4 corners

a_matrix = torch.zeros(2 * len(startpoints), 8, dtype=torch.float)

for i, (p1, p2) in enumerate(zip(endpoints, startpoints)):
a_matrix[2 * i, :] = torch.tensor([p1[0], p1[1], 1, 0, 0, 0, -p2[0] * p1[0], -p2[0] * p1[1]])
a_matrix[2 * i + 1, :] = torch.tensor([0, 0, 0, p1[0], p1[1], 1, -p2[1] * p1[0], -p2[1] * p1[1]])

b_matrix = torch.tensor(startpoints, dtype=torch.float).view(8)
res = torch.linalg.lstsq(a_matrix, b_matrix, driver="gels").solution
# do least squares in double precision to prevent numerical issues
res = torch.linalg.lstsq(a_matrix.double(), b_matrix.double(), driver="gels").solution.to(torch.float32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating a_matrix and b_matrix as float and then converting them as double, should we just create both as double?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, changed

@NicolasHug
Copy link
Member

Thanks @Parskatt . Looks like there's still a minor lint issue. I tried pushing directly to your PR branch but I don't have persmission. This diff should fix it, if you don't mind applying it manually

diff --git a/torchvision/transforms/functional.py b/torchvision/transforms/functional.py
index c67a45001f..b5c242c1e6 100644
--- a/torchvision/transforms/functional.py
+++ b/torchvision/transforms/functional.py
@@ -679,7 +679,7 @@ def _get_perspective_coeffs(startpoints: List[List[int]], endpoints: List[List[i
     if len(startpoints) != 4 or len(endpoints) != 4:
         raise ValueError(
             f"Please provide exactly four corners, got {len(startpoints)} startpoints and {len(endpoints)} endpoints."
-            )
+        )
     a_matrix = torch.zeros(2 * len(startpoints), 8, dtype=torch.float64)
 
     for i, (p1, p2) in enumerate(zip(endpoints, startpoints)):

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some CI failures but they're unrelated so this is good to merge. Thanks a lot for the report and for the fix @Parskatt

@NicolasHug NicolasHug merged commit 4c0f441 into pytorch:main Feb 6, 2024
45 of 62 checks passed
facebook-github-bot pushed a commit that referenced this pull request Mar 20, 2024
Summary: Co-authored-by: Nicolas Hug <[email protected]>

Reviewed By: vmoens

Differential Revision: D55062809

fbshipit-source-id: 8303a6f16c8c4fb3a59c970110edd557e9aeadeb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants