【pytorch】tensorboard的使用

一、tensorboard的说明

TensorBoard是深度学习领域通用的可视化工具套件,TensorBoard的核心价值在于将抽象的模型训练过程转化为直观的可视化图表。

在PyTorch中,SummaryWriter是TensorBoard的数据写入器,用于将训练数据(指标、模型结构、图像等)记录到事件文件(event files)中。

python 复制代码
class SummaryWriter(object):
    """Writes entries directly to event files in the log_dir to be
    consumed by TensorBoard.

    The `SummaryWriter` class provides a high-level API to create an event file
    in a given directory and add summaries and events to it. The class updates the
    file contents asynchronously. This allows a training program to call methods
    to add data to the file directly from the training loop, without slowing down
    training.
    """

记录标量(指标):使用add_scalar方法跟踪损失、准确率等指标。

记录图像:使用add_image方法展示训练数据或生成样本。

二、tensorboard的画图使用

add_scalar:scalar_value是y轴,global_step是x轴

创建y=x和y=2x的图,并通过网页展示

python 复制代码
from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter('logs')
# writer.add_image()
for i in range(100):
    writer.add_scalar('y=2x', 2*i, i)

writer.close()

在控制台输出,也可以指定其他端口tensorboard --logdir=logs --port=6007:

powershell 复制代码
tensorboard --logdir=logs

输出

浏览器打开http://localhost:6006/

三、tensorboard的记录图片使用

python 复制代码
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image

writer = SummaryWriter('logs')
image_path = r'data/train/ants_image/0013035.jpg'
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL)
#img_array的shape是(512, 768, 3),通道3,需要加dataformats='HWC'参数,否则报错
writer.add_image("test", img_array, 1, dataformats='HWC')


# for i in range(100):
#     writer.add_scalar('y=x', i, i)

writer.close()

输出

相关推荐
盼小辉丶5 天前
PyTorch实战(30)——使用TorchScript和ONNX导出通用PyTorch模型
人工智能·pytorch·深度学习·模型部署
封奚泽优5 天前
使用mmdetection项目进行训练记录
pytorch·python·cuda·mmdetection·mmcv
tony3655 天前
pytorch分布式训练解释
人工智能·pytorch·分布式
weixin_贾5 天前
深度学习基础理论与 PyTorch 实战 —— 从传统机器学习到前沿模型全攻略
pytorch·深度学习·机器学习
大连好光景5 天前
PyTorch深度学习----优化器
pytorch·深度学习·学习
多恩Stone6 天前
【3D-AICG 系列-11】Trellis 2 的 Shape VAE 训练流程梳理
人工智能·pytorch·算法·3d·aigc
隔壁大炮7 天前
08. PyTorch_张量基本创建方式
人工智能·pytorch·python
隔壁大炮7 天前
07. PyTorch框架简介
人工智能·pytorch·python
大鹏的NLP博客7 天前
Rust + PyTorch 实现 BGE 向量检索系统
人工智能·pytorch·rust
勾股导航8 天前
蚁群优化算法
人工智能·pytorch·python