ONNX-TensorRT模型转换代码编译笔记
一、相关依赖版本及链接
在编译onnx-tensorrt项目时如果各依赖项之间版本不匹配编译会报错。由于我下好的TensorRT是7.0版本,所以这里的依赖都是匹配TensoRT7.0的,版本和链接地址都在下面,各部分需要下载后单独编译。
- onnx-tensorrt7.0 代码链接
- onnx 1.6.0 代码链接
- protobuf 3.8.0 代码链接
- TensorRT7.0 代码链接
二、编译protobuf
下载好protobuf代码并解压。
cd protobuf
./autogen.sh
# build 并 install
./configure
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.
# 验证
protoc --version
三、编译ONNX
在上面提供的链接下载onnx,需要放在onnx-tensorrt/third_party文件夹中,然后编译。
cd onnx
mkdir build && cd build
cmake ..
make
四、编译onnx-tensorrt
cd onnx-tensorrt
mkdir build && cd build
cmake .. -D TENSORRT_ROOT=<path to tensorrt> && make -j
五、转换模型
如果版本匹配,编译过程应该很顺利,生成onnx2trt文件。然后参照项目的转换指令就可以转换模型了。
ONNX models can be converted to serialized TensorRT engines using the onnx2trt executable:
onnx2trt my_model.onnx -o my_engine.trt
ONNX models can also be converted to human-readable text:
onnx2trt my_model.onnx -t my_model.onnx.txt
转换过程中如果提示找不到库,需要检测环境变量,或者找到相应so文件复制到当前目录。