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

前端 未结 5 2075
遇见更好的自我
遇见更好的自我 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:17

    This intends to complete Andrzej Pronobis' answer. Following closely his nice post, I set up this minimal working example:

        plt.figure()
        plt.plot([1, 2])
        plt.title("test")
        buf = io.BytesIO()
        plt.savefig(buf, format='png')
        buf.seek(0)
        image = tf.image.decode_png(buf.getvalue(), channels=4)
        image = tf.expand_dims(image, 0)
        summary = tf.summary.image("test", image, max_outputs=1)
        writer.add_summary(summary, step)
    

    Where writer is an instance of tf.summary.FileWriter. This gave me the following error: AttributeError: 'Tensor' object has no attribute 'value' For which this github post had the solution: the summary has to be evaluated (converted into a string) before being added to the writer. So the working code for me remained as follows (simply add the .eval() call in the last line):

        plt.figure()
        plt.plot([1, 2])
        plt.title("test")
        buf = io.BytesIO()
        plt.savefig(buf, format='png')
        buf.seek(0)
        image = tf.image.decode_png(buf.getvalue(), channels=4)
        image = tf.expand_dims(image, 0)
        summary = tf.summary.image("test", image, max_outputs=1)
        writer.add_summary(summary.eval(), step)
    

    This could be short enough to be a comment on his answer, but these can be easily overlooked (and I may be doing something else different too), so here it is, hope it helps!

提交回复
热议问题