> 文章列表 > 项目环境部署(备注:Jetson NX 使用Tensorrt加速自训练yolov4-tiny模型)

项目环境部署(备注:Jetson NX 使用Tensorrt加速自训练yolov4-tiny模型)

项目环境部署(备注:Jetson NX 使用Tensorrt加速自训练yolov4-tiny模型)

这里请注意几个主要问题:

本环境是使用刷机的完全新的环境进行的配置:

  • onnx的版本
  • protcbuf的版本问题
  • 相关的安装顺序的问题

这里默认已经获得了训练好的yolo的模型,这里常用的是在darknet框架,利用谷歌的colab进行在线云端训练YoloV4-Tiny模型,下面会以YoloV4和YoloV4-Tiny举例
如果不行,就加上sudo,给定权限

  1. 第一步是下载github上大佬的相关项目的demo
git clone https://github.com/jkjung-avt/tensorrt_demos.git
  1. 下载完文件后定位到ssd文件夹中执行 install_pycuda.sh
cd ${HOME}/project/tensorrt_demos/ssd
./install_pycuda.sh
  1. 接着需要安装onnx
sudo apt-get install protobuf-compiler libprotoc-dev
sudo pip3 install onnx==1.4.1
  1. 接着定位到plugins文件夹build相关程序
cd ${HOME}/project/tensorrt_demos/plugins
make
  1. 下载并转换yolo文件
    下载模型文件(这里会下载yolov4和yolov3的三个不同版本的文件)
cd ${HOME}/project/tensorrt_demos/yolo
./download_yolo.sh	

(重点补充) . 执行完这个步骤之后,注意,在进行第六步的时候,会报错:
报错信息如下:

TypeError: 1.0 has type numpy.float32, but expected one of: int, long, float

那么,该怎么办呢?这个是因为我们装的onnx和protobuf的版本的问题,当时装的protobuf是低版本的,也正是必须要先装低版本的protobuf你才能装好onnx,不然不可能装好onnx。

移除低版本的protobuf 3.0.0
使用sudo apt-get remove protobuf-compiler libprotoc-dev移除protobuf包。
需要注意,仅执行这个命令是卸载不干净protobuf的,如果你再装其他版本protobuf会导致多版本的protobuf存在。
执行如上指令以后还要删除库和头文件、执行文件:
sudo rm  /usr/local/bin/protoc
sudo rm  -rf /usr/local/include/google
sudo rm /usr/local/lib/libproto*
sudo pip3 uninstall protobuf

这里当你执行的过程中,可能会报错:

nvidia-yolov4@nvidiayolov4:~$ sudo rm  /usr/local/bin/protoc**rm: 无法删除'/usr/local/bin/protoc': 没有那个文件或目录** 
nvidia-yolov4@nvidiayolov4:~$ sudo rm  -rf /usr/local/include/google
nvidia-yolov4@nvidiayolov4:~$ sudo rm /usr/local/lib/libproto***rm: 无法删除'/usr/local/lib/libproto*': 没有那个文件或目录** 
nvidia-yolov4@nvidiayolov4:~$ sudo pip3 uninsall protobuf
ERROR: unknown command "uninsall" - maybe you meant "uninstall"
nvidia-yolov4@nvidiayolov4:~$ sudo pip3 uninstall protobuf
The directory '/home/nvidia-yolov4/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Not uninstalling protobuf at /usr/lib/python3/dist-packages, outside environment /usr
nvidia-yolov4@nvidiayolov4:~$ sudo pip3 uninstall protobuf
The directory '/home/nvidia-yolov4/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Not uninstalling protobuf at /usr/lib/python3/dist-packages, outside environment /usr

上面标注为黑色的错误,注意,只要保证这些位置里面没有我们想要删除的这些文件就OK 了。
Not uninstalling protobuf at /usr/lib/python3/dist-packages, outside environment /usr这个错误,解决方法如下:
没关系,找到/usr/lib/python3/dist-packages这个目录,然后执行下面的这个操作就好
相关参考链接如下:

https://blog.csdn.net/zhubaoguai/article/details/80303456
sudo rm -rf protobuf-3.0.0.egg-info
sudo pip3 uninstall protobuf
The directory '/home/nvidia-yolov4/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Cannot uninstall requirement protobuf, not installed

这个就说明以及卸载好了

安装新版protobuf。(根据大佬的解释,他安装的是3.12.3我也就跟着安装这个版本)
解压、编译、安装
下载的地址如下:
https://github.com/protocolbuffers/protobuf/releases?after=3.15.0-rc1
这里我还把这个文件copy到了主目录下面,因为怕到时候其他地方出问题sudo tar zxvf protobuf-all-3.12.3.tar.gz
cd protobuf-3.12.3
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig
安装protobuf的python模块
cd ./python
python3 setup.py build
python3 setup.py test
sudo python3 setup.py install

总结:要先安装低版本的protobuf,安装成功onnx后,再更新protobuf版本。

  1. 转换yolo文件这里分两个步骤
    1.将.weight模型文件转换成 .onnx 格式
    2.再将 .onnx文件转换成 .trt 格式
python3 yolo_to_onnx.py -m obj-yolov4-tiny
python3 onnx_to_tensorrt.py -m obj-yolov4-tiny -v
  1. 使用TRT运行yolov4、yolov4-tiny
    –usb 代表使用USB摄像机,我的是1,可以通过以下命令查看, --model则是选择特定模型:
ls /dev/video*
cd ${HOME}/project/tensorrt_demos
python3 trt_yolo.py --usb 1 --model obj-yolov4-tiny
ESC 即可退出该行程
注意要修改/home/nvidia-yolov4/tensorrt_demos/utils
该路径下面的yolo_classes.py
要将这个修改为我们的class
只有这样才能保证显示出来的为正确的class
  1. 测试效果
    yolov4在原darknet框架下运行帧率只有1FPS,使用tensorrt后可达到15FPS
    yolov4-tiny在原darknet框架下运行帧率只有10FPS,使用tensorrt后可达到50FPS

本篇文章主要参考以下两篇文章
参考一
参考二