1. writer.add_scalar()
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
# 两个方法
# writer.add_image()
for i in range(100):
writer.add_scalar("y=x", i ,2*i) #(title,value,step)
writer.close()
命令行输入:
tensorboard --logdir logs/
网页打开地址:
2. writer.add_image()
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer = SummaryWriter("logs")
# 方法一:图片
image_path = "/home/wtj/Data/Coco2017_pet/images/train2017/000000000042.jpg"
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL) #转为array
writer.add_image("first_image", img_array, 0, dataformats = 'HWC')
# step可增加0,1,2,... dataformats = 'CHW' ,'HWC','HW'
# 方法二:变量
for i in range(100):
writer.add_scalar("y=2x", i ,2*i) #(title,value,step)
writer.close()
注意:图片需要array格式,可通过以下代码调试