vision: pil_to_tensor() raises a "NumPy array is not writeable" warning

🐛 Describe the bug

Steps to reproduce:

import torchvision
from PIL import Image
img = Image.open("test/assets/encode_jpeg/grace_hopper_517x606.jpg")
torchvision.transforms.functional.pil_to_tensor(img)

Raises:

./vision/torchvision/transforms/functional.py:177: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at  ./torch/csrc/utils/tensor_numpy.cpp:187.)
  img = torch.as_tensor(np.asarray(pic))

https://github.com/pytorch/vision/blob/f483e71b512e0f9dd5d6eb531fb602581ffb39a6/torchvision/transforms/functional.py#L177

There are a few options including copying the data, suppressing the warnings etc (see discussion below). The person who picks this up can comment on this issue which solution they are going for.

Versions

Latest main (f749835057ec16886f16d5533928e269860f1760)

cc @vfdev-5 @datumbox

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (15 by maintainers)

Most upvoted comments

As discussed offline with @datumbox I am going to proceed with the copy option for now.

Sounds good to me. You can implement it like this if you want. Please make sure you pass True when you update the code inside the references folder because there we want to avoid the warning. 😃

false, so the default behaviour wouldn’t change.

I would like to pick up this task