How to view a tensor as an image?

Hi, I was working on a project where I have a tensor output. How do I view it is an image?

What I’ve tried so far:

arr_ = np.squeeze(out_p)
plt.imshow(arr_)
plt.show()

The error:

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

I tried some of the answers mentioned on this forum and on stackoverflow, all to vain.
If anyone help, I would be very grateful
~Thanks!

Did you try to add the suggested tensor.detach().numpy() operation from the error message?
If so, what was the new error?

1 Like

I was wondering if I am doing it wrong?
This is what I’ve done so far:

out_img2=out_img.detach().numpy()
arr_ = np.squeeze(out_img2)
plt.imshow(arr_)
plt.show()

The error is: TypeError: Invalid shape (3, 512, 512) for image data

And when I tried:

out_img2=out_img.detach().numpy()
arr_ = out_img2.squeeze
plt.imshow(arr_)
plt.show()

The error is: TypeError: Image data of dtype object cannot be converted to float

Edit: Solved it.I had forgotten to permute!