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

Different Behaviors of tranforms.Resize and transforms.v2.Resize #8895

Closed
donglihe-hub opened this issue Feb 3, 2025 · 1 comment
Closed

Comments

@donglihe-hub
Copy link

🐛 Describe the bug

Test codes

import numpy as np
from torchvision.transforms import v2
from torchvision import transforms


if __name__ == "__main__":
    t = np.ones((1023, 587))

    transform_v2 = v2.Compose(
        [
            v2.Resize((224, 224)),
            v2.ToTensor(),
        ]
    )
    print(f"v2 passed. {transform_v2(t).shape} {type(transform_v2(t))}")

    transform_v1 = transforms.Compose(
        [
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
        ]
    )
    print(transform_v1(t).shape)

The expected output of the test codes is a (1, 224, 224) tensor. When input is an numpy.ndarray, transforms.Resize raises

TypeError: Unexpected type <class 'numpy.ndarray'>

While transforms.v2.Resize silently runs the code without resizing the input

v2 passed. torch.Size([1, 1023, 587]) <class 'torch.Tensor'>

IMO, shouldn't v2 behave the same as v1. If their bahaviors are not expected to be the same, shouldn't v2 at least tell users resizing doesn't take effect?

Versions

torchvision 0.21.0

@NicolasHug
Copy link
Member

numpy array aren't supported by torchvision's transforms, and they're treated as foreign types (just like int, str, etc.). So on v1 you pass a foreign type and you get an error. On v2, those foreign types are passed-through, which is useful e.g. to keep track of metadata associated with files, like their path, or anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants