Tensorflow:如何在Tensorboard中显示自定义图像(例如Matplotlib图) [英] Tensorflow: How to Display Custom Images in Tensorboard (e.g. Matplotlib Plots)
问题描述
图像仪表板部分Tensorboard自述文件的作者说:
The Image Dashboard section of the Tensorboard ReadMe says:
由于图像仪表板支持任意png,因此您可以使用它将自定义可视化效果(例如matplotlib散点图)嵌入到TensorBoard中.
Since the image dashboard supports arbitrary pngs, you can use this to embed custom visualizations (e.g. matplotlib scatterplots) into TensorBoard.
我看到如何将pyplot图像写入文件,作为张量读回,然后与tf.image_summary()一起使用将其写入TensorBoard,但是自述文件中的此语句表明有一种更直接的方法.在那儿?如果是这样,是否还有其他文档和/或示例来说明如何有效地做到这一点?
I see how a pyplot image could be written to file, read back in as a tensor, and then used with tf.image_summary() to write it to TensorBoard, but this statement from the readme suggests there is a more direct way. Is there? If so, is there any further documentation and/or examples of how to do this efficiently?
推荐答案
如果图像在内存缓冲区中,则很容易做到.下面,我显示一个示例,其中将pyplot保存到缓冲区,然后转换为TF图像表示形式,然后将其发送到图像摘要.
It is quite easy to do if you have the image in a memory buffer. Below, I show an example, where a pyplot is saved to a buffer and then converted to a TF image representation which is then sent to an image summary.
import io
import matplotlib.pyplot as plt
import tensorflow as tf
def gen_plot():
"""Create a pyplot plot and save to buffer."""
plt.figure()
plt.plot([1, 2])
plt.title("test")
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
return buf
# Prepare the plot
plot_buf = gen_plot()
# Convert PNG buffer to TF image
image = tf.image.decode_png(plot_buf.getvalue(), channels=4)
# Add the batch dimension
image = tf.expand_dims(image, 0)
# Add image summary
summary_op = tf.summary.image("plot", image)
# Session
with tf.Session() as sess:
# Run
summary = sess.run(summary_op)
# Write summary
writer = tf.train.SummaryWriter('./logs')
writer.add_summary(summary)
writer.close()
这提供了以下TensorBoard可视化效果:
This gives the following TensorBoard visualization:
这篇关于Tensorflow:如何在Tensorboard中显示自定义图像(例如Matplotlib图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!