How to visualize/display a data image in 'torch.FloatTensor' type

I have an image data named Img, and it is in ‘torch.FloatTensor’ format, I tried to use torchvision.transforms and/or matplotlib to display i, but I failed.
Can anyone tell me how I can do it?
Here are some extra info regarding my Image data:

print(type( Img ))
<class ‘torch.FloatTensor’>

print((Img.size()))
(1L, 3L, 128L, 128L)

I tried:

import torchvision.models as models
import torchvision.transforms as transforms
import torch.nn as nn
import torch

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

pilTrans = transforms.ToPILImage()
pilImg = pilTrans(Img)

or

imgplot = plt.imshow(Img)

But none of them gets me anywhere.

Thank you for your!

1 Like

you can convert it to a numpy array using Img.numpy() and visualize it with a standard numpy recipe:

https://matplotlib.org/users/image_tutorial.html#plotting-numpy-arrays-as-images

6 Likes

Than you!
I thought maybe there is an easier way to display torch.FloatTensor version of data.
Yeah I made it work by converting it to numpy.ndarray

pilTrans = transforms.ToTensor()
plt.imshow(pilTrans[1,:])

Can you try this code and see if this works?

1 Like