UserWarning: The given NumPy array is not writeable

 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.

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


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.


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.



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)


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 =‘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))


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 file in the API itself. One of the solutions presented here can be implemented inside this function.

1 Like

@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,
    test_data = tv.datasets.FashionMNIST(dataset_path,

    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

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?


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

I think this has been fixed in this PR.