Pytorch的网络结构可视化:Netron与TensorBoardX
Pytorch的網絡結構可視化:Netron與TensorBoardX
- Pytorch的網絡結構可視化:Netron
- Pytorch的網絡結構可視化:TensorBoardX
- 1.TensorBoardX簡介
- 2. tensorboardX的使用
- TensorBoard is not found.
- 參考資料
Pytorch的網絡結構可視化:Netron
最近剛剛發現一個非常好用的顯示模型神器Netron
https://github.com/lutzroeder/Netron
https://www.electronjs.org/apps/netron
借助這個工具可以像windows的軟件一樣導入已經訓練好的模型加權重即可一鍵生成
以下是我的一個模型使用該工具可視化結果,只不過目前該工具對于onnx支持非常好,但是pytorch權重轉變為onnx是非常方便的,只需要torch.onnx.export()命令即可導出onnx權重
以下代碼將resnet18-5c106cde.pth轉化為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網絡結構圖
我目前看了下visdom實現pytorch的網絡結構查找還是很困難,在stackflow上有很多人使用自己編寫的基于matplotlib來實現網絡結構可視化適用性也不是很好,后來查找到使用基于tensorboard所開發的tensorboardX可以很方便的實現pytorch網絡結構的可視化,因此決定采用這種方式。
Pytorch的網絡結構可視化:TensorBoardX
1.TensorBoardX簡介
tensorboardX的項目路徑:https://github.com/lanpa/tensorboardX
tensorboardX是基于tensorboard的思想用來寫tensorboard events的工具,可以實現對傳統的tensorboard中 scalar,image,figure,histogram,audio,text,graph,onnx_graph等事件進行編寫。
tensorboardX同時具有論壇供大家提出問題解決問題 ,論壇地址:https://github.com/lanpa/tensorboardX/wiki
2. tensorboardX的使用
tensorboardX的安裝以及依賴如下所示:
pip install tensorboardpip install tensorflowpip install tensorboardXtensorboardX的路徑下帶的有一個規范的demo,可以供大家參考。我這里公布一個我自己測試過的代碼,代碼來源于: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) # 假設輸入4張1*28*28的圖片 model = LeNet() with SummaryWriter(comment='LeNet') as w:w.add_graph(model, (dummy_input,))運行該代碼后會自動生成一個runs文件夾,并且在文件夾下會有一個對應的event,如下圖所示:
此時需要在terminal或者cmd下運行tensorboard --logdir = path
此處千萬要注意,如果按照上面的參考文檔的方式是會報錯的:No graph definition files were found 或者 No definition files were found,總之無法正常顯示網絡結構圖。
此處的path 是event對應的確切,完整的路徑
在運行后會出現一個http url,此時需要將該url 拷貝到chrome下即可看到如下所示框圖
結構框圖如下所示:
此時雙擊紅圈所示的LeNet模塊即可看到LeNet的細節信息,如下所示:
至此即可完成使用tensorboardX 對pytorch網絡結構的可視化
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]參考資料
總結
以上是生活随笔為你收集整理的Pytorch的网络结构可视化:Netron与TensorBoardX的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Serial communication
- 下一篇: 十九、CI框架之数据库操作delete用