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

Fix overshoot issue in F.to_pil_image #3610

Closed
wants to merge 4 commits into from
Closed

Fix overshoot issue in F.to_pil_image #3610

wants to merge 4 commits into from

Conversation

Coloquinte
Copy link

When converting a tensor to a PIL image, overshoots are not clamped. This means that a value of 1.001 becomes 0 instead of 255. This issue is described here: #2950 (comment)
Additionally, replace the imprecise truncation by a rounding.
Both issues are particularly annoying for downsampling/upsampling code, where such overshoots are common.

@facebook-github-bot
Copy link

Hi @Coloquinte!

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 cla@fb.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook 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.

I think these are reasonable changes, thanks @Coloquinte .

It'd be nice to also have a simple non-regression test in test/test_transforms.py, with a reference to this PR as a comment (it's often easier than to follow git blame)

@NicolasHug
Copy link
Member

Although the failing tests seem related: would you mind looking at these?

@Coloquinte
Copy link
Author

Thanks! I fixed the transform tests and added mine. I'm letting the CI do the checks, so there may be one more round.

It seems that some tests were failing due to the branch being a fork. I don't know how to workaround these.

When converting a tensor to a PIL image, overshoots are not clamped. This means that a value of 1.001 becomes 0 instead of 255. This issue is described here: #2950 (comment)
Additionally, replace the imprecise truncation by a rounding.
Both issues are particularly annoying for downsampling/upsampling code, where such overshoots are common.
@Coloquinte
Copy link
Author

The following error is still present during the CI run:

ValueError: Cannot find master in https://github.com/pytorch/vision. If it's a commit from a forked repo, please call hub.load() with forked repo directly.

Is there anything to do on my side to help merge this?

@fmassa
Copy link
Member

fmassa commented Apr 10, 2021

Hi @Coloquinte

The torchub failure is unrelated, don't worry.

I'll have a closer look at the PR on Monday, but here is my quick thoughts about it:

  • ideally, to_pil_image(to_tensor(data)) == data, and I'm not yet clear that the round satisfies this behavior

I think it might be safer to remove the .round, as it will affect the behavior of to_pil_image, but I'll let @vfdev-5 chime in as well

@Coloquinte
Copy link
Author

Hi @fmassa,

Thanks, no hurry.

I can confirm that to_pil_image(to_tensor(data)) == data is preserved - I can add a unit test for this if you like.

In my opinion, round() is necessary because it has the lowest error for to_tensor(to_pil_image(data)) - data, and is robust to a little noise i.e. to_pil_image(to_tensor(data) - epsilon) == data. This is important for image reconstruction applications, which otherwise have to second guess this behaviour to reduce the reconstruction error.

@Coloquinte
Copy link
Author

Hi,
It would be nice to see this merged. Any action I can take?

@datumbox datumbox requested a review from vfdev-5 June 13, 2021 19:18
@NicolasHug
Copy link
Member

Hi @Coloquinte , thanks for your patience. I took care of porting the tests to our new testing infra (with pytest).

I can confirm that to_pil_image(to_tensor(data)) == data is preserved - I can add a unit test for this if you like.

I think having such round-trip test would be great, yes! We could even parametrize it over a few epsilon values and do o_pil_image(to_tensor(data) - epsilon) == data as you suggested above.

@Coloquinte
Copy link
Author

Thanks @NicolasHug ! I pushed such a test on my branch

@NicolasHug
Copy link
Member

Hi @Coloquinte , I fixed the failing tests that resulted from a problem during the merge, but I think the new test_tensor_to_pil_robust is failing because it's not properly parametrized. Would you mind taking a look?

@Coloquinte Coloquinte closed this by deleting the head repository Apr 24, 2024
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.

4 participants