首页 > 解决方案 > 如何将图像标题添加到 tensorboardX?

问题描述

我目前正在使用 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)

标签: tensorflowpytorchtensorboardtensorboardx

解决方案


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


推荐阅读