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 bug when using PIL backend in references/classification #7665

Merged
merged 1 commit into from
Jun 12, 2023

Conversation

AetelFinch
Copy link
Contributor

fix #7664

@pytorch-bot
Copy link

pytorch-bot bot commented Jun 10, 2023

🔗 Helpful Links

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

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

✅ No Failures

As of commit f776906:
💚 Looks good so far! There are no failures yet. 💚

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

@facebook-github-bot
Copy link

Hi @AetelFinch!

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

@AetelFinch AetelFinch changed the title fix bug when using PIL backend in references/classification #7664 fix bug when using PIL backend in references/classification Jun 10, 2023
@@ -69,11 +69,10 @@ def __init__(
backend="pil",
):
trans = []

backend = backend.lower()
if backend == "tensor":
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably this should change to

Suggested change
if backend == "tensor":
if backend == "tensor" or backend == "pil":

backend = backend.lower()
if backend == "tensor":
trans.append(transforms.PILToTensor())
else:
elif backend != "pil":
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably still be buggy when backend="pil"

As it will not apply PILtoTensor() transform.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we apply PILtoTensor when backend="pil"? I was just guided by the way it is done in the ClassificationPresetTrain class

Copy link
Contributor

Choose a reason for hiding this comment

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

We probably do this in Line 83. I missed this. But it's confusing why would one apply PILToTensor() when backend type is Tensor(). Maybe I'm missing something again.

Copy link
Contributor Author

@AetelFinch AetelFinch Jun 11, 2023

Choose a reason for hiding this comment

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

As I see, we apply PILtoTensor at the beginning, when backend=="tensor", to do transformations over tensors, not over PIL images.

trans += [
    transforms.Resize(resize_size, interpolation=interpolation, antialias=True),
    transforms.CenterCrop(crop_size),
]

As written in the documentation for transforms.Resize:
"The output image might be different depending on its type: when downsampling, the interpolation of PIL images and tensors is slightly different, because PIL applies antialiasing. This may lead to significant differences in the performance of a network."

Copy link
Member

Choose a reason for hiding this comment

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

But it's confusing why would one apply PILToTensor() when backend type is Tensor().

Yeah it's not super clean nor obvious just reading the code. The key piece of information is that those presets make the hard assumption that whatever you pass as input is a PIL image, no matter the backend!

So when we pass backend="tensor" we actually need to first convert the input (a PIL image) to a tensor.

A more complete solution would be to do all those checks "at runtime" in forward() instead of here.

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 @AetelFinch , LGTM, will merge when green

@NicolasHug NicolasHug merged commit cff78aa into pytorch:main Jun 12, 2023
@github-actions
Copy link

Hey @NicolasHug!

You merged this PR, but no labels were added. The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

facebook-github-bot pushed a commit that referenced this pull request Jun 14, 2023
…7665)

Summary: Co-authored-by: Max Chuprov <m.chuprov@expasoft.tech>

Reviewed By: vmoens

Differential Revision: D46724127

fbshipit-source-id: 300232aadc8fe22ece46c229ec59ac0b95784756
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.

bug when using PIL backend in classification references
4 participants