Skip to content

UserWarning: Legacy tensor constructor is deprecated. #3666

@rafi-cohen

Description

@rafi-cohen

🐛 Bug

Using the ToTensor transform results in the following warning:

/path/to/env/lib/python3.8/site-packages/torchvision/transforms/functional.py:135: UserWarning: Legacy tensor constructor is deprecated. Use: torch.tensor(...) for creating tensors from tensor-like objects; or torch.empty(...) for creating an uninitialized tensor with specific sizes. (Triggered internally at  /opt/conda/conda-bld/pytorch_1618124769378/work/torch/csrc/utils/tensor_new.cpp:477.)

According to the message, the following line triggers the warning:

img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))

Note that there might be more places where the legacy constructor is used.

To Reproduce

Steps to reproduce the behavior:

  1. Simply apply the ToTensor transform

Below is a pure torchvision script generating the warning:

from torchvision.transforms import ToTensor
from torchvision.datasets import FakeData

data = FakeData(transform=ToTensor())
data[0]

Expected behavior

No warning

Environment

PyTorch version: 1.9.0.dev20210411
Is debug build: False
CUDA used to build PyTorch: 11.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: Could not collect

Python version: 3.8 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: 9.1.85
GPU models and configuration: 
GPU 0: GeForce GTX 1080 Ti
GPU 1: GeForce GTX 1080 Ti

Nvidia driver version: 450.102.04
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.2
[pip3] torch==1.9.0.dev20210411
[pip3] torchaudio==0.9.0a0+e8c5e45
[pip3] torchvision==0.10.0.dev20210411
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               11.1.1               h6406543_8    conda-forge
[conda] mkl                       2020.4             h726a3e6_304    conda-forge
[conda] mkl-service               2.3.0            py38h1e0a361_2    conda-forge
[conda] mkl_fft                   1.3.0            py38h5c078b8_1    conda-forge
[conda] mkl_random                1.2.0            py38hc5bc63f_1    conda-forge
[conda] numpy                     1.19.2           py38h54aff64_0  
[conda] numpy-base                1.19.2           py38hfa32c7d_0  
[conda] pytorch                   1.9.0.dev20210411 py3.8_cuda11.1_cudnn8.0.5_0    pytorch-nightly
[conda] torchaudio                0.9.0.dev20210411            py38    pytorch-nightly
[conda] torchvision               0.10.0.dev20210411      py38_cu111    pytorch-nightly

Additional context

pytorch/pytorch#54414
However, this change is about to be reverted, here: pytorch/pytorch#55831
But I'm guessing that once pytorch/pytorch#55780 is resolved, the legacy constructors will be deprecated again.
Perhaps something like pytorch/pytorch#54142 should be done in this repository as well.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions