-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Conversation
🔗 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 (): 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. |
Hi @Parskatt! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Signed the CLA |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
There was a problem hiding this 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
torchvision/transforms/functional.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
torchvision/transforms/functional.py
Outdated
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) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good, changed
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)):
|
There was a problem hiding this 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
Summary: Co-authored-by: Nicolas Hug <nh.nicolas.hug@gmail.com> Reviewed By: vmoens Differential Revision: D55062809 fbshipit-source-id: 8303a6f16c8c4fb3a59c970110edd557e9aeadeb
See #8248
cc @vfdev-5