I have a RGB image tensor as (3,H,W), but the plt.imshow() can not show RGB image with this shape. I want to change the tensor to (H,W,3). How can I do that, is pytorch function .view() can do that?
            Asked
            
        
        
            Active
            
        
            Viewed 1.2k times
        
    7
            
            
        - 
                    Does this answer your question? [What is the correct way to change image channel ordering between channels first and channels last?](https://stackoverflow.com/questions/43829711/what-is-the-correct-way-to-change-image-channel-ordering-between-channels-first) – Deusy94 Apr 26 '22 at 11:49
- 
                    @Deusy94 Basically, answer this question. But more detailly, this question is about tensor rather than numpy ndarray – PinkR1ver Apr 27 '22 at 05:00
3 Answers
9
            
            
        Find the method. use pytorch permute() method, see details: https://www.geeksforgeeks.org/python-pytorch-permute-method/
code:
image.permute(1, 2, 0)
 
    
    
        PinkR1ver
        
- 402
- 1
- 4
- 13
5
            An alternative to using torch.Tensor.permute is to apply torch.Tensor.movedim:
image.movedim(0,-1)
Which is actually more general than image.permute(1,2,0), since it works for any number of dimensions. It has the effect of moving axis=0 to axis=-1 in a sort of insertion operation.
Or equivalently with Numpy, using np.moveaxis:
 
    
    
        Ivan
        
- 34,531
- 8
- 55
- 100
2
            
            
        Please refer to this question
img_plot = img.numpy().transpose(1, 2, 0)
plt.imshow(img_plot)
 
    
    
        YScharf
        
- 1,638
- 15
- 20