The Image Dashboard section of the Tensorboard ReadMe says:
Since the image dashboard supports arbitrary pngs, you can use this to embed custom visualizations (e.g. matplotlib scatterplots) into 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?
The draw() function in pyplot module of matplotlib library is used to redraw the current figure.
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()
This gives the following TensorBoard visualization:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With