How to visualize the output tensor image from an autoencoder of dtype = torch.float32

Once my autoencoder is trained, its output is a tensor of shape [bs, h, w], where h and w are the height and width of the input image

i want to visualize an output image, so i choose the 1st one
img = output[1, :, :]
now the shape og img is [h, w]
and its dtype is torch.float32

Now i have 2 options here,

  • i can convert the output pixel values to uint8 [0 to 255]
    cv2.imshow( (img.numpy()*255).astype(np.uin8) )

  • or i can simply use the imshow of matplotlib which does not convert the pixel values to 0 to 255 uint8
    plt.imshow(img, cmap = ‘gray’)

Which one is more appropriate and shows the image without losing much inforamtion ?

Thanks in advance :slight_smile:

Hey,

Much of what you are asking is dependent on the data you have (not just it’s type, but it’s sparseness and length) and on the needs of your visualization.

Given that you have reduced it to these 2 options (why? maybe you want to use an external tool, so another option would be - torchvision.utils. save_image and export it to that handler) I can only say that the fact you are using cmap=‘gray’ in the second option, means that you don’t have an RGB(A) format of the data and that you are doing some transformation (or conversion) to the data on the second option as well(from the documentation: “The Colormap instance or registered colormap name used to map scalar data to colors”).