I want to display a single image loaded using ImageLoader and stored in a PyTorch Tensor. When I try to display it via plt.imshow(image) I get:
ImageLoader
Tensor
plt.imshow(image)
TypeError: Invalid dimensions for image data
The .shape of the tensor is:
.shape
torch.Size([3, 244, 244])
How do I display a PyTorch tensor as an image?
Given a Tensor representing the image, use .permute() to put the channels as the last dimension:
.permute()
plt.imshow( tensor_image.permute(1, 2, 0) )
Note: permute does not copy or allocate memory, and from_numpy() doesn't either.
permute
from_numpy()
1.4m articles
1.4m replys
5 comments
57.0k users