在 tensorboard 中显示 matplotlib 图而不是转储到文件

Displaying matplotlib figure in tensorboard instead of dumping to file

我正在尝试在 Tensorboard 中直接显示由 Tensorflow 生成的图像。我尝试使用此解决方案 但我不明白如何 link 将其用于训练期间生成的图像,因为摘要是在创建 Tensorflow 图之前定义的:

def plot(samples):
   fig = plt.figure(figsize=(4, 4))
   gs = gridspec.GridSpec(4, 4)
   gs.update(wspace=0.05, hspace=0.05)
   for i, sample in enumerate(samples):
       ax = plt.subplot(gs[i])
       plt.axis('off')
       ax.set_xticklabels([])
       ax.set_yticklabels([])
       ax.set_aspect('equal')
       plt.imshow(sample.reshape(28, 28), cmap='Greys_r')
   return fig

   # ....

   if it % 1000 == 0:
       samples = sess.run(G_sample, feed_dict={z: sample_z(16, z_dim)})
       fig = plot(samples)
       plt.savefig('out/{}.png'
                    .format(str(i).zfill(3)), bbox_inches='tight')
       i += 1
       plt.close(fig)

我认为您混淆了两件事:Tensorboard 摘要 (https://www.tensorflow.org/api_docs/python/tf/summary/image) 和保存图像。

如果你想在张量板上显示你的样本,你必须在你的代码中使用 tf.summary.image 并将你的 G_sample 张量提供给它。如果您查看 https://www.tensorflow.org/api_guides/python/summary#Generation_of_Summaries,您将获得有关如何使用 Filewriters 等的更多信息。

让我知道这是否有帮助,否则您会卡在其他地方!

使用 tf-matplotlib 一个简单的散点图可以归结为:

import tensorflow as tf
import numpy as np

import tfmpl

@tfmpl.figure_tensor
def draw_scatter(scaled, colors): 
    '''Draw scatter plots. One for each color.'''  
    figs = tfmpl.create_figures(len(colors), figsize=(4,4))
    for idx, f in enumerate(figs):
        ax = f.add_subplot(111)
        ax.axis('off')
        ax.scatter(scaled[:, 0], scaled[:, 1], c=colors[idx])
        f.tight_layout()

    return figs

with tf.Session(graph=tf.Graph()) as sess:

    # A point cloud that can be scaled by the user
    points = tf.constant(
        np.random.normal(loc=0.0, scale=1.0, size=(100, 2)).astype(np.float32)
    )
    scale = tf.placeholder(tf.float32)        
    scaled = points*scale

    # Note, `scaled` above is a tensor. Its being passed `draw_scatter` below. 
    # However, when `draw_scatter` is invoked, the tensor will be evaluated and a
    # numpy array representing its content is provided.   
    image_tensor = draw_scatter(scaled, ['r', 'g'])
    image_summary = tf.summary.image('scatter', image_tensor)      
    all_summaries = tf.summary.merge_all() 

    writer = tf.summary.FileWriter('log', sess.graph)
    summary = sess.run(all_summaries, feed_dict={scale: 2.})
    writer.add_summary(summary, global_step=0)

执行后,在 Tensorboard 中生成以下图