Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. import io
  2. import matplotlib.pyplot as plt
  3. import tensorflow as tf
  4.  
  5.  
  6. def gen_plot():
  7. """Create a pyplot plot and save to buffer."""
  8. plt.figure()
  9. plt.plot([1, 2])
  10. plt.title("test")
  11. buf = io.BytesIO()
  12. plt.savefig(buf, format='png')
  13. buf.seek(0)
  14. return buf
  15.  
  16.  
  17. # Prepare the plot
  18. plot_buf = gen_plot()
  19.  
  20. # Convert PNG buffer to TF image
  21. image = tf.image.decode_png(plot_buf.getvalue(), channels=4)
  22.  
  23. # Add the batch dimension
  24. image = tf.expand_dims(image, 0)
  25.  
  26. # Add image summary
  27. summary_op = tf.image_summary("plot", image)
  28.  
  29. # Session
  30. with tf.Session() as sess:
  31. # Run
  32. summary = sess.run(summary_op)
  33. # Write summary
  34. writer = tf.train.SummaryWriter('./logs')
  35. writer.add_summary(summary)
  36. writer.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement