Pytorch tranforms give me weird results

sidd.suresh97: <pre><code class="lang-auto">tfms = transforms.Compose([
    transforms.Resize(224),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    #transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])
</code></pre>
<p>I’m trying to resize my image to 224,224 and then centre crop it. Here is the original image and the image after the transform. Why are there 9 images after the transform is applied?</p>
<p>Image after the transform:-<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/7/4/7417e0016a8b82c73d611629b1847f39627ee1b6.png" alt="Screenshot 2020-02-11 at 8.06.02 PM" data-base62-sha1="gz0xogiWUsVdMN468IL8hs3IHTE" width="600" height="498"></p>

sidd.suresh97: <p>Image before the transform<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/f/4fa128dfb39f5a4ee2cc1f1d9c2e1f116288961e.png" data-download-href="https://discuss.pytorch.org/uploads/default/4fa128dfb39f5a4ee2cc1f1d9c2e1f116288961e" title="Screenshot 2020-02-11 at 8.03.14 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/f/4fa128dfb39f5a4ee2cc1f1d9c2e1f116288961e_2_690x495.png" alt="Screenshot 2020-02-11 at 8.03.14 PM" data-base62-sha1="bmr0c7bMebUjktOIWKIlQNXRQwC" width="690" height="495" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/f/4fa128dfb39f5a4ee2cc1f1d9c2e1f116288961e_2_690x495.png, https://discuss.pytorch.org/uploads/default/original/3X/4/f/4fa128dfb39f5a4ee2cc1f1d9c2e1f116288961e.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/f/4fa128dfb39f5a4ee2cc1f1d9c2e1f116288961e.png 2x" data-dominant-color="3E4241"><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">Screenshot 2020-02-11 at 8.03.14 PM</span><span class="informations">694×498 75.3 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>

ptrblck: <p>Could you print the shape and type of the image?<br>
The result looks interleaved, which would happen, if you e.g. use a <code>view</code> instead of <code>permute</code> to swap the axes.</p>

sidd.suresh97: <p>This is the code:-</p>
<pre><code class="lang-auto">tfms = transforms.Compose([
    transforms.Resize(224),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    #transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])
    object_img_tensor = tfms(PIL.Image.fromarray(object_image))
    context_img_tensor = tfms(PIL.Image.fromarray(context_image))
  
  
</code></pre>
<p>Type of image before tranform is applied :- &lt;class ‘numpy.ndarray’&gt;<br>
Shape of the image before tranform is applied :- (480, 640, 3)<br>
Type of image after tranform is applied :- &lt;class ‘torch.Tensor’&gt;<br>
Shape of the image after tranform is applied :- torch.Size([3, 224, 224])</p>

ptrblck: <p>The shapes look alright.<br>
Could you post the code you are using to visualize the image?</p>

sidd.suresh97: <p>plt.imshow(object_img_tensor.reshape(224,224,3))<br>
plt.show()</p>

vgsprasad: <p>Data was interleaved with ‘reshape’ function. Instead of ‘reshape’ function, use ‘permute’ function to shuffle the dimensions. Do like this</p>
<p>plt.imshow(object_img_tensor.permute(1,2,0))<br>
plt.show()</p>

sidd.suresh97: <p>Ok this works. I’m just curious as to how the reshape function interleaves the data?</p>

vgsprasad: <p>Let X be an input array of dimension [3][224][224]. When X is reshaped, it is first flattened as X[0][0][[0], X[0][0][1],…,X[0][0][223],X[0][1][0],…,X[0][223][223],X[1][0][0],…,X[2][223][223]. After that 3 elements are taken as group and forms an output array Y of dimension [224][224][3]. Hence, the output pixels are as Y[0][0][0] = X[0][0][0], Y[0][0][1] = X[0][0][1], Y[0][0][2] = X[0][0][2], Y[0][1][0] = X[0][0][3], …, Y[223][223][2] = X[2][223][223].</p>