记将MobileNet的pytorch模型转换成TorchScript格式出现的问题

这里写自定义目录标题

使用torch.jit.script()将mobilenetv3的pytorch模型转换成TorchScript时出现

RuntimeError:
Tried to access nonexistent attribute or method ‘ne’ of type ‘torch.SeModule’. Did you forget to initialize an attribute in init()?:
File “/home/guanchu5/Desktop/mobilenetv3/mobilenetv3.py”, line 69
out = self.nolinear2(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
if self.se != None:
~~~~~~~~~~~~~~~ <— HERE
out = self.se(out)
out = out + self.shortcut(x) if self.stride==1 else out

原始代码如下:
net = MobileNetV3_Small()
net.eval()
mobilenet = torch.jit.script(net, torch.rand(2,3,224,224))
x = torch.randn(2,3,224,224)
y = net(x)
mobilenet.save(’/home/guanchu5/Desktop/mobilenet.pt’)

检测后发现if self.se != None这段代码出现问题,pycharm的提示显示:使用相等运算符执行与“无”的比较,检验信息:此检验突出显示与无的比较。此类比较应始终使用“is”或“is not”进行,而不是等式运算符。

将if self.se != None替换成if self.se is not None,代码编译成功

你可能感兴趣的:(pytorch,libtorch,pytorch)