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

allow dispatch to PIL image subclasses #7835

Merged
merged 3 commits into from
Aug 16, 2023
Merged

Conversation

pmeier
Copy link
Collaborator

@pmeier pmeier commented Aug 16, 2023

Bug that was spotted by @vfdev-5:

opened_image = PIL.Image.open("test/assets/encode_jpeg/grace_hopper_517x606.jpg")
loaded_image = opened_image.convert("RGB")

assert isinstance(opened_image, PIL.Image.Image)
assert type(opened_image) is not PIL.Image.Image

assert type(loaded_image) is PIL.Image.Image

F.resize(loaded_image, (17, 11))  # ok
F.resize(opened_image, (17, 11))  # raises TypeError, since we currently only allow exact matches for PIL.Image.Image

This PR removes the restriction that we only walk up the MRO for datapoints.Datapoint and thus enabling an "isinstance" check for PIL.Image.Image.

That also means that we can now also register kernels for subclasses of PIL.Image.Image or torch.Tensor. However, since we don't plan on doing that and users are not allowed to do so

if not (isinstance(datapoint_cls, type) and issubclass(datapoint_cls, datapoints.Datapoint)):
raise ValueError(
f"Kernels can only be registered for subclasses of torchvision.datapoints.Datapoint, "
f"but got {datapoint_cls}."
)

this has no other side-effect. It actually simplifies the code.

cc @vfdev-5

@pytorch-bot
Copy link

pytorch-bot bot commented Aug 16, 2023

🔗 Helpful Links

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

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

❌ 7 New Failures

As of commit e3db48a with merge base c1592f9 (image):

NEW FAILURES - The following jobs have failed:

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

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.

LGTM if green, thanks Philip

@pmeier pmeier merged commit e0e6f7e into pytorch:main Aug 16, 2023
@pmeier pmeier deleted the pil-dispatch branch August 16, 2023 11:06
facebook-github-bot pushed a commit that referenced this pull request Aug 25, 2023
Reviewed By: matteobettini

Differential Revision: D48642256

fbshipit-source-id: 8595836f1e5382eccede49c77247651faee23398
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