Skip to content

Latest commit

 

History

History
145 lines (104 loc) · 3.34 KB

onnx_installation.md

File metadata and controls

145 lines (104 loc) · 3.34 KB

ONNX-TensorRT安装教程

因为我们的模型是按照PyTorch—>onnx—>TensorRT的路线进行转化,所以在安装TensorRT之后,需要进一步安装onnx环境。

1 环境准备

1.1 TensorRT安装

TensorRT的安装可以参考—TensorRT快速开始

1.2 protobuf>= 3.8.x

onnx-tensorrt编译时依赖protobuf,在编译之前需要保证已经成功安装。

  • Ubuntu安装:
sudo apt-get install libprotobuf-dev protobuf-compiler
  • Centos安装:
sudo yum install libprotobuf-dev protobuf-compiler
  • 源码安装:
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh

./configure
make
make check
sudo make install
sudo ldconfig

1.3 gcc=8.2

针对onnx-tensorrtTensorRT 7.0,分别尝试过用gcc 4.8.2gcc 5.3以及gcc 8.2进行编译,只有gcc 8.2可编译成功。

1.4 pycuda

可选,如果有python安装需求的必须安装。

pip install pycuda

2 编译安装

2.1 onnx-tensorrt下载

git clone --recursive https://github.com/onnx/onnx-tensorrt.git

注意:参数--recursive不可省略,保证onnx-tensorrt中的第三方依赖third_party/onnx可成功下载。

2.2 编译&安装

cd onnx-tensorrt
mkdir build && cd build
# cmake时可能会出现诸多问题,解决方法可参考QA部分
cmake .. -DProtobuf_INCLUDE_DIR=/path/to/protobuf/src -DTENSORRT_ROOT=/path/to/TensorRT-7.0.0.11

make -j8
sudo make install

若安装成功,则显示如下:

/usr/bin/make64 MAC=64 install
[  2%] Built target gen_onnx_proto
[ 12%] Built target onnx_proto
[ 22%] Built target nvonnxparser_static
[ 33%] Built target nvonnxparser
[ 93%] Built target onnx
[ 95%] Built target onnx2trt
[100%] Built target getSupportedAPITest
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/bin/onnx2trt
-- Set runtime path of "/usr/local/bin/onnx2trt" to ""
-- Installing: /usr/local/lib/libnvonnxparser.so.7.0.0
-- Installing: /usr/local/lib/libnvonnxparser.so.7
-- Set runtime path of "/usr/local/lib/libnvonnxparser.so.7.0.0" to ""
-- Installing: /usr/local/lib/libnvonnxparser.so
-- Installing: /usr/local/lib/libnvonnxparser_static.a
-- Installing: /usr/local/include/NvOnnxParser.h

2.3 安装python库

1)修改setup.py文件

找到第52行,添加-I{/path/to/TensorRT-7.0.0.11/include}

52 SWIG_OPTS = [
53     '-I/path/to/TensorRT-7.0.0.11/include',
54     '-c++',
55     '-modern',
56     '-builtin',
57 ]

2)安装

cd onnx-tensorrt
python setup.py install

若安装成功,则显示如下:

Using /xxx/3.6.5/lib/python3.6/site-packages
Finished processing dependencies for onnx-tensorrt==0.1.0

3 验证

>>> import onnx
>>> import onnx_tensorrt.backend as backend

参考

QA