如何将图片标题添加到tensorboardX



我目前正在使用tensorboardX来可视化输入图像,同时训练ResNet图像分类器。有没有办法在添加图像的同时添加图像标题?我希望在tensorboard显示器中的图像下方显示图像名称(存储在数据集中(。

到目前为止,我已经尝试将comment参数传递到我的tensorboard编写器中,但似乎无法完成任务。目前,我的代码的相关行是:

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)

没有办法直接使用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

最新更新