Torch.from_numpy not support negative strides

Hsuxu: <p>When use torch.from_numpy from the Ndarray with negative strides, there is  a runtime error<br>
’RuntimeError: some of the strides of a given numpy array are negative.'<br>
For example,</p>
<pre><code>import numpy as np
x=np.random.random(size=(32,32,7))
torch.from_numpy(np.flip(x,axis=0))

RuntimeError: some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future releases.
</code></pre>
<p>Same error with np.rot90()</p>

chenyuntc: <p>how about</p>
<pre><code class="lang-python">torch.from_numpy(np.flip(x,axis=0).copy())
</code></pre>

Hsuxu: <p>Thanks for your recommendation, I have solve this problem.</p>

youkaichao: <p>also works for me, thanks! though i don;t know why…</p>

chenyuntc: <p><code>ndarray.copy()</code> will alocate new memory for numpy array which make it <em>normal</em>, I mean the stride is not negative any more.</p>

youkaichao: <p>Excuse me, I’m puzzled about the word ‘normal’, what do you mean by ‘normal<br>
numpy array’?</p>

chenyuntc: <p>it’s like something <code>contiguous</code> in tensor. the data is stored orderly.</p>

Eric_K: <p>hello，</p>
<p>I just don’t understand what is “negative stride” means, could you please interpret it for me? Thank you very much!!</p>

Zhiqiu_Lin: <p>This means that your numpy array has undergone such operation:<br>
<code>image = image[..., ::-1]</code><br>
I guess this has something to do with how numpy array are stored in memory, and unfortunately PyTorch doesn’t currently support numpy array that has been reversed using negative stride.</p>
<p>A simple fix is to do</p>
<pre><code class="lang-auto">image = image[..., ::-1] - np.zeros_like(image)
</code></pre>

Eric_K: <p>Ok, I understand it. There is a function in numpy like contiguous in pytorch. You can try it. Thanks.</p>
<p><a href="https://maas.mail.163.com/dashi-web-extend/html/proSignature.html?iconUrl=http%3A%2F%2Fmail-online.nosdn.127.net%2Fa74f0bc6760ea9d1d8b5a113be800853.jpg&amp;name=%E4%BF%A1%E5%B7%A5%20%E5%88%98%E8%93%AC%E5%8D%9A&amp;uid=example%40163.com&amp;ftlId=3&amp;items=%5B%22%E9%82%AE%E7%AE%B1%EF%BC%9Apengbo18555%40163.com%22%5D" rel="nofollow noopener"><br>
</a><br>
<img width="38" height="38" src="http://mail-online.nosdn.127.net/a74f0bc6760ea9d1d8b5a113be800853.jpg"><br>
信工 刘蓬博</p>
<p>邮箱：pengbo18555@163.com</p>
<p>签名由 <a href="https://mail.163.com/dashi/dlpro.html?from=mail88" rel="nofollow noopener">网易邮箱大师</a> 定制</p>

Gtheodorou: <p>If you don’t want to flip the image, if for example you have already trained a network with un-flipped images, then you can save and load the image before passing it for inference.</p>

Shoaib_Ahmed_Siddiqu: <p>I think this is a more elegant solution to the problem leveraging the PyTorch API.</p>
<p>torch.flip(torch.from_numpy(x), dims=(0,))</p>

Tsingwaa_Tsang: <p>Thanks! It does works.  <img src="https://discuss.pytorch.org/images/emoji/apple/partying_face.png?v=9" title=":partying_face:" class="emoji" alt=":partying_face:"></p>

RicoSuaveGuapo: <p>One can also pin the memory again by</p>
<pre><code class="lang-auto">x = np.flip(x,axis=0)
torch.from_numpy(np.ascontiguousarray(x))
</code></pre>