UserWarning: The given NumPy array is not writeable

Zhaoyi-Yan: <pre><code class="lang-auto">/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.
</code></pre>

Zhaoyi-Yan: <p>Whenever a new epoch starts, the warning appears, which is disgusting.</p>

Zhaoyi-Yan: <p>It is caused by <code>img = transforms.ToTensor()(img)</code></p>

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

liang_qu: <p>What you said is awesome, according to your method, I have solved the problem</p>

Gaofeng: <ul>
<li>How to solve this problem?</li>
</ul>

Zhaoyi-Yan: <pre><code class="lang-python"># Instead of using 
# img = transforms.ToTensor()(img)

# Use this:
img = transforms.ToTensor()(np.array(img))
</code></pre>
<p><code>ToTensor()</code> recevies PIL objects or numpy arrays. So explictly changes the pil array to numpy array is fine.</p>
<p>Ref:<br>
</p><aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/vision/issues/2194" target="_blank" rel="nofollow noopener">github.com/pytorch/vision</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/vision/issues/2194" target="_blank" rel="nofollow noopener">Float PILImage not converted as writeable</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-05-07" data-time="18:54:44" data-timezone="UTC">06:54PM - 07 May 20 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/lukasHoel" target="_blank" rel="nofollow noopener">
          <img alt="lukasHoel" src="https://avatars2.githubusercontent.com/u/36296213?v=4" class="onebox-avatar-inline" width="20" height="20">
          lukasHoel
        </a>
      </div>
    </div>
  </div>
</div>

<div class="github-row">
  <p class="github-content">🐛 Bug
When we have a float PIL-Image (e.g. mode='F'), e.g. for the purpose of applying transforms to it, and finally convert...</p>
</div>

<div class="labels">
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


a63312001: <p>This solution saves my life. Thanks!</p>

Vimos: <p>I also met a similar problem on <code>pytorch 1.5.1</code>. The case is a bit different. I am trying to read from <code>lmdb</code> via <code>msgpack</code>. I have to change the code from:</p>
<pre><code class="lang-auto">img_dump = msgpack.loads(dump, raw=False)
</code></pre>
<p>to</p>
<pre><code class="lang-auto">img_dump = {k: np.copy(v) for k, v in msgpack.loads(dump, raw=False).items()}
</code></pre>
<p>using <code>numpy.copy</code>.</p>

hinken: <p>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.</p>
<p>pic = Image.open(fn).convert(mode=‘L’)<br>
tensor = tvt.functional.pil_to_tensor(img).type(torch.ByteTensor)</p>
<p>=&gt; the warning, ending with:<br>
(Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:141.)<br>
img = torch.as_tensor(np.asarray(pic))</p>

naveenvemy: <p>This warning also occurs when processing the raw dataset downloaded using MNIST class in torchvision.datasets</p>
<p>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.</p>

visionscaper: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>, similar to <a class="mention" href="/u/naveenvemy">@naveenvemy</a>, I observed that this warning occurs when loading the fashion-MNIST dataset as follows:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>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.</p>
<p>I tried to fix it as follows, but it didn’t work:</p>
<pre><code class="lang-auto"># 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))
</code></pre>
<p>Do you have any suggestions?</p>

visionscaper: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> 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 <a class="mention" href="/u/naveenvemy">@naveenvemy</a> suggested in <code>read_sn3_pascalvincent_tensor</code> of <code>mnist.py</code>.</p>
<p>The <code>read_sn3_pascalvincent_tensor</code> function ends with:</p>
<p><code>return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)</code></p>
<p>which generates the warning. Should <code>copy=True</code>?</p>

ptrblck: <p>Good catch! CC <a class="mention" href="/u/pmeier">@pmeier</a> do you think this is something which should be fixed or is the warning expected in this line of code?</p>

ArshadIram: <p>Yes, the same error occurs while loading the MNIST dataset as well.</p>

pmeier: <p>I think this has been fixed in <a href="https://github.com/pytorch/vision/pull/4184" rel="noopener nofollow ugc">this PR</a>.</p>

tanzh-lamda: <p>This indeed helps. Thanks.</p>

kaykayi_tony: <p>wow, I changed <code>copy=True</code>, then the warning disappeared! thx!</p>

heitorrapela: <p>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:</p>
<p>self.transform(image=np.array(imgs, copy=True), …)</p>

lolbus: <p>I followed your method and the warning and crashes disappeared.</p>
<p>Can you explain why this happened and whether if there is any side effect if we follow this method by creating a copy?</p>
<p>I was just doing a simple colorjitter transform, why do I end up with a non writeable numpy?</p>
<p>thanks alot</p>