question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UserWarning: Legacy tensor constructor is deprecated.

See original GitHub issue

🐛 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: https://github.com/pytorch/vision/blob/eee424922e998dddcaabe927e823ec515aa2927b/torchvision/transforms/functional.py#L135

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

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rafi-cohencommented, Apr 13, 2021

The offending line torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes())) is part of the implementation of torchvision.transforms.functional.to_tensor, which is called by ToTensor. Therefore, using ToTensor triggers a warning.

0reactions
fmassacommented, Jun 21, 2021

Fixed in #3813

Read more comments on GitHub >

github_iconTop Results From Across the Web

userwarning: torch.eig is deprecated in favor of ... - You.com
torch.eig() is deprecated in favor of torch.linalg.eig() and will be removed in a ... pytorch/visionUserWarning: Legacy tensor constructor is deprecated.
Read more >
PyTorch 1.9.0 Now Available - Exxact Corporation
Legacy tensor constructors and Tensor.new no longer support ... UserWarning: Single-Process Multi-GPU is not the recommended mode for DDP.
Read more >
Changelog — PyTorch Lightning 1.8.5 documentation
Deprecated num_processes , gpus , tpu_cores, and ipus from the Trainer constructor in favor of using the accelerator and devices arguments (#11040).
Read more >
warning: invocation with class name is deprecated php, python ...
PHP 4 style constructors (methods that have the same name as the class they ... Legacy autograd function with non-static forward method is...
Read more >
Multi-worker training with Estimator | TensorFlow Core
Invoking this method in graph mode retains the legacy behavior of the ... (from tensorflow.python.autograph.pyct.static_analysis.liveness) is deprecated and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found