【pytorch-learning】(三) 可视化

tensorboard的使用

安装

1
pip install tensorboard

使用

add_scalar

运行代码

1
2
3
4
5
6
7
8
from torch.utils.tensorboard import  SummaryWriter
writer = SummaryWriter("logs")# 保存log的文件夾
##y=x
for i in range(100):

writer.add_scalar("y=2x",2*i,i)

writer.close()

命令行运行:打开事件文件

1
tensorboard --logdir=logs --port=6007

tensorboard参数: —logdir=文件夹名称 —port=端口号 可以不设置端口号就是默认,为了避免冲突可以设置

由于多次添加,导致log文件中有各种,可能导致tensorboard显示曲线的一些拟合等。

解决:

  • 删除logs文件夹下的文件,重新运行程序。
  • 对应每个任务每次都设立子文件夹。

浏览器查看

网址在命令行有显示。前面示例的为 localhost:6007。

add_image

图片

数据类型需要tensor或者numpy array

1
2
3
4
5
6
7
8
9
10
11
12
13
from torch.utils.tensorboard import  SummaryWriter
from PIL import Image
import numpy as np

writer = SummaryWriter("logs")
img_path = "./hymenoptera_data/hymenoptera_data/train/ants/0013035.jpg"
img_PIL = Image.open(img_path)
img_array = np.array(img_PIL)
print(img_array.shape)
print(type(img_array))
writer.add_image("test",img_array,1,dataformats="HWC")##默认是CHW,如果不是需要进行设置

writer.close()

add_graph

可视化模型

  • 代码中使用tensorboard的add_graph将模型加入,注意传参需要传入模型输入。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter


class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.model1 = Sequential(
Conv2d(3, 32, kernel_size=5, padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
nn.Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self,x):
x = self.model1(x)
return x
model = Model()
print(model)

input = torch.ones(64,3,32,32)
output = model(input)
print(output.shape)

writer = SummaryWriter("logs_seq")
writer.add_graph(model,input)
writer.close()
  • 命令行运行命令打开事件文件

  • 浏览器查看模型,可以点击放大,看模型具体层

tensorboard查看

Visdom可视化

窗口视图,界面友好

安装

  • 方式一
1
pip install visdom

命令行运行

1
python -m visdom.server

如果报错找不到路径,解决方式:在该目录下自己创建该文件夹

  • 安装方式2:避免download问题
  1. pip uninstall visdom
  2. 从github下载visdom文件
  3. cd 该目录下,pip install -e .
  4. 在项目路径下 python -m visdom.server成功启动

使用

visdom的一些具体使用:https://blog.csdn.net/weixin_41010198/article/details/117853358

一个使用实例

  1. 命令行运行python -m visdom.server成功启动
  2. 运行代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from visdom import Visdom
import torch

loss = torch.rand(10)

global_step = torch.arange(10)


viz = Visdom()


viz.line([0.],[0.],win='train_loss',opts = dict(title='train_loss'))

for idx,lossi in enumerate(loss):

viz.line([lossi.item()],[idx],win = 'train_loss',update='append')
  1. 浏览器查看

浏览器视图

完整模型训练测试过程中使用visdom可视化

手写数字识别数据集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import  torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms

from visdom import Visdom

batch_size=200
learning_rate=0.01
epochs=2

train_loader = torch.utils.data.DataLoader(
datasets.MNIST('./mnist data/', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
# transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('./mnist data/', train=False, transform=transforms.Compose([
transforms.ToTensor(),
# transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=True)



class MLP(nn.Module):

def __init__(self):
super(MLP, self).__init__()

self.model = nn.Sequential(
nn.Linear(784, 200),
nn.LeakyReLU(inplace=True),
nn.Linear(200, 200),
nn.LeakyReLU(inplace=True),
nn.Linear(200, 10),
nn.LeakyReLU(inplace=True),
)

def forward(self, x):
x = self.model(x)

return x

device = torch.device('cuda:0')
net = MLP().to(device)
optimizer = optim.SGD(net.parameters(), lr=learning_rate)
criteon = nn.CrossEntropyLoss().to(device)

viz = Visdom()

viz.line([0.], [0.], win='train_loss', opts=dict(title='train loss'))
viz.line([[0.0, 0.0]], [0.], win='test', opts=dict(title='test loss&acc.',
legend=['loss', 'acc.']))
global_step = 0

for epoch in range(epochs):

for batch_idx, (data, target) in enumerate(train_loader):
data = data.view(-1, 28*28)
data, target = data.to(device), target.to(device)

logits = net(data)
loss = criteon(logits, target)

optimizer.zero_grad()
loss.backward()
# print(w1.grad.norm(), w2.grad.norm())
optimizer.step()

global_step += 1
viz.line([loss.item()], [global_step], win='train_loss', update='append')

if batch_idx % 100 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))


test_loss = 0
correct = 0
for data, target in test_loader:
data = data.view(-1, 28 * 28)
data, target = data.to(device), target.cuda()
logits = net(data)
test_loss += criteon(logits, target).item()

pred = logits.argmax(dim=1)
correct += pred.eq(target).float().sum().item()

viz.line([[test_loss, correct / len(test_loader.dataset)]],
[global_step], win='test', update='append')
viz.images(data.view(-1, 1, 28, 28), win='x')
viz.text(str(pred.detach().cpu().numpy()), win='pred',
opts=dict(title='pred'))

test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))

  • 视图

image-20221124201948485

端口占用处理

windows上

  • 查找本端口当前使用情况
1
netstat -ano |findstr "8097"
1
2
TCP    0.0.0.0:8097           0.0.0.0:0              LISTENING       12236
TCP [::]:8097 [::]:0 LISTENING 12236
  • 查询当前端口PID的进程,前一步看到为12236
1
tasklist | findstr 12236
  • 终止进程
1
taskkill /f /t /im "12236"
1
成功: 已终止 PID 12236 (属于 PID 7560 子进程)的进程。