Cropped Tensor mixed result

frp_alv: <p>Hi, I’m trying to crop a section of a 4 component tensor [batch, channel, height, weight] that was originally a numpy image. I am using numpy-like indexing, here’s the code:</p>
<pre><code>    # img_mod is a pytorch tensor that was a numpy array
    b = img_mod.shape[0]
    c = img_mod.shape[1]
    h = img_mod.shape[2]
    w = img_mod.shape[3]

    # Trying to crop the first quadrant
    img_mod_frag = img_mod[:, :, 0:h//2, 0:w//2]

    # Reshape the components to make the tensors displayable
    # Display img_mod
    plt.imshow(img_mod.reshape(h, w, c))
    plt.title("img_mod")
    plt.show()

    # Display img_mod_frag
    plt.imshow(img_mod_frag.reshape(h//2, w//2, c))
    plt.title("img_mod_frag")
    plt.show()
</code></pre>
<p>My intention is to crop the first quadrant of the image but these are the results:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/5/d5d3c68fb66b46e0bb9067ab720c2a2f489a5f4d.png" data-download-href="https://discuss.pytorch.org/uploads/default/d5d3c68fb66b46e0bb9067ab720c2a2f489a5f4d" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/5/d5d3c68fb66b46e0bb9067ab720c2a2f489a5f4d_2_690x196.png" alt="image" data-base62-sha1="uvBqyLZCRyfPjD6xjvfHpGJLoxn" width="690" height="196" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/5/d5d3c68fb66b46e0bb9067ab720c2a2f489a5f4d_2_690x196.png, https://discuss.pytorch.org/uploads/default/original/3X/d/5/d5d3c68fb66b46e0bb9067ab720c2a2f489a5f4d.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/5/d5d3c68fb66b46e0bb9067ab720c2a2f489a5f4d.png 2x" data-dominant-color="8781AC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">704×200 101 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>The resulting image is ‘mixed’, I don’t know if this is the correct behaviour of tensors. Then, I would like to process the fragment in a simple CNN, but even if I crop the image properly transforming it to numpy and indexing It, when I process It with my model() function It ‘mixes’ the fragment too. Like the img_mod_frag case.</p>
<p>My question is if this is the correct way to crop propperly a tensor quadrant. If it’s not, What’s the correct way? Am I doing something wrong? or should I train my model with these ‘mixed’ fragments?</p>
<p>Thanks.</p>

russellizadi: <p>You should always be careful with the <code>reshape</code> method since it works respecting the order of dimensions! You can either use <code>transpose</code> or <code>permute</code> to change the order of dimensions. Regarding the cropping tensors, I suggest you take a look at <a href="https://pytorch.org/docs/stable/torchvision/transforms.html" rel="noopener nofollow ugc">torchvision/transforms</a>.</p>
<p>Here’s one example using the <code>permute</code> method:</p>
<pre><code class="lang-auto">from PIL import Image
import matplotlib.pyplot as plt
import torch
import torchvision.transforms as transforms

image = Image.open("Lenna.png")
img_mod = transforms.ToTensor()(image).unsqueeze(0)

# img_mod is a pytorch tensor that was a numpy array
b = img_mod.shape[0]
c = img_mod.shape[1]
h = img_mod.shape[2]
w = img_mod.shape[3]

# Trying to crop the first quadrant
img_mod_frag = img_mod[:, :, 0:h//2, 0:w//2]

# Reshape the components to make the tensors displayable
# Display img_mod
fig = plt.figure()
ax = fig.add_subplot(1, 2, 1)
ax.imshow(img_mod[0].permute(1, 2, 0))
plt.title("img_mod")

# Display img_mod_frag
ax = fig.add_subplot(1, 2, 2)
ax.imshow(img_mod_frag[0].permute(1, 2, 0))
plt.title("img_mod_frag")

plt.savefig("Lenna2.png")
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/8/7854742a0a9022612272b92cf2cc3d5c09fa227d.png" alt="Lenna2" data-base62-sha1="haueK9LiMktQkfrjQXJaZ6oWhkN" width="432" height="288"></p>

frp_alv: <p>Thank you so much! I clearly see how It works now.</p>