Issue
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:
TypeError: Invalid dimensions for image data
The .shape
of the tensor is:
torch.Size([3, 244, 244])
How do I display a PyTorch tensor as an image?
Solution
Given a Tensor
representing the image, use .permute()
to put the channels as the last dimension:
plt.imshow( tensor_image.permute(1, 2, 0) )
Note: permute
does not copy or allocate memory, and from_numpy()
doesn't either.
Answered By - Tom Hale
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.