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

Refactor load_image to return torch.Tensor instead of PIL.Image #2366

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Ankur-singh
Copy link
Contributor

Context

What is the purpose of this PR? Is it to

  • add a new feature
  • fix a bug
  • update tests and/or documentation
  • other (refactor)

Please link to any issues this PR addresses. #2303

Changelog

What are the changes made in this PR?

  • Updated load_image to return torch.Tensor instead of PIL.Image
  • Updated CLIPImageTransform to support both torch.Tensor and PIL.Image
  • Added 4x new tests for CLIPImageTransform with torch.Tensor input

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

  • run pre-commit hooks and linters (make sure you've first installed via pre-commit install)
  • add unit tests for any new functionality
  • update docstrings for any new or updated methods or classes
  • run unit tests via pytest tests
  • run recipe tests via pytest tests -m integration_test
  • manually run any new or modified recipes with sufficient proof of correctness
  • include relevant commands and any other artifacts in this summary (pastes of loss curves, eval results, etc.)

UX

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

  • I did not change any public API
  • I have added an example to docs or docstrings

Copy link

pytorch-bot bot commented Feb 8, 2025

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

❌ 2 New Failures, 3 Cancelled Jobs

As of commit f77145f with merge base 8c9235e (image):

NEW FAILURES - The following jobs have failed:

CANCELLED JOBS - The following jobs were cancelled. Please retry:

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

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 8, 2025
Copy link
Contributor

@joecummings joecummings left a 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.

Copy link
Contributor

@joecummings joecummings left a 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!

@@ -44,9 +46,9 @@ def truncate(
return tokens_truncated


def load_image(image_loc: Union[Path, str]) -> "PIL.Image.Image":
def load_image(image_loc: Union[Path, str]) -> "torch.Tensor":
Copy link
Contributor

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.

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
Copy link
Contributor

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.

try:
image = torchvision.io.decode_image(image_loc)
except Exception as e:
raise ValueError("Failed to open image as torch.Tensor") from e
Copy link
Contributor

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.

@@ -252,9 +252,13 @@ def __init__(
antialias=self.antialias,
)

def __call__(self, *, image: Image.Image, **kwargs) -> Mapping[str, Any]:
def __call__(
self, *, image: Union[Image.Image, torch.Tensor], **kwargs
Copy link
Contributor

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?

@NicolasHug
Copy link
Member

cc @NicolasHug just for a quick glance.

This SGTM

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)
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants