如何向 tensorboardX 添加图片标题?
Posted
技术标签:
【中文标题】如何向 tensorboardX 添加图片标题?【英文标题】:How do I add an image title to tensorboardX? 【发布时间】:2020-07-09 10:52:14 【问题描述】:我目前正在使用 tensorboardX 来可视化输入图像,同时训练 ResNet 图像分类器。有没有办法将图像标题与添加的图像一起添加?我想在张量板显示的图像下方显示图像名称(存储在数据集中)。
到目前为止,我已经尝试将 comment
参数传递给我的张量板编写器,但这似乎并没有完成这项工作。
目前,我的代码的相关行是:
pretrain_train_writer = SummaryWriter('log/pretrain_train')
img_grid = vutils.make_grid(inputs[tp_idx_0], normalize=True, scale_each=True, nrow=8)
pretrain_val_writer.add_image('true_positive_class_0', img_grid, global_step=epoch, comment = img_path)
【问题讨论】:
【参考方案1】:没有办法直接使用 tensorboard,而是您必须使用 matplotlib 创建带有标题的图像,然后将它们提供给 tensorboard。以下是 tensorboard 文档中的示例代码:
def plot_to_image(figure):
"""Converts the matplotlib plot specified by 'figure' to a PNG image and
returns it. The supplied figure is closed and inaccessible after this call."""
# Save the plot to a PNG in memory.
buf = io.BytesIO()
plt.savefig(buf, format='png')
# Closing the figure prevents it from being displayed directly inside
# the notebook.
plt.close(figure)
buf.seek(0)
# Convert PNG buffer to TF image
image = tf.image.decode_png(buf.getvalue(), channels=4)
# Add the batch dimension
image = tf.expand_dims(image, 0)
return image
def image_grid():
"""Return a 5x5 grid of the MNIST images as a matplotlib figure."""
# Create a figure to contain the plot.
figure = plt.figure(figsize=(10,10))
for i in range(25):
# Start next subplot.
plt.subplot(5, 5, i + 1, title=class_names[train_labels[i]])
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
return figure
# Prepare the plot
figure = image_grid()
# Convert to image and log
with file_writer.as_default():
tf.summary.image("Training data", plot_to_image(figure), step=0)
这里是文档的链接:https://www.tensorflow.org/tensorboard/image_summaries
【讨论】:
以上是关于如何向 tensorboardX 添加图片标题?的主要内容,如果未能解决你的问题,请参考以下文章