-
Notifications
You must be signed in to change notification settings - Fork 683
Refactor load_image to return torch.Tensor instead of PIL.Image #2366
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
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/torchtune/2366
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit a069f8a with merge base 7b654ea ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
joecummings
left a comment
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.
cc @NicolasHug just for a quick glance.
joecummings
left a comment
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.
Couple of comments, but super happy to see this working!
torchtune/data/_utils.py
Outdated
|
|
||
|
|
||
| def load_image(image_loc: Union[Path, str]) -> "PIL.Image.Image": | ||
| def load_image(image_loc: Union[Path, str]) -> "torch.Tensor": |
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.
You don't need to quote the return type as torch.Tensor is definitely already importable.
torchtune/data/_utils.py
Outdated
| image = Image.open(image_loc) | ||
| except Exception as e: | ||
| raise ValueError(f"Failed to open image as PIL Image from {image_loc}") from e | ||
| raise ValueError("Failed to load image as torch.Tensor") from e |
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.
Can you be more specific with this error? Something like failed to load image from remote.
torchtune/data/_utils.py
Outdated
| try: | ||
| image = torchvision.io.decode_image(image_loc) | ||
| except Exception as e: | ||
| raise ValueError("Failed to open image as torch.Tensor") from e |
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.
Same here, lets be specific that this fails to load from a local image.
|
|
||
| def __call__(self, *, image: Image.Image, **kwargs) -> Mapping[str, Any]: | ||
| def __call__( | ||
| self, *, image: Union[Image.Image, torch.Tensor], **kwargs |
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.
cc @pbontrager Do we actually want to allow PIL images through here? Or limit to just tensor?
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.
hi @pbontrager, any comments?
This SGTM |
torchtune/data/_utils.py
Outdated
| image_loc = request.urlopen(image_loc) | ||
| image_loc = request.urlopen(image_loc).read() | ||
| image = torchvision.io.decode_image( | ||
| torch.frombuffer(image_loc, dtype=torch.uint8) |
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.
Chatting with @pbontrager about this, I think you'll want to ensure that you get RGB images from decode_image by passing mode="RGB". https://pytorch.org/vision/main/generated/torchvision.io.ImageReadMode.html#torchvision.io.ImageReadMode
This is typically needed for images that are encoded as grayscale, or have some transparency channel.
(Here and below)
f77145f to
3f04994
Compare
|
Sorry guys, I was using python 3.11 and everything seemed to work. It was a typing issue, learnt that in python 3.9 one can't use subscripted generics like if isinstance(x, Union[Image.Image, torch.Tensor]):
... I change it to if isinstance(x, (Image.Image, torch.Tensor)):
... this seems to work in both python 3.9 and 3.11.
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2366 +/- ##
=======================================
Coverage 23.46% 23.46%
=======================================
Files 373 373
Lines 22405 22420 +15
=======================================
+ Hits 5257 5261 +4
- Misses 17148 17159 +11 ☔ View full report in Codecov by Sentry. |
4150ee0 to
3cd5709
Compare
|
Hi guys, sorry I've been a bit out of the loop on this one. @Ankur-singh what's the status here? Is this ready for another round of review? |
|
@ebsmothers its read for review. Should work 🤞 |
joecummings
left a comment
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.
lgtm
3cd5709 to
a069f8a
Compare
|
Thanks for all the hard work @Ankur-singh !!!@ |
Context
What is the purpose of this PR? Is it to
Please link to any issues this PR addresses. #2303
Changelog
What are the changes made in this PR?
load_imageto returntorch.Tensorinstead ofPIL.ImageCLIPImageTransformto support bothtorch.TensorandPIL.ImageCLIPImageTransformwithtorch.TensorinputTest plan
Please make sure to do each of the following if applicable to your PR. If you're unsure about any one of these just ask and we will happily help. We also have a contributing page for some guidance on contributing.
pre-commit install)pytest testspytest tests -m integration_testUX
If your function changed a public API, please add a dummy example of what the user experience will look like when calling it.
Here is a docstring example
and a tutorial example