Tensorboard原本是Google TensorFlow的可视化工具,可以用于记录训练数据、评估数据、网络结构、图像等,并且可以在web上展示,对于观察神经网络的过程非常有帮助。PyTorch也推出了自己的可视化工具,一个是tensorboardX包,一个是torch.utils.tensorboard,二者的使用相差不大,这里介绍后者
注: 虽说PyTorch中直接有tensorboard的包,但是有时用的时候还是会报错,所以安装TensorFlow之后torch.utils.tensorboard就可以直接使用且稳定,所以这里介绍安装TensorFlow的方法。
pip install tensorflow_gpu=2.5.0 -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install six numpy wheel
pip install keras_applications=1.0.6 --no-deps
pip install keras_preprocessing=1.0.5 --no-deps
conda install --channel https://conda.anaconda.org/anaconda tensorflow=2.5.0
首先展示该包的使用的大致流程
1)导入tensorboard,实例化SummaryWriter类,指明记录日记路径等信息
from torch.utils.tensorboard import SummaryWriter
#实例化SummaryWriter,并指明日志存放路径。在当前目录如果每月logs目录将自动创建
#如果不写log_dir,系统将会创建runs目录
writer = SummaryWriter(log_dir = ‘logs’)
#调用实例
writer.add_xxx()
#关闭writer
writer.close()
2)调用相应的API,接口一般格式为:
add_xxx(tag_name, object, iteration-number)
3)启动tensorboard,在命令行中输入
tensorboard --logdir=r’加logs所在路径’
4)复制网址在浏览器中打开
add_scalar(tag, scalar_value, global_step=None, walltime=None)
参数:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter()
for x in range(1, 101) :
writer.add_scalar('y = 2x', x, 2 * x)
writer.close()
add_scalars( main_tag , tag_scalar_dict , global_step = None , walltime = None)
参数
from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter()
r = 5
for x in range(1, 101) :
writer.add_scalars('run_14h', {
'xsinx' : x * np.sin(x / r),
'xcosx' : x * np.cos(x / r),
'xtanx' : x * np.tan(x / r)}, x)
writer.close()
add_histogram( tag , values , global_step = None , bins = ‘tensorflow’ , walltime = None , max_bins = None )
参数:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter()
for step in range(10) :
x = np.random.randn(1000)
writer.add_histogram('distribution of gaussion', x, step)
writer.close()
add_image(tag, img_tensor, global_step=None, walltime=None, dataformats = ‘CHW’)
参数:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
import cv2 as cv
import torch
img = cv.imread('zhou.jpg', cv.IMREAD_COLOR)#输入图像要是3通道的,所以读取彩色图像
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img = torch.tensor(img.transpose(2, 0, 1))#cv读取为numpy图像为(H * W * C),所以要进行轴转换
writer.add_image('zhou_ge', img, 0)
writer.close()
add_figure( tag , figure , global_step = None , close = True , walltime = None )
参数:
例子:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
import matplotlib.pyplot as plt
%matplotlib
writer = SummaryWriter()
x = np.linspace(0, 10, 1000)
y = np.sin(x)
figure1 = plt.figure()
plt.plot(x, y, 'r-')
writer.add_figure('my_figure', figure1, 0)
writer.close()
add_graph(model, input_to_model=None, verbose=False, use_strict_trace = True)
参数:
例子:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
import torch
import torch.nn as nn
writer = SummaryWriter()
class MLP(nn.Module) :
def __init__(self):
super(MLP, self).__init__()
self.Net = nn.Sequential(
nn.Linear(784, 512),
nn.ReLU(),
nn.Linear(512, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
def forward(self, input):
input = input.view(-1, 28 * 28)
return self.Net(input)
model = MLP()
input = torch.FloatTensor(np.random.rand(32, 1, 28, 28))
writer.add_graph(model, input)
writer.close()
嵌入:
add_embedding(mat, metadata=None, label_img=None, global_step = None, tag=‘default’, metadata_header=None)
参数:
以上是关于一些tensorboard可视化的操作,如果您有收获的话,就给一个三连。就此谢过!