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.ToTensor and transforms.v2.ToTensor #8902

Open
jneuendorf opened this issue Feb 7, 2025 · 0 comments
Open

Comments

@jneuendorf
Copy link

🐛 Describe the bug

In the docs it says

Deprecated

Func Desc
v2.ToTensor() [DEPRECATED] Use v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]) instead.

But when using the suggested code, the values are slightly different.

Test code

from pprint import pprint

import torch
import numpy as np
import torchvision.transforms.v2 as v2
from torchvision import transforms as v1
from PIL.Image import Image, fromarray

np_image = np.array(
    [[[100, 150, 200]], [[25, 75, 125]]],
    dtype=np.uint8,
)
pil_image = fromarray(np_image)  # like done for CIFAR10


class ToTensorV2:
    # As of https://pytorch.org/vision/stable/transforms.html#conversion
    to_tensor = v2.Compose(
        [
            v2.ToImage(),
            v2.ToDtype(torch.float32, scale=True),
        ]
    )

    def __call__(self, inpt: torch.Tensor | Image | np.ndarray) -> torch.Tensor:
        return self.to_tensor(inpt)


original_transform = v1.ToTensor()
custom_transform = ToTensorV2()
result1 = original_transform(pil_image)
result2 = custom_transform(pil_image)

# Print results
print("Original image (numpy array):")
print(np_image)
print("\nShape:", np_image.shape)
print("dtype:", np_image.dtype)

print("\nv1.ToTensor() result:")
print(result1)
pprint(result1.tolist())
print("\nShape:", result1.shape)
print("dtype:", result1.dtype)

print("\nToTensorV2 result:")
print(result2)
pprint(result2.tolist())
print("\nShape:", result2.shape)
print("dtype:", result2.dtype)

print("\nDiff:")
print(result1 == result2)

Versions

PyTorch version: 2.5.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 15.3 (arm64)
GCC version: Could not collect
Clang version: 16.0.0 (clang-1600.0.26.6)
CMake version: version 3.31.5
Libc version: N/A

Python version: 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:35:20) [Clang 16.0.6 ] (64-bit runtime)
Python platform: macOS-15.3-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Apple M4 Pro

Versions of relevant libraries:
[pip3] mypy==1.15.0
[pip3] mypy_extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] pytorch-lightning==2.5.0.post0
[pip3] pytorch-metric-learning==2.8.1
[pip3] torch==2.5.1
[pip3] torchmetrics==1.6.1
[pip3] torchvision==0.20.1
[conda] faiss-cpu                 1.9.0           py3.12_hbe593ad_0_cpu    pytorch
[conda] libfaiss                  1.9.0            hcb8d3e5_0_cpu    pytorch
[conda] numpy                     1.26.4          py312h8442bc7_0    conda-forge
[conda] pytorch                   2.5.1                  py3.12_0    pytorch
[conda] pytorch-lightning         2.5.0.post0        pyh101cb37_0    conda-forge
[conda] pytorch-metric-learning   2.8.1              pyh101cb37_1    conda-forge
[conda] torchmetrics              1.6.1              pyhd8ed1ab_0    conda-forge
[conda] torchvision               0.20.1                py312_cpu    pytorch
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

1 participant