onnx -> tensorRT 当下是比较火的 深度学习模型 加速, 验证项目里用到,这里记录一下:
一 升级 protobuf 为 protobuf 3.8
1 2 3 4 5 | git clone https://github.com/google/protobuf -b '3.8.x' apt remove libprotobuf-dev # uninstall old version cd protobuf && ./autogen.sh && ./configure --prefix=/usr/ && make -j6 && make install -j6 |
注: 升级完 python的 protobuf 模块也要更新: (否则 依赖protobuf 的很多py 模型跑不跑 悲剧 了。)
1 2 3 4 5 6 7 | pip3 uninstall protobuf pip install protobuf==3.8.0 #2 onnx-tensorrt git clone --recursive -b 7.1 https://github.com/onnx/onnx-tensorrt.git |
二 下载 onnx-tensorrt 代码:
# 科鞋上网, 注意 选择与 jetpack 4.4.1 兼容的 7.1 版本
1 | git clone --recursive -b 7.1 https://github.com/onnx/onnx-tensorrt.git onnx_tensorrt_7.1 |
三 编译安装 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # build.sh export PATH=/opt/cmake_3_18_4/bin/:/usr/local/cuda/bin:$PATH cmake -DCUDA_INCLUDE_DIRS=/usr/local/cuda/include -DTENSORRT_ROOT=/usr/src/tensorrt -DCMAKE_INSTALL_PREFIX=/usr/ \ .. make -j8 make install # 安装 python模块 cd onnx-tensorrt ## 安装 python模块依赖 apt install swig ## 安装 python模块 python3 setup.py install |
遇到错误如下, 以及解决办法:
https://github.com/onnx/onnx-tensorrt/issues/350
Applied patch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | --- a/NvOnnxParser.h +++ b/NvOnnxParser.h @@ -31,6 +31,10 @@ #define NV_ONNX_PARSER_MINOR 1 #define NV_ONNX_PARSER_PATCH 0 +#ifndef TENSORRTAPI +#define TENSORRTAPI +#endif // TENSORRTAPI + static const int NV_ONNX_PARSER_VERSION = ((NV_ONNX_PARSER_MAJOR * 10000) + (NV_ONNX_PARSER_MINOR * 100) + NV_ONNX_PARSER_PATCH); //! \typedef SubGraph_t diff --git a/setup.py b/setup.py index 8ffa543..d6244a3 100644 --- a/setup.py +++ b/setup.py @@ -59,10 +59,11 @@ EXTRA_COMPILE_ARGS = [ '-std=c++11', '-DUNIX', '-D__UNIX', - '-m64', '-fPIC', '-O2', '-w', + '-march=armv8-a+crypto', + '-mcpu=cortex-a57+crypto', '-fmessage-length=0', '-fno-strict-aliasing', '-D_FORTIFY_SOURCE=2', |