NVIDIA-AI-IOT / torch2trt

An easy to use PyTorch to TensorRT converter
MIT License
4.6k stars 676 forks source link

TypeError: add_constant(): incompatible function arguments #440

Closed xyl3902596 closed 2 years ago

xyl3902596 commented 4 years ago

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:

  1. (self: tensorrt.tensorrt.INetworkDefinition, shape: tensorrt.tensorrt.Dims, weights: tensorrt.tensorrt.Weights) -> tensorrt.tensorrt.IConstantLayer
    
    import torch
    import torch.nn as nn
    import torch.nn.functional as F

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) )

def forward(self, x):
    out = self.left(x)
    out += self.shortcut(x)
    out = F.relu(out)
    return out

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)

def make_layer(self, block, channels, num_blocks, stride):
    strides = [stride] + [1] * (num_blocks - 1)   #strides=[1,1]
    layers = []
    for stride in strides:
        layers.append(block(self.inchannel, channels, stride))
        self.inchannel = channels
    return nn.Sequential(*layers)

def forward(self, x):
    out = self.conv1(x)
    out = self.layer1(out)
    out = self.layer2(out)
    out = self.layer3(out)
    out = self.layer4(out)
    out = F.avg_pool2d(out, 14)
    out = out.view(out.size(0), -1)
    out = self.fc(out)
    return out

class ResNet18(nn.Module): def init(self, num_classes=3): super(ResNet18, self).init() self.model = ResNet(ResidualBlock, num_classes= num_classes)

def forward(self, x):
    return self.model(x)

if name == "main": input = torch.ones([1, 3, 224, 224]) net=ResNet18() out=net(input)



Thanks for your reply!
BernardinD commented 4 years ago

@xyl3902596 try passing net.module into torch2trt instead. I had to do this for my ERFNet graph

huangluyao commented 3 years ago

我也遇到了这个问题,后来发现是batchnormal重新计算了均值和方差,当转到trt的时候就报错了。必须要使用net.eval()再进行模型转换

ZichengDuan commented 3 years ago

我也遇到了这个问题,后来发现是batchnormal重新计算了均值和方差,当转到trt的时候就报错了。必须要使用net.eval()再进行模型转换

太牛了,多谢!!!!直接就成功了