UserWarning: The given NumPy array is not writeable

/pytorch/torch/csrc/utils/tensor_numpy.cpp:141:
 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.
7 Likes

Whenever a new epoch starts, the warning appears, which is disgusting.

6 Likes

It is caused by img = transforms.ToTensor()(img)

1 Like

How are you loading/creating the numpy array or image?
Could you check the writeable flag via print(arr.flags['WRITEABLE'])?
If it’s set to False, you should create a copy of it as suggested by the warning message.

8 Likes

What you said is awesome, according to your method, I have solved the problem

  • How to solve this problem?
# Instead of using 
# img = transforms.ToTensor()(img)

# Use this:
img = transforms.ToTensor()(np.array(img))

ToTensor() recevies PIL objects or numpy arrays. So explictly changes the pil array to numpy array is fine.

Ref:

13 Likes

This solution saves my life. Thanks!

1 Like

I also met a similar problem on pytorch 1.5.1. The case is a bit different. I am trying to read from lmdb via msgpack. I have to change the code from:

img_dump = msgpack.loads(dump, raw=False)

to

img_dump = {k: np.copy(v) for k, v in msgpack.loads(dump, raw=False).items()}

using numpy.copy.

1 Like

Still not fixed in pyTorch 1.6.0 and torchvision 0.7.0, trying to load a PIL image as a Tensor succeeds but prints this warning. My guess is that Torchvision transform pil_to_tensor tries to create a Tensor that inherits the underlying numpy storage from the PIL Image, which is marked as read-only, and since a Tensor can’t be read-only, it has to print the warning.

pic = Image.open(fn).convert(mode=‘L’)
tensor = tvt.functional.pil_to_tensor(img).type(torch.ByteTensor)

=> the warning, ending with:
(Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:141.)
img = torch.as_tensor(np.asarray(pic))

2 Likes

This warning also occurs when processing the raw dataset downloaded using MNIST class in torchvision.datasets

The processing function is read_sn3_pascalvincent_tensor() and it is defined inside mnist.py file in the API itself. One of the solutions presented here can be implemented inside this function.

2 Likes

@ptrblck, similar to @naveenvemy, I observed that this warning occurs when loading the fashion-MNIST dataset as follows:

def load_data():
    transform = tv.transforms.ToTensor()

    training_data = tv.datasets.FashionMNIST(dataset_path,
                                             train=True,
                                             download=True,
                                             transform=transform)
    test_data = tv.datasets.FashionMNIST(dataset_path,
                                         train=False,
                                         download=True,
                                         transform=transform)

    return training_data, test_data

What is interesting is that the warning doesn’t occur with torch 1.8.1 + torchvision 0.9.1, but does occur with torch 1.9.0 + torchvision 0.10.0.

I tried to fix it as follows, but it didn’t work:

# Fix given NumPy array is not writeable warning
class ToTensor(tv.transforms.ToTensor):

    def __call__(self, pic):
        return super().__call__(np.array(pic, copy=True))

Do you have any suggestions?

@ptrblck having looked in more detail at the problem, I now see that the warning is generated at image load time, not at the moment of transform; as @naveenvemy suggested in read_sn3_pascalvincent_tensor of mnist.py.

The read_sn3_pascalvincent_tensor function ends with:

return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)

which generates the warning. Should copy=True?

Good catch! CC @pmeier do you think this is something which should be fixed or is the warning expected in this line of code?

2 Likes

Yes, the same error occurs while loading the MNIST dataset as well.

2 Likes

I think this has been fixed in this PR.

1 Like

This indeed helps. Thanks.

wow, I changed copy=True, then the warning disappeared! thx!

1 Like

I was having the same warning after updating the libraries in another environment, so when I used the transform from Albumentations, the copy=True solution worked fine before calling the function:

self.transform(image=np.array(imgs, copy=True), …)

I followed your method and the warning and crashes disappeared.

Can you explain why this happened and whether if there is any side effect if we follow this method by creating a copy?

I was just doing a simple colorjitter transform, why do I end up with a non writeable numpy?

thanks alot