Closed xyl3902596 closed 2 years ago
@xyl3902596 try passing net.module
into torch2trt instead. I had to do this for my ERFNet graph
我也遇到了这个问题,后来发现是batchnormal重新计算了均值和方差,当转到trt的时候就报错了。必须要使用net.eval()再进行模型转换
我也遇到了这个问题,后来发现是batchnormal重新计算了均值和方差,当转到trt的时候就报错了。必须要使用net.eval()再进行模型转换
太牛了,多谢!!!!直接就成功了
Traceback (most recent call last): File "yolo2trt.py", line 22, in
model_trt = torch2trt(net, [x])
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch2trt-0.1.0-py3.7.egg/torch2trt/torch2trt.py", line 536, in torch2trt
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
result = self.forward(*input, kwargs)
File "/root/classfiy/door/backbone/resnet18.py", line 74, in forward
return self.model(x)
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
result = self.forward(*input, *kwargs)
File "/root/classfiy/door/backbone/resnet18.py", line 58, in forward
out = self.conv1(x)
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
result = self.forward(input, kwargs)
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch/nn/modules/container.py", line 117, in forward
input = module(input)
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
result = self.forward(*input, **kwargs)
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch/nn/modules/batchnorm.py", line 113, in forward
self.num_batches_tracked = self.num_batches_tracked + 1
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch2trt-0.1.0-py3.7.egg/torch2trt/torch2trt.py", line 284, in wrapper
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch2trt-0.1.0-py3.7.egg/torch2trt/converters/add.py", line 13, in convert_add
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch2trt-0.1.0-py3.7.egg/torch2trt/torch2trt.py", line 152, in add_missing_trt_tensors
File "/opt/miniconda3/envs/tensorrt/lib/python3.7/site-packages/torch2trt-0.1.0-py3.7.egg/torch2trt/torch2trt.py", line 354, in wrapper
TypeError: add_constant(): incompatible function arguments. The following argument types are supported:
class ResidualBlock(nn.Module): def init(self, inchannel, outchannel, stride=1): super(ResidualBlock, self).init() self.left = nn.Sequential( nn.Conv2d(inchannel, outchannel, kernel_size=3, stride=stride, padding=1, bias=False), nn.BatchNorm2d(outchannel), nn.ReLU(inplace=True), nn.Conv2d(outchannel, outchannel, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(outchannel) ) self.shortcut = nn.Sequential() if stride != 1 or inchannel != outchannel: self.shortcut = nn.Sequential( nn.Conv2d(inchannel, outchannel, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(outchannel) )
class ResNet(nn.Module): def init(self, ResidualBlock, num_classes): super(ResNet, self).init() self.inchannel = 16 self.conv1 = nn.Sequential( nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1, bias=False), nn.BatchNorm2d(16), nn.ReLU() ) self.layer1 = self.make_layer(ResidualBlock, 16, 2, stride=1) self.layer2 = self.make_layer(ResidualBlock, 32, 2, stride=2) self.layer3 = self.make_layer(ResidualBlock, 64, 2, stride=2) self.layer4 = self.make_layer(ResidualBlock, 128, 2, stride=2) self.fc = nn.Linear(128, 3)
class ResNet18(nn.Module): def init(self, num_classes=3): super(ResNet18, self).init() self.model = ResNet(ResidualBlock, num_classes= num_classes)
if name == "main": input = torch.ones([1, 3, 224, 224]) net=ResNet18() out=net(input)