【问题标题】:After calculating a tensor, how can I show it as a image?计算张量后,如何将其显示为图像?
【发布时间】:2016-05-31 08:16:44
【问题描述】:

我有一个一维 numpy 数组。在 TensorFlow 中执行计算后,我得到一个 tf.Tensor 作为输出。我正在尝试将其重塑为二维数组并将其显示为图像。

如果它是一个 numpy ndarray,我会知道如何将其绘制为图像。但它现在是张量!

虽然我尝试tensor.eval() 将其转换为 numpy 数组,但我收到一条错误消息“无默认会话”。

谁能教我如何将张量显示为图像?

... ...
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()
... ...

【问题讨论】:

  • 欢迎使用 stackoverflow。您可以发布您的尝试吗?
  • 请用良好的缩进更新问题。见stackoverflow.com/help/how-to-ask 会更容易得到答案
  • 对不起,不熟悉这个..刚刚更新

标签: python arrays image numpy tensorflow


【解决方案1】:

您看到的直接错误是因为 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() 操作和TensorBoard 来可视化由更大的训练管道产生的张量。

【讨论】:

  • 非常感谢。我是张量流的新手。努力学习使用它。非常感谢您的回答
猜你喜欢
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 2016-12-11
相关资源
最近更新 更多