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

to_pil_image: correct rounding #8142

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

rb-synth
Copy link

@rb-synth rb-synth commented Dec 4, 2023

fixes #8141

Copy link

pytorch-bot bot commented Dec 4, 2023

🔗 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.

@facebook-github-bot
Copy link

Hi @rb-synth!

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!

@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
Collaborator

@pmeier pmeier left a 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:

# 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?

@rb-synth
Copy link
Author

rb-synth commented Dec 5, 2023

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 to_pil_image is systematically decreased by up to 1/255 = 0.39% intensity.

@NicolasHug NicolasHug added this to Ideally in 0.18 TODOs Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
0.18 TODOs
Ideally
Development

Successfully merging this pull request may close these issues.

to_pil_image rounds down
3 participants