After calculating the tensor, how can I show it as an image?

I have one numpy sized array. After doing the calculation in TensorFlow, I get the output tf.Tensor. I am trying to convert it to a 2 dimensional array and show it as an image.

If it were numpy ndarray, I would know how to do it as an image. But now it's a tensor!

Although I tried to tensor.eval()convert it to a numpy array, I got the error message "No session by default."

Can someone teach me how to show tensor as image?

... ...
init = tf.initialize_all_variables()    
sess = tf.Session()
sess.run(init)

# training
for i in range(1):
    sess.run(train_step, feed_dict={x: x_data.T, y_: y_data.T})

# testing
probability = tf.argmax(y,1);
sess.run(probability, feed_dict={x: x_test.T})

#show result
img_res = tf.reshape(probability,[len_y,len_x])
fig, ax = plt.subplots(ncols = 1)

# It is the the following line that I do not know how to make it work...
ax.imshow(np.asarray(img_res.eval())) #how to plot a tensor ?#
plt.show()
... ...
+4
source share
1 answer

, , , Tensor.eval() "default Session" . , (i) with tf.Session():, (ii) with sess.as_default():, (iii) tf.InteractiveSession.

:

# Pass the session to eval().
ax.imshow(img_res.eval(session=sess))

# Use sess.run().
ax.imshow(sess.run(img_res))

, tf.image_summary() op TensorBoard , .

+2

Source: https://habr.com/ru/post/1629462/


All Articles