首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将图片标题添加到tensorboardX?

如何将图片标题添加到tensorboardX?
EN

Stack Overflow用户
提问于 2020-03-29 05:43:33
回答 1查看 369关注 0票数 1

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

到目前为止,我已经尝试将一个comment参数传递给我的tensorboard编写器,这似乎不能完成这项工作。目前,我的代码的相关行是:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2020-07-08 21:09:59

没有办法直接使用tensorboard,而是必须使用matplotlib创建带有标题的图像,然后将它们提供给tensorboard。以下是tensorboard文档中的示例代码:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60907358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档