网络搭建-tensorRT c++ api和python pytorch api的对比

38 篇文章 21 订阅
6 篇文章 1 订阅
该博客对比了使用Python PyTorch API和TensorRT C++ API实现经典LeNet5网络的过程。从初始化到前向传播,详细展示了卷积、池化、全连接层及激活函数的添加和配置。通过此对比,读者可以理解两种不同平台下深度学习模型的构建方式。
摘要由CSDN通过智能技术生成

以lenet5为例(2个卷积 + 3个全连接层)。

1.Python api (pytorch)

    def __init__(self):
        super(Lenet5, self).__init__()

        self.conv1 = nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=0)
        self.pool1 = nn.AvgPool2d(kernel_size=2, stride=2, padding=0)
        self.conv2 = nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=0)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # tmp = torch.ones(1, 1, 32, 32).to('cuda:0')     shape: (1, 1, 32, 32)
        
        x = F.relu(self.conv1(x))  # nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=0)  -> [1, 6, 28, 28]

        x = self.pool1(x)          # nn.AvgPool2d(kernel_size=2, stride=2, padding=0)  -> [1, 6, 14, 14]

        x = F.relu(self.conv2(x))  # nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=0) -> [1, 16, 10, 10]

        x = self.pool1(x)          # nn.AvgPool2d(kernel_size=2, stride=2, padding=0)    -> [1, 16, 5, 5]

        x = x.view(x.size(0), -1)  # view                           [1, 16, 5, 5]       -> [1, 400]

        x = F.relu(self.fc1(x))    # nn.Linear(16 * 5 * 5, 120)                         -> [1, 120]

        x = F.relu(self.fc2(x))    # nn.Linear(120, 84)                                 -> [1, 84]
        x = F.softmax(self.fc3(x), dim=1)  # nn.Linear(84, 10)                          -> [1, 10]
        return x

2. python和tensorRT C++ api对比

x = torch.ones(1, 1, 32, 32).to('cuda:0')     shape: (1, 1, 32, 32)
""" c++ creates input tensor
ITensor* data = network->addInput(INPUT_BLOB_NAME, dt, Dims3{1, INPUT_H, INPUT_W});
assert(data);   
"""
   
x = F.relu(self.conv1(x))  # nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=0)  -> [1, 6, 28, 28]
""" c++ conv1 + relu
IConvolutionLayer* conv1 = network->addConvolutionNd(*data, 6, DimsHW{5, 5}, weightMap["conv1.weight"], weightMap["conv1.bias"]);
assert(conv1);
conv1->setStrideNd(DimsHW{1, 1}); 

IActivationLayer* relu1 = network->addActivation(*conv1->getOutput(0), ActivationType::kRELU);
assert(relu1);
"""

x = self.pool1(x)          # nn.AvgPool2d(kernel_size=2, stride=2, padding=0)  -> [1, 6, 14, 14]
""" c++ pool1
IPoolingLayer* pool1 = network->addPoolingNd(*relu1->getOutput(0), PoolingType::kAVERAGE, DimsHW{2, 2});
assert(pool1);
pool1->setStrideNd(DimsHW{2, 2});
"""

x = F.relu(self.conv2(x))  # nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=0) -> [1, 16, 10, 10]
""" c++ conv2 + relu
IConvolutionLayer* conv2 = network->addConvolutionNd(*pool1->getOutput(0), 16, DimsHW{5, 5}, weightMap["conv2.weight"], weightMap["conv2.bias"]);
assert(conv2);
conv2->setStrideNd(DimsHW{1, 1});

IActivationLayer* relu2 = network->addActivation(*conv2->getOutput(0), ActivationType::kRELU);
assert(relu2);
"""

x = self.pool1(x)          # nn.AvgPool2d(kernel_size=2, stride=2, padding=0)      -> [1, 16, 5, 5]
""" c++ pool2
IPoolingLayer* pool2 = network->addPoolingNd(*relu2->getOutput(0), PoolingType::kAVERAGE, DimsHW{2, 2});
assert(pool2);
pool2->setStrideNd(DimsHW{2, 2});
"""

x = x.view(x.size(0), -1)  # view                           [1, 16, 5, 5]       -> [1, 400]
# c++不需要view

x = F.relu(self.fc1(x))    # nn.Linear(16 * 5 * 5, 120)                         -> [1, 120]
""" c++ fc1 + relu
IFullyConnectedLayer* fc1 = network->addFullyConnected(*pool2->getOutput(0), 120, weightMap["fc1.weight"], weightMap["fc1.bias"]);
assert(fc1);

IActivationLayer* relu3 = network->addActivation(*fc1->getOutput(0), ActivationType::kRELU);
assert(relu3);
"""

x = F.relu(self.fc2(x))    # nn.Linear(120, 84)                                 -> [1, 84]
""" c++ fc2 + relu
IFullyConnectedLayer* fc2 = network->addFullyConnected(*relu3->getOutput(0), 84, weightMap["fc2.weight"], weightMap["fc2.bias"]);
assert(fc2);

IActivationLayer* relu4 = network->addActivation(*fc2->getOutput(0), ActivationType::kRELU);
assert(relu4);
"""

x = F.softmax(self.fc3(x), dim=1)  # nn.Linear(84, 10)                          -> [1, 10]
""" c++ fc3 + softmax
IFullyConnectedLayer* fc3 = network->addFullyConnected(*relu4->getOutput(0), OUTPUT_SIZE, weightMap["fc3.weight"], weightMap["fc3.bias"]);
assert(fc3);

ISoftMaxLayer* prob = network->addSoftMax(*fc3->getOutput(0));
assert(prob);
prob->getOutput(0)->setName(OUTPUT_BLOB_NAME);
network->markOutput(*prob->getOutput(0));
"""

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值