Pytorch神经网络结构可视化之tensorwatch


使用tensorwatch对网络进行可视化,但是绘制的图有问题,那十有八九就是版本较高带来的问题!

踩坑日记

本来自己使用的pytorch版本是1.8,听说tensorwatch对版本有要求,特意装了一个低版本的pytorch,结果还是掉坑里了。

pytorch=1.4
torchvision=0.5.0
pydot=1.4.2
tensorwatch=0.9.1
上述的版本可以画出图,但是画出的图有问题:

import tensorwatch as tw
import torchvision.models
alexnet_model = torchvision.models.alexnet()
img = tw.draw_model(alexnet_model, [1, 3, 224, 224])
img.save(r'alex.jpg')

类似这样(整个只是图的一部分):
在这里插入图片描述
原因:pytorch的版本较高。

出坑日记

这里提供一个可用的版本:
pytorch=1.2
torchvision=0.4.0
pydot=1.4.2
tensorwatch=0.8.7

一、环境安装

按照上面的版本安装pytorch和tensorwatch的环境。

二、graphviz安装

安装 graphviz这个软件,记住安装的目录,并将bin目录添加到系统环境变量中,在命令行中输入dot.exe,如果不报错的话基本上就ok了。

三、逐个解决错误

搞定之后,执行下面的代码不出意外会出现如下错误:

import tensorwatch as tw
import torchvision.models
alexnet_model = torchvision.models.alexnet()
img = tw.draw_model(alexnet_model, [1, 3, 224, 224])

1、第一个错误:KeyError: 'upsample_bilinear2d’
解决方案:找到这个文件symbolic_opset9.py,目录在E:\Anaconda3\envs\pytorch1.2\Lib\site-packages\torch\onnx。同时找到

upsample_nearest3d = _interpolate('upsample_nearest3d', 5, "nearest")

大概在745行左右,将下面的代码粘贴到上面那行代码的下面,第一个问题成功被解决。

upsample_bilinear1d = _interpolate('upsample_bilinear1d', 3, "linear")
upsample_bilinear2d = _interpolate('upsample_bilinear2d', 4, "linear")
upsample_bilinear3d = _interpolate('upsample_bilinear3d', 5, "linear")

2、第二个错误:FileNotFoundError: [WinError 2] “dot” not found in path.
解决方案
:对pydot.py文件做如下修改。
(1)、set_prog函数
修改前:

def set_prog(self, prog):
    """Sets the default program.

    Sets the default program in charge of processing
    the dot file into a graph.
    """
    self.prog = prog

修改后:

def set_prog(self, prog):
   """Sets the default program.

   Sets the default program in charge of processing
   the dot file into a graph.
   """
   path = r'E:\Graphviz\bin'
   prog = os.path.join(path, prog)
   prog += '.exe'
   # self.prog = prog
   return prog

(2)、create函数
找到下面这段代码,

if prog is None:
     prog = self.prog
 assert prog is not None

在后面添加,

prog = self.set_prog('dot')

最终,大功告成,可以成功的画出想要的图了,给一个最上面的代码画出AlexNet的例子,如下图(仅截取了小部分):
在这里插入图片描述
注:踩坑日记,如果对你有帮助,欢迎点赞!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PyTorch提供了一种可视化神经网络结构的方法,可以使用`torchsummary`库来实现。首先,你需要安装`torchsummary`库,可以使用以下命令进行安装: ``` pip install torchsummary ``` 安装完成后,你可以按照以下步骤进行卷积神经网络结构可视化: 1. 导入需要的库: ```python import torch import torch.nn as nn from torchsummary import summary ``` 2. 定义你的卷积神经网络模型: ```python class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1) self.relu1 = nn.ReLU() self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1) self.relu2 = nn.ReLU() self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) self.fc1 = nn.Linear(128 * 8 * 8, 1024) self.relu3 = nn.ReLU() self.fc2 = nn.Linear(1024, 10) def forward(self, x): x = self.conv1(x) x = self.relu1(x) x = self.pool1(x) x = self.conv2(x) x = self.relu2(x) x = self.pool2(x) x = x.view(-1, 128 * 8 * 8) x = self.fc1(x) x = self.relu3(x) x = self.fc2(x) return x model = Net() ``` 3. 使用`summary`函数来可视化模型结构: ```python summary(model, input_size=(3, 32, 32)) ``` 这将输出模型的详细结构信息,包括每一层的输入形状、参数数量等。 需要注意的是,`input_size`参数需要根据你的输入数据的形状进行调整。在上面的示例中,假设输入数据是3通道的32x32图像。 希望这能回答你的问题!如果有其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值