How to Display Custom Images in Tensorboard (e.g. Matplotlib Plots)?

前端 未结 5 2070
遇见更好的自我
遇见更好的自我 2020-12-04 22:01

The Image Dashboard section of the Tensorboard ReadMe says:

Since the image dashboard supports arbitrary pngs, you can use this to embed custom visual

5条回答
  •  长情又很酷
    2020-12-04 22:14

    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:

提交回复
热议问题