Pytorch的网络结构可视化:Netron与TensorBoardX
Pytorch的網(wǎng)絡(luò)結(jié)構(gòu)可視化:Netron與TensorBoardX
- Pytorch的網(wǎng)絡(luò)結(jié)構(gòu)可視化:Netron
- Pytorch的網(wǎng)絡(luò)結(jié)構(gòu)可視化:TensorBoardX
- 1.TensorBoardX簡(jiǎn)介
- 2. tensorboardX的使用
- TensorBoard is not found.
- 參考資料
Pytorch的網(wǎng)絡(luò)結(jié)構(gòu)可視化:Netron
最近剛剛發(fā)現(xiàn)一個(gè)非常好用的顯示模型神器Netron
https://github.com/lutzroeder/Netron
https://www.electronjs.org/apps/netron
借助這個(gè)工具可以像windows的軟件一樣導(dǎo)入已經(jīng)訓(xùn)練好的模型加權(quán)重即可一鍵生成
以下是我的一個(gè)模型使用該工具可視化結(jié)果,只不過(guò)目前該工具對(duì)于onnx支持非常好,但是pytorch權(quán)重轉(zhuǎn)變?yōu)閛nnx是非常方便的,只需要torch.onnx.export()命令即可導(dǎo)出onnx權(quán)重
以下代碼將resnet18-5c106cde.pth轉(zhuǎn)化為resnet18.onnx
# -- coding: utf-8 -- import io import torch import torch.onnx import torchvisiondevice = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")def pth2onnx():model = torchvision.models.resnet18(pretrained=False)# pth_file = 'resnet18-5c106cde.pth'# model.load_state_dict(torch.load(pth_file, map_location=device))model.to(device)# data type n*c*h*wdummy_input = torch.randn(1, 3, 256, 256)dummy_input = dummy_input.to(device)input_names = ['actual_input']output_names = ['output']torch.onnx.export(model, dummy_input, 'resnet18.onnx', verbose=True, input_names=input_names,output_names=output_names)if __name__ == '__main__':pth2onnx()resnet18網(wǎng)絡(luò)結(jié)構(gòu)圖
我目前看了下visdom實(shí)現(xiàn)pytorch的網(wǎng)絡(luò)結(jié)構(gòu)查找還是很困難,在stackflow上有很多人使用自己編寫的基于matplotlib來(lái)實(shí)現(xiàn)網(wǎng)絡(luò)結(jié)構(gòu)可視化適用性也不是很好,后來(lái)查找到使用基于tensorboard所開發(fā)的tensorboardX可以很方便的實(shí)現(xiàn)pytorch網(wǎng)絡(luò)結(jié)構(gòu)的可視化,因此決定采用這種方式。
Pytorch的網(wǎng)絡(luò)結(jié)構(gòu)可視化:TensorBoardX
1.TensorBoardX簡(jiǎn)介
tensorboardX的項(xiàng)目路徑:https://github.com/lanpa/tensorboardX
tensorboardX是基于tensorboard的思想用來(lái)寫tensorboard events的工具,可以實(shí)現(xiàn)對(duì)傳統(tǒng)的tensorboard中 scalar,image,figure,histogram,audio,text,graph,onnx_graph等事件進(jìn)行編寫。
tensorboardX同時(shí)具有論壇供大家提出問(wèn)題解決問(wèn)題 ,論壇地址:https://github.com/lanpa/tensorboardX/wiki
2. tensorboardX的使用
tensorboardX的安裝以及依賴如下所示:
pip install tensorboardpip install tensorflowpip install tensorboardXtensorboardX的路徑下帶的有一個(gè)規(guī)范的demo,可以供大家參考。我這里公布一個(gè)我自己測(cè)試過(guò)的代碼,代碼來(lái)源于:https://blog.csdn.net/sunqiande88/article/details/80155925
# -- coding: utf-8 -- import torch import torch.nn as nn from tensorboardX import SummaryWriterclass LeNet(nn.Module):def __init__(self):super(LeNet, self).__init__()self.conv1 = nn.Sequential( # input size=(1*28*28)nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, stride=1, padding=2),nn.ReLU(), # (6*28*28)nn.MaxPool2d(kernel_size=2, stride=2), # output_size(6*14*14))self.conv2 = nn.Sequential(nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5),nn.ReLU(), # (16*10*10)nn.MaxPool2d(kernel_size=2, stride=2) # output_size=(16*5*5))self.fc1 = nn.Sequential(nn.Linear(in_features=16 * 5 * 5, out_features=120),nn.ReLU())self.fc2 = nn.Sequential(nn.Linear(in_features=120, out_features=84),nn.ReLU())self.fc3 = nn.Linear(84, 10)def forward(self, x):x = self.conv1(x)x = self.conv2(x)# Linear的輸入和輸出都是維度為1的值,所以要把多維度的tensor展平成一維x = x.view(x.size()[0], -1)x = self.fc1(x)x = self.fc2(x)x = self.fc3(x)return xdummy_input = torch.rand(4, 1, 28, 28) # 假設(shè)輸入4張1*28*28的圖片 model = LeNet() with SummaryWriter(comment='LeNet') as w:w.add_graph(model, (dummy_input,))運(yùn)行該代碼后會(huì)自動(dòng)生成一個(gè)runs文件夾,并且在文件夾下會(huì)有一個(gè)對(duì)應(yīng)的event,如下圖所示:
此時(shí)需要在terminal或者cmd下運(yùn)行tensorboard --logdir = path
此處千萬(wàn)要注意,如果按照上面的參考文檔的方式是會(huì)報(bào)錯(cuò)的:No graph definition files were found 或者 No definition files were found,總之無(wú)法正常顯示網(wǎng)絡(luò)結(jié)構(gòu)圖。
此處的path 是event對(duì)應(yīng)的確切,完整的路徑
在運(yùn)行后會(huì)出現(xiàn)一個(gè)http url,此時(shí)需要將該url 拷貝到chrome下即可看到如下所示框圖
結(jié)構(gòu)框圖如下所示:
此時(shí)雙擊紅圈所示的LeNet模塊即可看到LeNet的細(xì)節(jié)信息,如下所示:
至此即可完成使用tensorboardX 對(duì)pytorch網(wǎng)絡(luò)結(jié)構(gòu)的可視化
TensorBoard is not found.
Just started using Tensorflow, but I am not able to use tensorboard command on my cmd, it gives the error command
C:\Users\tushar\PycharmProjects>tensorboard --logdir="NewTF" 'tensorboard' is not recognized as an internal or external command,operable program or batch file.I had the same problem for tensorflow 1.5.0 and windows10.
Following tensor documentation (“Launching TensorBoard” section), you can try:
python -m tensorboard.main --logdir=[PATH_TO_LOGDIR]參考資料
總結(jié)
以上是生活随笔為你收集整理的Pytorch的网络结构可视化:Netron与TensorBoardX的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Serial communication
- 下一篇: 十九、CI框架之数据库操作delete用