網(wǎng)絡(luò)結(jié)構(gòu)可視化
使用pytorch定義網(wǎng)絡(luò)結(jié)構(gòu)之后,為了直觀起見,需要可視化網(wǎng)絡(luò)結(jié)構(gòu),以圖的形式顯示出來。pytorch網(wǎng)絡(luò)結(jié)構(gòu)可視化可以采用tensorboardX。
開發(fā)/實(shí)驗(yàn)環(huán)境
- Ubuntu 18.04
- Anaconda3, python3.6.
- pytorch 1.0
- tensorflow, tensorboard, tensorboardX
- pycharm
tensorboardX
首先需要安裝tensorboard, tensorflow。
pip install tensorflow
pip install tensorboard
pip install tensorboardX
實(shí)驗(yàn)
- 首先定義一個(gè)CNN網(wǎng)絡(luò),LetNet-5為例
import torch
import torch.nn as nn
import torch.nn.functional as F
from tensorboardX import SummaryWriter
'''
CNN計(jì)算
(H - k +2 * P) / S + 1
(W - k +2 * P) / S + 1
LetNet-5
input: 32*32*3
out_conv1 = (32-5)+1 = 28
max_pool1 = 28 / 2 = 14
out_conv2 = (14 - 5) + 1 = 10
max_pool2 = 10 / 2 = 5
'''
'''
定義一個(gè)神經(jīng)網(wǎng)絡(luò)
https://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html#sphx-glr-beginner-blitz-neural-networks-tutorial-py
'''
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# conv1層,輸入的灰度圖,所以 in_channels=1, out_channels=6 說明使用了6個(gè)濾波器/卷積核,
# kernel_size=5卷積核大小5x5
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5)
# conv2層, 輸入通道in_channels 要等于上一層的 out_channels
self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5)
# an affine operarion: y = Wx + b
# 全連接層fc1,因?yàn)?2x32圖像輸入到fc1層時(shí)候,feature map為: 5x5x16
# 因此,全連接層的輸入特征維度為16*5*5, 因?yàn)樯弦粚觕onv2的out_channels=16
# out_features=84,輸出維度為84,代表該層為84個(gè)神經(jīng)元
self.fc1 = nn.Linear(16*5*5, 120)
self.fc2 = nn.Linear(in_features=120, out_features=84)
self.fc3 = nn.Linear(in_features=84, out_features=10)
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square you can only specify a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
# 特征圖轉(zhuǎn)換為一個(gè)1維的向量
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
print(net)
# tensorboardX
# 假設(shè)輸入1張1*32*32的圖片
dummy_input = torch.rand(1, 1, 32, 32)
model = Net()
with SummaryWriter(comment='LeNet') as w:
w.add_graph(model, (dummy_input, ))
運(yùn)行結(jié)果:
(輸入為1x32x32, 不是1x28x28)
image
在當(dāng)前工程目錄下生成一個(gè)runs文件夾。
image.png
- 使用tensorboardX
因?yàn)楣P者安裝的是anconda3, 采用pip方式安裝tensorflow, tensorboard, tensorboardX, 因此自動(dòng)在anaconda3/bin/ 目錄下自動(dòng)生成了tensorbard文件。
image.png
image.png
step1: 在終端下,切換到run目錄下:
image.png
step2: 運(yùn)行命令
tensorboard --logdir xxxx
xxx------run所在的路徑
image.png
image.png
step3: 在瀏覽器查看結(jié)果
image.png
image.png
查看網(wǎng)絡(luò)結(jié)構(gòu):
image.png
保存網(wǎng)絡(luò)結(jié)果:
image.png
letNet-5.png
End
參考:https://blog.csdn.net/sunqiande88/article/details/80155925