[Introduction to Artificial Intelligence] Installation and simple use of PyTorch visualization tool Tensorboard

[Introduction to Artificial Intelligence] Installation and simple use of PyTorch visualization tool Tensorboard


1. Install Tensorboard

1.1 Install Tensorboard

  • Tensorboard was originally a visualization tool for Tensorflow, but since PyTorch version 1.2.0, PyTorch has officially built-in support for Tensorboard. However, Tensorboard still needs to be installed manually. Otherwise, an error will be reported.

ModuleNotFoundError: No module named ‘tensorboard’

  • After entering the corresponding virtual environment, enter the following instructions to install.
pip install tensorboard

1.2 Verify installation

  • Enter the following command. If no error is reported, the installation is successful.
from torch.utils.tensorboard import SummaryWriter

2. Introduction to Tensorboard functions

  1. Four commonly used functions of Tensorboard
  • graphs: Save network structure diagram;
  • scalars: accuracy, learning rate, loss curve;
  • histograms: training weight distribution;
  • images: Display image information.
  1. Draw lines——add_scalaar
from torch.utils.tensorboard import SummaryWriter

# 创建编辑器,保存日志,指令保存路径log_dir
writer = SummaryWriter(log_dir="./logs") # 指定保存位置

# y = 2 * x
for i in range(100):
    # 添加标题,x轴,y轴
    # tag: 标题名, scalar_value: y轴, global_step: x轴
    writer.add_scalar(tag="y=2x",scalar_value=2*1,global_step=i)

# 关闭
writer.close()
  • Executing the above code in PyCharm will save relevant files in the generated logs folder.
  • Go to the upper-level directory of logs in the PyCharm virtual environment terminal, enter the startup command (below), and click the generated link to view the results.
# 使用默认端口
tensorboard --logdir=logs
# 使用指定端口
tensorboard --logdir=logs --port=6007
  • Type in the terminal Ctrl + Cto end the process.
  1. Display a single image - add_image
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image

# 创建编辑器,保存日志,指令保存路径log_dir
writer = SummaryWriter(log_dir="./logs") # 指定保存位置

# 图像地址
image_path = "/home/chenshili/图片/food_01.jpeg"

# 打开图像
img_PIL = Image.open(image_path) # 用PIL打开的图像,其文件格式是PIL的特有类

# 转化成numpy数据,因为add_image只能传入array或者tensor数据,所以要做类型转化。
img_array = np.array(img_PIL)

# 开始画图,tag: 标题,img_tensor: tensor或者numpy类型的数据, dataformats: H高W宽C通道,指定HWC类型
writer.add_image(tag="train",img_tensor=img_array,global_step=1,dataformats="HWC")

# 关闭
writer.close()
  1. Draw model——add_graph
from torch.utils.tensorboard import SummaryWriter
import torch
from torch.nn import Linear,ReLU,Sequential

# 创建编辑器,保存日志,指令保存路径log_dir
writer = SummaryWriter(log_dir="./logs") # 指定保存位置

model = Sequential(
    Linear(1,10),
    ReLU(),
    Linear(10,1)
)

data = torch.ones(10,1)

writer.add_graph(model=model,input_to_model=data)

# 关闭
writer.close()
  1. Model comparison
from torch.utils.tensorboard import SummaryWriter
import os
import datetime

# 按时间构建生成文件保存的文件夹
log_dir = os.path.join("./logs",datetime.datetime.now().strftime("%Y%m%d_%H%M%S"))
print(log_dir)

# 创建编辑器,保存日志,指令保存路径log_dir
writer = SummaryWriter(log_dir=log_dir) # 指定保存位置

for i in range(100):
    writer.add_scalar(tag="loss",scalar_value=1*i,global_step=i)
    # 多执行几次把scalar_value的值做修改,如2*i,i等

# 关闭
writer.close()

Guess you like

Origin blog.csdn.net/qq_44928822/article/details/128722693
Recommended