Visualize image from pixels

Hello! I have a pytorch tensor, call it a, which should contain the pixel values of an image. Here it is how it looks like:

tensor([[[154., 143., 137.,  ..., 136., 136., 136.],
         [140., 155., 143.,  ..., 136., 136., 136.],
         [131., 153., 138.,  ..., 136., 136., 136.],
         ...,
         [163., 170., 181.,  ..., 189., 172., 152.],
         [172., 176., 184.,  ..., 204., 199., 190.],
         [178., 179., 184.,  ..., 213., 218., 224.]],

        [[207., 196., 194.,  ..., 204., 204., 204.],
         [196., 213., 202.,  ..., 204., 204., 204.],
         [193., 216., 204.,  ..., 204., 204., 204.],
         ...,
         [194., 197., 201.,  ..., 213., 207., 192.],
         [200., 197., 194.,  ..., 211., 203., 193.],
         [205., 196., 190.,  ..., 208., 203., 203.]],

        [[223., 212., 211.,  ..., 223., 223., 223.],
         [211., 227., 218.,  ..., 223., 223., 223.],
         [208., 231., 218.,  ..., 223., 223., 223.],
         ...,
         [214., 180., 132.,  ..., 177., 201., 204.],
         [178., 156., 121.,  ..., 169., 176., 172.],
         [154., 141., 118.,  ..., 166., 160., 158.]]])

How can I turn this numbers back into the image? Thank you!

x is a tensor
y = x.numpy()

from matplotlib import pyplot as plt 
plt.imshow(y) % y should be 2D.

Hi,

Your image dimension is 3xMxN. To use imshow, the image dimension should be (M,N,3) if the image data is 3 dimensional. In addition the values should be in range [0,1] or [0,255]. In your case, it looks like that the values fall in the latter range. Hence, no issues with the pixel values. But, you still have to transpose. Following would be a solution.

import matplotlib.pyplot as plt
import numpy as np

npimg = img.numpy() # img is your tensor
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()

Thanks

Thank you for your reply. I don’t really get the right image back. This is what I get:
img
which seems to be mostly white.

Hi,

This image contains pixels where most of them are white. It means that pixel value is (255,255,255). As from the tensor values that you have shared, there are lots of other values as well. Check whether you are loading the right image. And check whether you are changing the values.

Thanks

All I did was to use the lines of code that you provided, replacing img with the tensor that I show above. I am not sure where I could have gone wrong.

Hi,

Could you please share where are you loading the tensor, then I can have a look.

Thanks

This is how (part of) my code looks like:

class ImageDataset(Dataset):
    def __init__(self, csv_file, root_dir):
        self.file_source = pd.read_csv(csv_file)
        self.root_dir = root_dir

    def __len__(self):
        return len(self.file_source)

    def __getitem__(self, idx):
        img_name = os.path.join(self.root_dir,
                                self.file_source.iloc[idx, 1])
        image = io.imread(img_name)
        image = image.transpose(2, 0, 1)
        image = torch.from_numpy(image)
        image = image.float()
        
        return image

rocket_dataset = ImageDataset(csv_file='./rocket_images_reduced/rockets.csv',
                              root_dir='./rocket_images_reduced/train/')

img_test = rocket_dataset[0]

npimg = img_test.numpy() # img is your tensor
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()

What is the size of img_test tensor?

print(img_test.size())

torch.Size([3, 64, 64])

Hi,

try this and check.

import matplotlib.pyplot as plt
import numpy as np

npimg = img.numpy() # img is your tensor
npimg = npimg / 255 # add this line
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()

Thanks

1 Like

It’s working now! Thank you so much!