Hi Aleemsidra!



 aleemsidra:

However, I am not able to understand  inp.numpy().transpose((1,2,0)), what transpose is doing here.
…
However, the corresponding tensor shape is torch.Size([4, 3, 224, 224]).


You should get an error here.  You need to pass four axes to numpy’s
transpose() to transpose a 4-d tensor.


Apart from that, the shape of the tensor image is 3,224,224. but when it is being transformed to ndarray why the shape is being changed to (228, 906, 3). Should it become 224, 224, 3.


I have no idea where your (228, 906, 3) is coming from.  I get
(244, 244, 3), which is what you expect and want.  (matplotlib’s
imshow() wants the three color channels as the last dimension,
rather than the first.  That’s the purpose of the transpose().)
Here’s an illustration of these points:
>>> import torch
>>> torch.__version__
'1.7.1'
>>> inp = torch.randn (4, 3, 224, 224)
>>> inp.shape
torch.Size([4, 3, 224, 224])
>>> inp.numpy().transpose ((1, 2, 0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: axes don't match array
>>> inp[0].shape
torch.Size([3, 224, 224])
>>> inp[0].numpy().transpose ((1, 2, 0)).shape
(224, 224, 3)

Best.
K. Frank