Skip to content

Add error message when setting 1D tensor ToImage() #9114

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

Merged
merged 4 commits into from
Jun 23, 2025

Conversation

chengolivia
Copy link
Contributor

As per #9025, I added a descriptive error message to the torchvision.transforms.v2.ToImage() method when a 1D tensor is passed in.

Copy link

pytorch-bot bot commented Jun 23, 2025

🔗 Helpful Links

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

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

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

@facebook-github-bot
Copy link

Hi @chengolivia!

Thank you for your pull request.

We require contributors to sign our Contributor License Agreement, and yours needs attention.

You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted.

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 [email protected]. Thanks!

@chengolivia chengolivia marked this pull request as ready for review June 23, 2025 15:23
@chengolivia
Copy link
Contributor Author

Tested with the following test.py script:

import torch
from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import ToImage

my_data = OxfordIIITPet(
    root="data"
)

ti = ToImage()

ti(torch.tensor([0]))

Output:

Traceback (most recent call last):
  File "/Users/oliviacheng/projects/oss/vision/test.py", line 11, in <module>
    ti(torch.tensor([0]))
    ~~^^^^^^^^^^^^^^^^^^^
  File "/Users/oliviacheng/miniconda3/envs/pytorch-dev/lib/python3.13/site-packages/torch/nn/modules/module.py", line 1773, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/oliviacheng/miniconda3/envs/pytorch-dev/lib/python3.13/site-packages/torch/nn/modules/module.py", line 1784, in _call_impl
    return forward_call(*args, **kwargs)
  File "/Users/oliviacheng/projects/oss/vision/torchvision/transforms/v2/_transform.py", line 69, in forward
    self.transform(inpt, params) if needs_transform else inpt
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/Users/oliviacheng/projects/oss/vision/torchvision/transforms/v2/_type_conversion.py", line 39, in transform
    return F.to_image(inpt)
           ~~~~~~~~~~^^^^^^
  File "/Users/oliviacheng/projects/oss/vision/torchvision/transforms/v2/functional/_type_conversion.py", line 23, in to_image
    return tv_tensors.Image(output)
           ~~~~~~~~~~~~~~~~^^^^^^^^
  File "/Users/oliviacheng/projects/oss/vision/torchvision/tv_tensors/_image.py", line 46, in __new__
    raise ValueError(f"Tensor must be 2D or higher, got {tensor.ndim}D tensor.")
ValueError: Tensor must be 2D or higher, got 1D tensor.

@chengolivia
Copy link
Contributor Author

Closes #9025

@facebook-github-bot
Copy link

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

Great, thank you for the PR @chengolivia

@NicolasHug NicolasHug merged commit 7b283c2 into pytorch:main Jun 23, 2025
1 check passed
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

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.

3 participants