第一步安装:
conda install tensorboard或者pip install tensorboard
第二步导包加使用:
from torch.utils.tensorboard import SummaryWriterwriter = SummaryWriter('logs')---代码---writer.close()
SummaryWriter
输入参数为保存到哪个文件夹。
第三步,学习使用add_scalar
直接看源代码:
Args:
tag (string): Data identifier
scalar_value (float or string/blobname): Value to save
global_step (int): Global step value to record
总结起来就是三个参数:
tag:名字
scalar_value:y轴数据
global_step:x轴数据
举个栗子:
from torch.utils.tensorboard import SummaryWriterwriter = SummaryWriter('logs')x = range(100)for i in x:writer.add_scalar('y=x+10', i, i+10)writer.close()
第四步,运行
tensorboard --logdir=logs或者:tensorboard --logdir=绝对地址
第五步,学习使用add_image
tag (string): Data identifier
img_tensor (torch.Tensor, numpy.array, or string/blobname): Image data
global_step (int): Global step value to record
tag:名字
img_tensor :图片数据,类型要求为Tensor,numpy,string/blobname
global_step:要记录的全局步长值
dataformats:不是(3,H,W)形式用此参数
其中对img_tensor的形状有要求,而默认格式是(3,H,W)即通道(channel)为3,H为高度,W为宽度,不是格式需要使用dataformats=''
,该参数填写的数据为:CHW
, HWC
, HW
from torch.utils.tensorboard import SummaryWriterfrom PIL import Imageimport numpy as npwriter = SummaryWriter('logs')image_path = 'dog.png'img_PIL = Image.open(image_path)# 创建PIL的图片类image_array = np.array(img_PIL)# 转成print(image_array.shape)writer.add_image('dog', image_array, 0, dataformats='HWC')writer.close()