gpt4 book ai didi

python - 在张量板上显示 matplotlib 图而不是转储到文件

转载 作者:太空宇宙 更新时间:2023-11-03 15:38:44 27 4
gpt4 key购买 nike

我正在尝试直接在 Tensorboard 中显示由 Tensorflow 生成的图像。我尝试使用此解决方案 Tensorflow: How to Display Custom Images in Tensorboard (e.g. Matplotlib Plots)但我不明白如何将其链接到训练期间生成的图像,因为摘要是在创建 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)

最佳答案

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 内会产生以下结果

关于python - 在张量板上显示 matplotlib 图而不是转储到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42329394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com