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.

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

See original GitHub issue

🐛 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

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jdsgomescommented, Oct 7, 2021

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

1reaction
datumboxcommented, Oct 7, 2021

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. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

UserWarning: The given NumPy array is not writeable
This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to...
Read more >
The given NumPy array is not writeable, and PyTorch does not ...
Why i'm getting the message "UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors."?
Read more >
Warning "The given NumPy array is not writeable" raised by ...
Due to a possible bug of torchvision.datasets.MNIST, the first time this downloads its data the following UserWarning is raised: The given ...
Read more >
the given numpy array is not writeable, and pytorch ... - You.com
Sidenote : the warning about writable numpy arrays is completely unrelated, it just means that pytorch will possibly override the "non-writable" data of...
Read more >
[Torchvision] Image Random Transforming Baseline - Kaggle
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:7: UserWarning: The given NumPy array is not writeable, and PyTorch does not support ...
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