tensorflow - 如何将图像标题添加到 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)
解决方案
没有办法直接使用 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
推荐阅读
- sql - 在 Oracle 的 user_tab_partitions 上按 HIGHVALUE 排序
- javascript - 使用正则表达式替换和转换文本
- javascript - 我将如何将使用 fetch 调用的函数转换为 async/await?
- javascript - Firebase.collection 不是函数
- c++ - 安装,opencv 4,5,1 错误:libtiff.so.5:未定义对“jbg_enc_out”的引用
- javascript - 从前端上传 s3 存储桶中的图像
- c++ - C/C++ 编译过程的哪个阶段使其依赖于系统?
- javascript - 为什么使用 JavaScript 添加 loading="lazy" 不能真正延迟加载图像?
- amazon-web-services - 查找我可以列出和获取但不拥有的大型 s3 存储桶大小的快速方法?
- django - 为什么 React Native 会从 Django 接收“models.TextChoices”属性作为转换为字符串的元组?