如何将图像标题添加到tensorboardX?

时间:2020-03-28 21:43:32

标签: tensorflow pytorch tensorboard tensorboardx

我目前在训练ResNet图像分类器时使用tensorboardX可视化输入图像。有没有一种方法可以将图像标题与添加的图像一起添加?我想在张量板显示中的图像下方显示图像名称(存储在数据集中)。

到目前为止,我已经尝试过将一个comment参数传递给我的tensorboard writer,但这似乎并没有完成。 目前,我代码的相关行是:

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 个答案:

答案 0 :(得分:1)

无法直接通过tensorboard进行操作,而是必须使用matplotlib创建带有标题的图像,然后将其提供给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