最近想用PP-YOLO做一个推理部署的项目,新买了个板子老黄的Jetson Xavier NX,准备用Paddle Inference来试试效果~
本期教程教你从0到1,带你动手实操从Jetson Xavier NX系统的烧录,到Paddle Inference预测库的安装,以及源码编译Paddle Inference。每一步已经踩过的坑完完全全记录下来,并且自行重装系统多次验证流程准确性。
再也不用担心CSDN上关于Paddle Inference与Jetson Xavier NX资料少而不能玩转Paddle烦恼啦!
本文章转载至我的AI Studio项目,文章中所有用到的文件都以数据集形式在AI Studio项目中,进入项目即可下载,不要你1分CSDN积分!不花你一分钱!
https://aistudio.baidu.com/aistudio/projectdetail/969585
9.25更新了在Jetson Xavier NX安装PaddleHub~
编译好的JetPack4.4环境下的Paddle Inference预测库与源码编译时配置环境所需的文件已经作为数据集上传到AI Studio中。
项目具体结构如下:
!unzip -o -d work data/data53821/NX_PPInference.zip
Archive: data/data53821/NX_PPInference.zip
extracting: work/for NX/.pip/pip.conf
inflating: work/for NX/get-pip.py
inflating: work/for NX/mask&camera_test.tar.xz
inflating: work/for NX/nx_cmake.sh
extracting: work/for NX/Paddle-release-2.0-beta.zip
inflating: work/for NX/sources.list
inflating: work/for win/DiskGenius.zip
inflating: work/for win/win32diskimager-1.0.0-install.exe
!unzip -o -d work data/data53732/fluid_inference_JetPack4.4.zip
Archive: data/data53732/fluid_inference_JetPack4.4.zip
inflating: work/fluid_inference_install_dir.tar.xz
inflating: work/paddlepaddle_gpu-2.0.0-cp36-cp36m-linux_aarch64.whl
!tree data/
data/
├── data53732
│ └── fluid_inference_JetPack4.4.zip
└── data53821
└── NX_PPInference.zip
2 directories, 2 files
----------------get-pip.py——pip工具安装脚本
----------------mask&camera_test.tar.xz——口罩检测测试TensorRT程序
----------------nx_cmake.sh——源码编译时cmake配置脚本
----------------Paddle-release-2.0-beta.zip——Paddle 2.0.0 Beta源码包
----------------sources.list——apt的清华源
----------------(隐藏文件夹.pip下的pip.conf)——Python的清华源
!tree -a work/
work/
├── fluid_inference_install_dir.tar.xz
├── for NX
│ ├── get-pip.py
│ ├── mask&camera_test.tar.xz
│ ├── nx_cmake.sh
│ ├── Paddle-release-2.0-beta.zip
│ ├── .pip
│ │ └── pip.conf
│ └── sources.list
├── for win
│ ├── DiskGenius.zip
│ ├── .ipynb_checkpoints
│ └── win32diskimager-1.0.0-install.exe
└── paddlepaddle_gpu-2.0.0-cp36-cp36m-linux_aarch64.whl
4 directories, 10 files
4 directories, 10 files
首先下载Jetson Xavier NX的JetPack4.4镜像:https://developer.nvidia.com/embedded/downloads
点击下图位置下载。
顺便注意一下这里的MD5,下载完成后续可用于校验zip包下载是否出现异常。
下载完成后准备SD卡,如果SD卡已经做过其他系统需要清楚所有分区。为了方便大家这里准备了分区工具DiskGenius,在数据集目录下的NX_PPInference.zip里。
首先清除SD卡所有分区,注意不要清错磁盘!操作如下图所示:
然后建立新分区,操作如下图所示:
完成后将刚刚下载好的镜像烧录至SD卡,烧写操作如左图,校验MD5如右图所示(校验步骤可省略,注意是校验zip包的MD5,而不是img文件的MD5)
烧写成功后将SD插入NX上,启动后根据提示配置一下,这一步比较简单则不进行图文讲解,完成后系统会自动重启进入桌面。
然后安装pip,这里我在NX_PPInference.zip里work/for NX/下提供了get-pip.py:
python3 get-pip.py
此时安装完pip不能再终端中使用pip3,只能使用python3 -m pip。需要重启pip3才能生效。
这里暂时先不进行重启,安装jetson-stats——Jetson 状态查看工具,用于查看NX的资源占用情况。完成后一起重启。
安装jetson-stats,:
sudo -H python3 -m pip install jetson-stats
完成后重启。
使用Jetson 状态查看工具方法也很简单,打开终端输入jtop命令,可以看到CPU、GPU,内存、SD卡的使用情况。
也能看到系统里CUDA、cuDNN、TensorRT、OpenedCV等运行库的版本,如下图:
或者在右上角状态栏找到英伟达图标选择15W 6CORE模式,开启最高性能。
sudo nvpmodel -m 2 && sudo jetson_clocks
git clone https://github.com/NVIDIA/nccl.git
make -j6
sudo make install
`
吐槽一下编译、安装NCCL完成后的提示像报错一样,其实不是的,直接sudo make install就好
`
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda
unzip Paddle-release-2.0-beta.zip
删除第一行opencv-python<=4.2.0.32后保存。
因为JetPack4.4本身已经安装好OpenCV4.1.1,没有必要再安装,而且保留的话会在最后安装whl包时无法安装opencv而报错\\\导致Paddle无法安装!///
这个问题可能会在Paddle 2.0.0正式发布时去除对OpenCV的强依赖,这边已经跟Paddle Inference的工程师建议啦~
`
这里已经提供好编译的脚本,复制到Paddle-release-2.0-beta文件夹下,然后`sh nx_cmake.sh`
脚本会自动创建build文件夹并cd进入,然后执行cmake生成Makefile。
`
cd build
make -j6
cmake .. \
-DWITH_CONTRIB=OFF \
-DWITH_MKL=OFF \ #编译支持MKL的预测库
-DWITH_MKLDNN=OFF \ #编译支持MKLDNN的预测库
-DWITH_AVX=OFF \ #是否编译含有AVX指令集的飞桨二进制文件
-DWITH_GPU=ON \ #编译支持GPU的预测库
-DWITH_TESTING=OFF \ #是否开启单元测试
-DCMAKE_BUILD_TYPE=Release \ #编译方式,仅使用预测库设为Release即可
-DON_INFER=ON \ #预测时使用,必须设为ON
-DWITH_PYTHON=ON \ #是否内嵌PYTHON解释器并编译Wheel安装包
-DPY_VERSION=3.6 \ #指定Python版本
-DWITH_XBYAK=OFF \ #使用XBYAK编译,在jetson硬件上编译需要设置为OFF
-DWITH_NV_JETSON=ON #在NV Jetson硬件上编译时需要设为ON
最后编译生成的预测库位于build文件夹下:
pytho预测库:python/dist/paddlepaddle_gpu-2.0.0-cp36-cp36m-linux_aarch64.whl
C++预测库:fluid_inference_install_dir/
fluid_inference_install_dir/version.txt 中记录了该预测库的版本信息,包括Git Commit ID、使用OpenBlas或MKL数学库、CUDA/CUDNN版本号,如:
GIT COMMIT ID:
WITH_MKL: OFF
WITH_MKLDNN: OFF
WITH_GPU: ON
CUDA version: 10.2
CUDNN version: v8.0
CXX compiler version: 7.5.0
WITH_TENSORRT: ON
TensorRT version: v7
Q:cmake时报错:
– The CUDA compiler identification is unknown
CMake Error at CMakeLists.txt:42 (enable_language):
No CMAKE_CUDA_COMPILER could be found.Tell CMake where to find the compiler by setting either the environment
variable “CUDACXX” or the CMake cache entry CMAKE_CUDA_COMPILER to the full
path to the compiler, or to the compiler name if it is in the PATH.
A:未配置cuda环境变量,
gedit ~/.bashrc
后将如下命令复制到最下面:export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH export CUDA_HOME=$CUDA_HOME:/usr/local/cuda
保存后执行
source ~/.bashrc
使环境变量生效
Q:cmake时报错:
CMake Error at python/CMakeLists.txt:112 (message):
patchelf not found, please install it.For Ubuntu, the command is: apt-get install -y patchelf.
A:根据报错提示已经很明确了,缺少patchelf,直接apt安装即可:
sudo apt-get install -y patchelf
Q:编译NCCL时报错:
CMake Error at cmake/nccl.cmake:18 (file):
file failed to open for reading (No such file or directory):/home/nx/Paddle-release-2.0-beta/NCCL_INCLUDE_DIR-NOTFOUND/nccl.h
A:缺少NCCL多卡通信框架,虽然我们的NX是单卡,但是Paddle目前对NCCL依旧存在依赖,执行下列命令源码编译后安装即可
git clone https://github.com/NVIDIA/nccl.git make -j6 sudo make install ` 如果在源码编译NCCL时报错fatal error: cuda_runtime.h: No such file or directory: 这个问题比较玄学,CUDA已经安装,自己也能找到在/usr/local/cuda/include/cuda_runtime.h找到此头文件,最后解决的方案是: 这个问题是因为我在源码编译NCCL之前已经配置好了CUDA的环境变量,我把CUDA环境变量“删掉后”就可以正常编译能找到头文件了,玄学。。。 所有在前面建议先源码编译NCCL后,再配置CUDA环境变量。这个问题我前两次配置环境的时候没有遇到,不知道为啥在第三次遇到了这个问题, 如果大家配置时知道原因的话,欢迎在评论区教育教育我这个菜鸡T^T `
Q:make时卡住怎么办?
A:如果使用make -j6,大概会在23%的卡住。如果卡住Ctrl+C终止编译,然后改make -j4。跑出25%的时候可以再次Ctrl+C终止编译改回make -j6。总之就是哪里卡住就终止,然后降低make线程数,跑通后再make -j6;
在前5%编译卡住的话可能因为在下载第三方包,由于包在外网速度比较慢,可以终止编译再make、或者自行科学上网提升速度。
Q:build到最后100%出现ERROR: …/aarch64-linux-gpn/crtn.o: Too many open files.怎么办?
A:增加系统同一时间最多可开启的文件数至2048,然后再make
ulimit -n 2048
如果不想源码编译,这里提供我自己编译好的whl包位于data/data53732/fluid_inference_JetPack4.4.zip里,pip安装之前需要安装一些依赖环境
sudo apt install python3-dev python3-matplotlib
pip3 install paddlepaddle_gpu-2.0.0-cp36-cp36m-linux_aarch64.whl
使用 python 进入python解释器,输入import paddle.fluid ,再输入 paddle.fluid.install_check.run_check()。
如果出现 Your Paddle Fluid is installed successfully!,说明您已成功安装。
这一点跟PaddlePaddle验证方式一样~
Q:报错Building wheel for matplotlib (setup.py) … error
A:需要安装python3-matplotlib,
sudo apt install python3-matplotlib
后再次执行pip3 install paddlepaddle_gpu-2.0.0-cp36-cp36m-linux_aarch64.whl
Q:报错Building wheel for netifaces (setup.py) … error && Building wheel for regex (setup.py) … error
A:需要安装python3-dev,
sudo apt install python3-dev
后再次执行pip3 install paddlepaddle_gpu-2.0.0-cp36-cp36m-linux_aarch64.whl
我在data/data53821/NX_PPInference.zip里提供一些测试程序,work/for NX/mask&camera_test.tar.xz。
CSI-Camera与Inference-mask_detector
#单目摄像头测试
python3 simple_camera.py
#双目摄像头测试
python3 dual_camera.py
#单目摄像头测试口罩识别
python3 cam_runtime.py
22 config.enable_use_gpu(memory_pool_init_size_mb=100, device_id=0)
23 config.enable_tensorrt_engine(
24 workspace_size = 1<<30,
25 max_batch_size=1, min_subgraph_size=5,
26 precision_mode=AnalysisConfig.Precision.Float32,
27 use_static=True, use_calib_mode=False)
20 #config.disable_gpu()
另外提一下config.enable_tensorrt_engine的参数use_static,设置为True以后会在第一次运行时在模型文件下生成一个_opt_cache文件夹将模型静态化。以便下次快速运行TensorRT加速。
建议在自己的环境首次运行时删除掉两个模型文件夹下的_opt_cache,避免发生奇奇怪怪的错误~
安装好了Paddle Inference,这里再给大家分享一下如何在Jetson Xavier NX安装PaddleHub
之前我以为PaddleHub不能在ARMLinux环境下安装,因为pip安装时候会发生报错。
后来发现PaddleHub的包名后缀是py3-none-any,所以我决定好好看一下到底是什么原因导致无法安装PaddleHub!
pip3 install paddlehub
,安装时会出现如下报错:nx@nx-desktop:~/Downloads$ pip3 install paddlehub
Defaulting to user installation because normal site-packages is not writeable
Collecting paddlehub
Downloading paddlehub-1.8.2-py3-none-any.whl (336 kB)
|████████████████████████████████| 336 kB 214 kB/s
Requirement already satisfied: pandas; python_version >= "3" in /usr/lib/python3/dist-packages (from paddlehub) (0.22.0)
Collecting flask>=1.1.0
Using cached Flask-1.1.2-py2.py3-none-any.whl (94 kB)
Requirement already satisfied: tqdm in /home/nx/.local/lib/python3.6/site-packages (from paddlehub) (4.49.0)
Collecting sentencepiece
Using cached sentencepiece-0.1.91.tar.gz (500 kB)
ERROR: Command errored out with exit status 1:
command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-s_aw3i44/sentencepiece/setup.py'"'"'; file='"'"'/tmp/pip-install-s_aw3i44/sentencepiece/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-p6wyy_8t
cwd: /tmp/pip-install-s_aw3i44/sentencepiece/
Complete output (5 lines):
Package sentencepiece was not found in the pkg-config search path.
Perhaps you should add the directory containing `sentencepiece.pc'
to the PKG_CONFIG_PATH environment variable
No package 'sentencepiece' found
Failed to find sentencepiece pkgconfig
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
仔细阅读报错结果,Failed to find sentencepiece pkgconfig。主要是在安装sentencepiece发生了错误。
于是Baidu搜索查阅资料,在这篇文章下找到了解决方案:https://blog.csdn.net/sinat_33455447/article/details/90265938
sudo apt-get install cmake build-essential pkg-config libgoogle-perftools-dev
git clone https://github.com/google/sentencepiece
cd sentencepiece
mkdir build
cd build
cmake ..
make -j $(nproc)
sudo make install
sudo ldconfig -v
pip3 install paddlehub
至此我们就完成了Jetson Xavier NX下安装PaddleHub~
花费了4、5天终于在Jetson Xavier NX配置好了Paddle Inference和PaddleHub~
整理了全部的资料,来分享给大家~
帮助你能5分钟能快速在NX上安装Paddle Inference以及PaddleHub!
如果你对这个项目里的口罩识别程序感兴趣,欢迎参考我第一篇Jetson的项目《基于Jetson Nano与Paddle Inference实现的口罩识别》
请参考下面的链接:https://aistudio.baidu.com/aistudio/projectdetail/735759
最后,文章难免出现纰漏的地方也希望小伙伴们批评改正,欢迎大家留言与fork哦~
来AI Studio互粉吧等你哦 https://aistudio.baidu.com/aistudio/personalcenter/thirdview/141984
顺便安利一下官方的Demo,包含C++与Python下两种语言的样例程序:
https://github.com/PaddlePaddle/Paddle-Inference-Demo
以及我们的PaddleHub哦~
https://github.com/PaddlePaddle/PaddleHub