Negative strides in tensor error

Samuel_Bachorik: <p>Hi, when I use numpy.flip()  on my array and then i try to create from this array torch tensor with <code>torch.from_numpy(x)</code><br>
It says</p>
<pre><code class="lang-auto">ValueError: At least one stride in the given numpy array is negative, and tensors with negative strides are not currently supported. (You can probably work around this by making a copy of your array  with array.copy().) 
</code></pre>
<p>Why is this happening ? I see there are some solutions on internet but this never happened to me yet and I was using numpy.flip() a lot.<br>
When I use min() max() on this array there is no negative number so why is this happening ?</p>
<p>example code:<br>
Just normal numpy array, flip it and it crashes.</p>
<pre><code class="lang-auto">import numpy
import torch
x = numpy.zeros((12,3,48,48))

def _augmentation_flip(image_np, label_np):
    aug_img = numpy.flip(image_np, 1)
    aug_label = numpy.flip(label_np, 1)

    return aug_img, aug_label

x,y = _augmentation_flip(x,x)

torch.from_numpy(x)
</code></pre>

ptrblck: <aside class="quote no-group" data-username="Samuel_Bachorik" data-post="1" data-topic="134287">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/samuel_bachorik/48/56232_2.png" class="avatar"> Samuel_Bachorik:</div>
<blockquote>
<p>When I use min() max() on this array there is no negative number so why is this happening ?</p>
</blockquote>
</aside>
<p>Not the values in the array/tensor are negative but the strides, which are used to access elements in the tensor via indexing.<br>
Negative strides are not supported in PyTorch, so you would have to create a copy first:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.randn(10, 10)
x[:, ::-1]
# &gt; ValueError: step must be greater than zero
x.numpy()[:, ::-1] # works

a = np.random.randn(10, 10)
a.strides
# &gt; (80, 8)
a = np.flip(a)
a.strides
# &gt; (-80, -8)

x = torch.from_numpy(a)
# &gt; ValueError: At least one stride in the given numpy array is negative, and tensors with negative strides are not currently supported. (You can probably work around this by making a copy of your array  with array.copy().) 

x = torch.from_numpy(a.copy()) # works
x.stride()
# &gt; (10, 1)
</code></pre>

Samuel_Bachorik: <p>Now I understand thank you a lot !</p>

d0uwe: <p>For a negative stride of 2 (so -2), you can also do as a workaround:</p>
<p>tensor[:, :, (not res.shape[-1] % 2)*1::2]</p>
<p>No need to copy etc.</p>

whlteXbread: <p>for what it’s worth, this error can happen if you’re trying to make a tensor out of an image that has been “rotated” via metadata adjustment.</p>
<p>for example:</p>
<ol>
<li>in macOS, select an image and push CMD+L. this doesn’t rewrite any raster data, it just changes the orientation of the image in the image’s metadata.</li>
<li>load the image into a python script via <code>imageio</code>: <code>import imageio; image = imageio.imread("/path/to/image.jpg")</code> — imageio respects the image’s metadata and implements the rotation using a negative stride in the appropriate place</li>
<li>try to make the image into a tensor: <code>import torch; tensor = torch.Tensor(image)</code>
</li>
</ol>
<p>this should raise the negative stride error.</p>
<p>if you write the <code>image</code> to disk and try the above procedure again, everything should be fine.</p>

max-wild: <p>Would you be able to elaborate on this? I think I see what you’re getting at but I’m not sure how you’d write it out</p>

d0uwe: <p>I meant more that with this syntax, you can extract the same values as that you would get by using a negative strides, however the order of the values is reversed now:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/5/b/5b9e425b0c472c2b874be48f9b416b4e3f0d1a86.png" alt="image" data-base62-sha1="d4uw9rMsPcYVTOidVkZkYl10LCC" width="458" height="176"></p>