Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I view Tensor as an image?

I learned some data with tensorflow.

For the test, I saw the shape of the final result.

It was tensor of (1, 80, 80, 1).

I use matplotlib or PIL to do this,

I wanted to see the image after changing to a pie array.

But I could not change the tensor to numpy.

I could not do anything because of the session even if I used eval ().

There is no way to convert tensor to numpy.

Can I see the tensor as an image?

(mytensor1) # mytensor

arr = np.ndarray(mytensor1)
arr_ = np.squeeze(arr)
plt.imshow(arr_)
plt.show()

but there is error message: TypeError: expected sequence object with len >= 0 or a single integer

like image 649
ONION Avatar asked Jun 17 '18 14:06

ONION


2 Answers

You can use squeeze function from numpy. For example

arr = np.ndarray((1,80,80,1))#This is your tensor
arr_ = np.squeeze(arr) # you can give axis attribute if you wanna squeeze in specific dimension
plt.imshow(arr_)
plt.show()

Now, you can easily display this image (e.g. above code, assuming you are using matplotlib.pyplot as plt).

like image 162
talos1904 Avatar answered Oct 05 '22 03:10

talos1904


For people using PyTorch, the simplest way that I know is this:

import matplotlib.pyplot as plt

plt.imshow(my_tensor.numpy()[0], cmap='gray')

That should do it

like image 34
J. Krajewski Avatar answered Oct 05 '22 01:10

J. Krajewski