PyTorch转TensorRT流程

1.安装TensorRT

很多教程,可以谷歌

2.安装onnx

sudo apt-get install protobuf-compiler libprotoc-dev
pip install onnx

3.安装 onnx-tensorrt

git clone --recursive https://github.com/onnx/onnx-tensorrt.git
mkdir build
cd build
cmake .. -DTENSORRT_ROOT=<tensorrt_install_dir>
OR
cmake .. -DTENSORRT_ROOT=<tensorrt_install_dir> -DGPU_ARCHS="61"
make -j8
sudo make install

如果遇到bug, `git checkout v5.0,然后删除build文件夹里的内容重新build.

4.准备PyTorch model

简单起见,以torch.save(net,’./model_300.pkl’)形式保存

5. PyTorch model 转 ONNX

import torch
​
model = './model_300.pkl'
​
dummy_input = torch.randn(batch_size, 3, 300, 300, device='cuda')
model = torch.load(model)
torch.onnx.export(model, dummy_input,"mymodel.onnx" , verbose=False)

6.测试ONNX model 是否与PyTorch model 输出一致

你应该在onnx_tensorrt 目录下

import onnx_tensorrt.backend as backend
import cv2
import onnx
import numpy as np
​
​
model = onnx.load("model_300.onnx")
engine = backend.prepare(model, device='CUDA:0')
path = '../Net/test.jpg'
img = cv2.imread(path)
print(img.shape)
img = cv2.resize(img,(300,300))
img = img.transpose(2,0,1)
img = np.ascontiguousarray(img)
img = img[np.newaxis,:]
print(img.shape)
input_data= img.astype(np.float32)
data =np.random.random(size=(3, 300, 300))
#data = data.transpose(2,0,1)
data = data[np.newaxis,:]
#input_data=data.astype(np.float32)
#input_data = np.random.random(size=(1, 3, 300, 300)).astype(np.float32)
output_data = engine.run(input_data)
print(output_data)

7.ONNX 转 TensorRT engine

onnx2trt model_300.onnx -o my_engine.trt

8. 其他 bugs

(1)importError: No module named onnx_tensorrt.backend

去onnx-tensorrt 目录

(2)while trans to ONNX/TensorRT,[8] Assertion failed: get_shape_size(new_shape) == get_shape_size(tensor.getDimensions())

修改 变换时view中的 -1

(3)ImportError: /usr/local/lib/libnvonnxparser.so.0: undefined symbol: _ZNK6google8protobuf7Message11GetTypeNameB5cxx11Ev

这可能帮助你

(4) 如果你使用SSD或者类似,maxpool(ceil_mode) 不支持

修改用padding代替

    原文作者:spectre
    原文地址: https://zhuanlan.zhihu.com/p/74144263
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞