Save variable as image & difference between variable and tensor

I am running code on torch==0.3.1 & torchvision==0.2.1.

(1) I have output of CNN stored in “var_1” having torch.Size([3, 512, 512]) <class ‘torch.cuda.FloatTensor’>. I used “save_image(zz,‘t1.png’)” command to save as image but shows the error “TypeError: tensor or list of tensors expected, got <class ‘torch.autograd.variable.Variable’>”

I try to use “var_1= torch.from_numpy(numpy.array(var_1))” command before save_image but system hang. How can i save it as image?

(2) I found difference between variable and tensor is gradient propagation. But i think that difference is not present in latest version. If it is then after which version the vector and variable are the same?

  1. Variables were deprecated in PyTorch 0.4, and you can use tensors in newer versions.

  2. Could you update to the latest stable PyTorch (1.5) and torchvision (0.6) release and retry your code?

1 Like

Code is not supported in that version and required many changes. It will be fine if there is method to visualize the variable in form of image.

I’m not sure, what causes this error in these old versions, but you should be able to transform the tensor to a numpy array, permute its dimensions to [height, width, channels] and use matplotlib.pyplot.imshow to visualize the image.

1 Like

In pytorch we can’t see the variable as image? If not i will not try much.

PyTorch or torchvision don’t reimplement visualization libraries and use e.g. PIL or other standard libraries to visualize tensors.

1 Like

Okay ptrblck,
Thanks.