Pytorch中使用tensorboard学习笔记(6) 添加torch.Tensor形式的图片add_image和add_images

参考链接: add_image
参考链接: add_images
代码:

from torch.utils.tensorboard import SummaryWriter
import numpy as np
img = np.zeros((3, 100, 100))
img[0] = np.arange(0, 10000).reshape(100, 100) / 10000
img[1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000

img_HWC = np.zeros((100, 100, 3))
img_HWC[:, :, 0] = np.arange(0, 10000).reshape(100, 100) / 10000
img_HWC[:, :, 1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000

writer = SummaryWriter()
writer.add_image('my_image', img, 0)

# If you have non-default dimension setting, set the dataformats argument.
writer.add_image('my_image_HWC', img_HWC, 0, dataformats='HWC')
writer.close()

# tensorboard --logdir=runs

运行结果展示:
Pytorch中使用tensorboard学习笔记(6) 添加torch.Tensor形式的图片add_image和add_images_第1张图片

代码:

from torch.utils.tensorboard import SummaryWriter
import numpy as np

img_batch = np.zeros((16, 3, 100, 100))
for i in range(16):
    img_batch[i, 0] = np.arange(0, 10000).reshape(100, 100) / 10000 / 16 * i
    img_batch[i, 1] = (1 - np.arange(0, 10000).reshape(100, 100) / 10000) / 16 * i

writer = SummaryWriter()
writer.add_images('my_image_batch', img_batch, 0)
writer.close()

# tensorboard --logdir=runs

运行结果展示:Pytorch中使用tensorboard学习笔记(6) 添加torch.Tensor形式的图片add_image和add_images_第2张图片

你可能感兴趣的:(Pytorch中使用tensorboard学习笔记(6) 添加torch.Tensor形式的图片add_image和add_images)