How to convert an image with dimensionality HxWxC to CxHxW

AlbertZhang: <p>Hi, I’m stuck in how to convert an image with dimensionality HxWxC to CxHxW,  where C is the channel, H is the height and W is the width.<br>
Much thanks for your help.</p>

AlbertZhang: <p>Well, I get the answer by reading the document. Thanks for your attention.</p>

richard: <p>For posterity:<br>
<code>torch.permute(2, 0, 1)</code> will do the job</p>

AlbertZhang: <p><a class="mention" href="/u/richard">@richard</a> Thanks for your excellent suggestion. I converted the tensor to numpy and then used <code>numpy.transpose</code>, but <code>torch.permute</code> will do more excellent job.<img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=5" title=":grinning:" class="emoji" alt=":grinning:"></p>

AlbertZhang: <p><a class="mention" href="/u/richard">@richard</a> Hi, richard. Is there some operation in torch that can rotate tensors just like <code>rot90</code> in numpy? Converting between tensors(or Variables) and numpy arrays may be time consuming and cumbersome so I just would not like to use numpy <img src="https://discuss.pytorch.org/images/emoji/apple/thinking.png?v=5" title=":thinking:" class="emoji" alt=":thinking:"></p>

Fnjn: <p>If you are working on data augmentaion, I recommend trying out torchvision.transforms.RandomRotation. Otherwise, it’s easier to work around in Pillow.</p>

AlbertZhang: <p><a class="mention" href="/u/fnjn">@Fnjn</a> Umm, I am not working on data augmentation. It’s something on a custom function.</p>
<p>EDIT: But I think I could use the source code of <code>torchvision.transforms.RandomRotation</code> for reference. Thanks a lot.</p>

richard: <p><a class="mention" href="/u/albertzhang">@AlbertZhang</a> not that I know of, but it could be something interesting to add to the API. Were you thinking of arbitrary rotations (I’m not sure how that would make sense…), or just rot90/270/180?</p>

AlbertZhang: <p><a class="mention" href="/u/richard">@richard</a> just rot90/180/270. I don’t think arbitrary rotation will make sense for matrices.</p>
<p>EDIT: BTW, what would the derivative of such rotation operation with respect to the matrix it handling on be like? A corresponding rotation operation? It is beyond my understanding now. <img src="https://discuss.pytorch.org/images/emoji/apple/thinking.png?v=5" title=":thinking:" class="emoji" alt=":thinking:"></p>

cakeeatingpolarbear: <p>You can read about <a href="https://arxiv.org/abs/1506.02025" rel="nofollow noopener">Spatial Transformer Networks</a></p>

AlbertZhang: <p><a class="mention" href="/u/cakeeatingpolarbear">@cakeeatingpolarbear</a> It may be helpful. Thanks a lot.</p>

richard: <p><a class="mention" href="/u/albertzhang">@AlbertZhang</a> the derivative would be a rotation in the opposite direction (of the gradOutput).</p>

AlbertZhang: <p><a class="mention" href="/u/richard">@richard</a> Sorry for the delaying of reply. Adding the API for tensor rotation would be great. Thanks so much for your help and great job.</p>

richard: <p>I’ve opened an issue for that here: <a href="https://github.com/pytorch/pytorch/issues/6271">https://github.com/pytorch/pytorch/issues/6271</a> , hopefully someone will get to it.</p>

mayujie_jozek: <p>simple and efficient <img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=10" title=":+1:" class="emoji" alt=":+1:"></p>