-
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
to_pil_image: correct rounding #8142
base: main
Are you sure you want to change the base?
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/8142
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Hi @rb-synth! 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 [email protected]. Thanks! |
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.
Although this is technically BC breaking, I would be ok to treat this as a bug fix, given that this aligns this transform with ConvertImageDtype
:
vision/torchvision/transforms/_functional_tensor.py
Lines 81 to 89 in 30397d9
# https://github.com/pytorch/vision/pull/2078#issuecomment-612045321 | |
# For data in the range 0-1, (float * 255).to(uint) is only 255 | |
# when float is exactly 1.0. | |
# `max + 1 - epsilon` provides more evenly distributed mapping of | |
# ranges of floats to ints. | |
eps = 1e-3 | |
max_val = float(_max_value(dtype)) | |
result = image.mul(max_val + 1.0 - eps) | |
return result.to(dtype) |
Note that the only reason for us not using round
in the snippet above is that it is an additional op that we can avoid if we are ok with slightly different behavior.
Thoughts @NicolasHug?
Thanks for your feedback! We were running into this problem when we wrapped VitMatte, and even though it was fairly confident that a pixel was forgreound (0.9999), this would be rounded down to 254, which was causing problems for us further downstream. So I'd advocate for an implementation that was more correct at the expense of a small increase in computation time. As is, anything processed with |
fixes #8141