TensorRT-yolov3部署(python&C++)

一、Python版

使用的是TensorRT 7.0官方python用例,主要包括一下几个过程

  • 1.将Darknet得到的cfg和weights文件转换成onnx模型
  • 2.使用onnx模型生成.trt文件并对图片进行检测
  • 3.切换FP16
  • 1.Darknet-->ONNX

    python yolov3_to_onnx.py
    首先得安装onnx,pip安装即可,然后修改py文件中的一些参数,包括cfg文件、weights文件的路径,以及输出向量的大小等:
    主要原因是TensorRT版本和ONNX版本匹配问题,经多次试验得出结果:

    TensorRT 5.1.5与ONNX 1.4.1相匹配,TensorRT 7.0.0与ONNX 1.7.0相匹配

    使用其他版本会报错。

    sudo apt-get -y --force-yes install python-pycuda

    [TensorRT] WARNING: TensorRT was linked against cuDNN 7.6.3 but loaded cuDNN 7.5.0
    [TensorRT] WARNING: TensorRT was linked against cuDNN 7.6.3 but loaded cuDNN 7.5.0
    [TensorRT] WARNING: Current optimization profile is: 0. Please ensure there are no enqueued operations pending in this context prior to switching profiles
    Running inference on image /media/luopeng/F/TensorRT-7.0.0.11/samples/python/yolov3_onnx/images/nu2.jpg...
    Traceback (most recent call last):
      File "onnx_to_tensorrt.py", line 188, in <module>
        main()
      File "onnx_to_tensorrt.py", line 168, in main
        trt_outputs = [output.reshape(shape) for output, shape in zip(trt_outputs, output_shapes)]
      File "onnx_to_tensorrt.py", line 168, in <listcomp>
        trt_outputs = [output.reshape(shape) for output, shape in zip(trt_outputs, output_shapes)]
    ValueError: cannot reshape array of size 7581 into shape (1,21,13,13)
    

    修改get_engine中模型输入大小和data_processing.py中的类别数量:

  • 1.将Darknet得到的cfg和weights文件转成yolov3.wts(pytorch版yolov3的权重文件)。
  • 2.编译C++版TensorRT for YOLOv3
  • 3.生成yolov3.engine用于推理加速并对图片进行检测
  • 4.切换FP16
  • TensorRT 7.0.0 CUDA10.0 OpenCV with contrib 3.4.5

    1. 生成yolov3.wts

    导入pytorch版的yolov3,将darknet上训练好的yolov3.weights放到该工程,使用gen_wts.py生成yolov3.wts

    git clone https://github.com/wang-xinyu/tensorrtx.git
    git clone https://github.com/ultralytics/yolov3.git
    // download its weights 'yolov3.pt' or 'yolov3.weights'
    cd yolov3
    cp ../tensorrtx/yolov3/gen_wts.py .
    python gen_wts.py yolov3.weights
    // a file 'yolov3.wts' will be generated.
    

    2.编译C++版TensorRT for YOLOv3

    将生成的yolov3.wts放入tensorrtx/yolov3目录,在yolov3.cpp中可修改yolov3.wts路径,NMS thresh,BBox confidence thresh,使用FP16还是FP32等;在yolov3.h中可修改网络输入大小、类别数量等。
    开始编译:

    mkdir build
    cd build
    cmake ..
    

    在编译过程中可能出现的问题:

    fatal error: NvInfer.h: No such file or directory #include "NvInfer.h" ^~~~~~~~~~~ compilation terminated. CMake Error at myplugins_generated_mish.cu.o.Debug.cmake:219 (message): Error generating /media/tensorrtx/yolov4/build/CMakeFiles/myplugins.dir//./myplugins_generated_mish.cu.o CMakeFiles/myplugins.dir/build.make:70: recipe for target 'CMakeFiles/myplugins.dir/myplugins_generated_mish.cu.o' failed make[2]: *** [CMakeFiles/myplugins.dir/myplugins_generated_mish.cu.o] Error 1 CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/myplugins.dir/all' failed make[1]: *** [CMakeFiles/myplugins.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

    原因是TensorRT的头文件没有被找到,解决方法是将TensorRTx.x.x/include加入环境变量或将这些头文件复制到/usr/include下。

    2)能编译通过但在执行的时候报错:
    段错误(核心已转储)
    

    3.生成yolov3.engine用于推理加速并对图片进行检测

    编译完成后

    sudo ./yolov3 -s             // serialize model to plan file i.e. 'yolov3.engine'
    sudo ./yolov3 -d  ../images/      //deserialize plan file and run inference, the images will be processed.
    

    -s:生成推理引擎文件, -d:加载引擎文件开始推理