TensorBoard 是一组用于数据可视化的工具。它包含在流行的开源机器学习库 Tensorflow 中。虽然他是TensorFlow 的一部分,但是可以独立安装,并且服务于Pytorch等其他的框架。TensorBoard 的主要功能包括:
- 可视化模型的网络架构
- 跟踪模型指标,如损失和准确性等
- 检查机器学习工作流程中权重、偏差和其他组件的直方图
- 显示非表格数据,包括图像、文本和音频
- 将高维嵌入投影到低维空间
使用步骤
1.导入对应包
from torch.utils.tensorboard import SummaryWriter
2.write.add_scalar()使用
参数列表如下:
Args:
tag (str): Data identifier
scalar_value (float or string/blobname): Value to save
global_step (int): Global step value to record
walltime (float): Optional override default walltime (time.time())with seconds after epoch of event
new_style (boolean): Whether to use new style (tensor field) or old style (simple_value field). New style could lead to faster data loading.
其中tag为图表标题,scalar_value为图标的纵坐标,global_step为横坐标
举例:
# 写一个y=x
for i in range(100):
write.add_scalar("y=x",i,i)
运行程序后,在终端输入如下代码进行图表查看
tensorboard --logdir=logs --host=localhost --port=6006
显示如下链接,点击后可进入查看
write.add_image()的使用
参数列表如下:
Args:
tag (str): Data identifier
img_tensor (torch.Tensor, numpy.ndarray, or string/blobname): Image data
global_step (int): Global step value to record
walltime (float): Optional override default walltime (time.time())seconds after epoch of event
dataformats (str): Image data format specification of the form CHW, HWC, HW, WH, etc.
注意此处的img类型,必须是tensor,numpy,string,blobname其中一种
img_path = "dataset/train/ants/0013035.jpg"
img_PIL = Image.open(img_path)
img_array = np.array(img_PIL)
write.add_image("test",img_array,1, dataformats='HWC')
如上运行,控制台输入命令,点击链接,如图所示: