先前在开发板上部署了RKNN Toolkit 1.6.0版本,在运行官方提供的demo时存在一些问题,所以这两天在开发板上安装了RKNN Toolkit 1.4.0版本,安装和测试的过程中也遇到了一些问题,故记录在此文章中,方便以后自己部署时或者各位同学参考。
开发环境 | 软件版本/配置 |
---|---|
开发板型号 | Firefly AIO-3399 ProC开发板 (3+16GB) |
开发板操作系统 | Ubuntu 18.04LTS |
开发板固件版本 | AIO-RK3399PROC-UBUNTU18.04-GPT-20200525-1016.img |
RKNN Toolkit版本 | 1.4.0 (rknn_toolkit-1.4.0-cp37-cp37m-linux_aarch64.whl) |
Python版本 | Python 3.7.10 |
电脑远程软件 | Xshell 6&Xftpd 6 |
官方提供的RKNN 1.6.0版本用户手册中,有对系统依赖的说明,如下图:
同时官方的文档也给出了RKNN 1.4.0版本的系统依赖说明,如下图:
对比两张图,可以看到RKNN两个版本有以下三个依赖包的版本不相同:
V1.6.0版本 | V1.4.0版本 |
---|---|
protobuf == 3.11.2’ | protobuf == 3.6.1’ |
‘onnx == 1.6.0’ | ‘onnx == 1.4.1’ |
requests == 2.22.0’ | requests == 3.11’ |
故有些在RKNN 1.6.0环境下编译出的whl包在部署1.4.0版本时不可直接使用,需要再重新编译。
1、RKNN-Toolkit → 基于 Python 环境的模型转换推理开发工具
2、DRV → NPU 和上位机通信的服务
3、API → 调用NPU 硬件的 API 接口
firefly@firefly:~$ dpkg -l | grep 3399pro
ii firefly-3399pronpu-driver 1.3.1 arm64 <insert up to 60 chars description>
更新DRV的版本
sudo apt update
sudo apt install firefly-3399pronpu-driver
更新完后,检查DRV的版本已升级至1.6.0
firefly@firefly:~$ dpkg -l | grep 3399pro
ii firefly-3399pronpu-driver 1.6.0 arm64 <insert up to 60 chars description>
使用Xshell 6 SSH登录开发板,进行下列操作,防止后续编译软件包时因系统内存不足导致编译失败
(谨慎使用,最新固件AIO-RK3399PROC-UBUNTU-20210304-1705.img关闭图形界面后会导致系统无法启动,串口显示如下报错信息:
4189 串口[ 4.772713] 00000040: 000002a5 000002a5 00000000 00000000 00000000 00000000 00000000 00000000
4190 串口[ 4.773490] 00000060: 00000004 00000004 00000753 00000753 00000000 00000000 00000000 00000000
4191 串口[ 4.774254] 00000080: 00000000 00000000
4192 串口[ 4.820522] ---[ end Kernel panic - not syncing: Attempted to kill init! exitcode=0x00007f00
4193 串口[ 4.820522]
4194 串口DDR Version 1.24 20191016
AIO-RK3399PROC-UBUNTU18.04-GPT-20200525-1016.img该版本可以正常操作)
sudo systemctl set-default multi-user.target
sudo reboot
#未关GUI界面时的系统占用
Memory usage: 12 % of 1893MB
Usage of /: 24% of 15G
#关闭GUI界面时的系统占用
Memory usage: 4 % of 1893MB
Usage of /: 24% of 15G
(PS:待RKNN Toolkit安装完成后可再手动开启用户界面
sudo systemctl set-default graphical.target
sudo reboot
)
sudo apt update #检查可更新文件
sudo apt install software-properties-common #安装可添加源的工具
sudo add-apt-repository ppa:deadsnakes/ppa #添加源,否则会无法找到python3.7-dev软件包
sudo apt install python3.7-dev #安装python3.7
(开发板系统中同时存在多个 Python(Python2.7&python3.6)版本,建议使用 virtualenv来管
理 Python环境)
#创建 virtualenv环境
sudo apt install virtualenv #安装virtualenv软件
virtualenv -p /usr/bin/python3.7 venv #创建虚拟环境
source venv/bin/activate #激活venv环境
(venv) firefly@firefly:~$ pip3 -V #查看当前pip3所在Python的路径
pip 21.0.1 from /home/firefly/venv/lib/python3.7/site-packages/pip (python 3.7)pip
修改python默认为python3.7:
为了因为C++调用python时,默认是调用python2.7,这导致很多python3.7的语法报错。所以需要更改python默认软连接
#查看路径python,python3.7路径
which python python3.7
/usr/bin/python
/usr/bin/python3.7
sudo rm /usr/bin/python
sudo rm /usr/bin/python3
#创建python3.软连接到python&Python3
sudo ln -s /usr/bin/python3.7 /usr/bin/python
sudo ln -s /usr/bin/python3.7 /usr/bin/python3
至此Python3.7环境配置完成。
sudo apt-get update
sudo apt-get install cmake gcc g++ libprotobuf-dev protobuf-compiler libgfortran5-dbg libopenblas-dev gfortran libprotoc-dev
sudo apt-get install liblapack-dev libjpeg-dev zlib1g-dev
sudo apt-get install libhdf5-dev
pip3 install --upgrade pip #更新pip包的版本
pip3 install wheel setuptools #安装 Python 打包工具
(PS:opencv-python和h5py同时依赖的numpy包的版本必须是1.16.3,故需先安装编译numpy包。较难编译和费时较长的whl包已上传至网盘,如有需要,可从文末的链接下载到PC端,通过Xftpd软件上传至开发板安装目录,离线安装即可
)
重要 :安装包的顺序不可调换,否则默认安装的依赖包版本与RKNN所要求的冲突
执行以下命令:
pip3 install numpy==1.16.3 #安装numpy包,编译完成即可进行下一步
#或将离线包下载后安装
#pip3 install numpy-1.16.3-cp37-cp37m-linux_aarch64.whl
pip3 install h5py==2.8.0 -i https://pypi.tuna.tsinghua.edu.cn/simple #安装h5py包,编译完成即可进行下一步
#或将离线包下载后安装h5py
#pip3 install h5py-2.8.0-cp37-cp37m-linux_aarch64.whl
安装并编译opencv-Python包,如果不指定版本,默认会从清华源下载已编译好的最新版本包opencv_python-4.5.1.48-cp37-cp37m-manylinux2014_aarch64.whl (34.5 MB),但该包默认依赖numpy的版本要求numpy>=1.19.3,RKNN Toolkit依赖的numpy版本必须是1.16.3,所以不可安装最新版本的opencv_python-4.5.1.48版本,需下载4.3.0.38版本的opencv-python源码包编译
opencv-python的各个版本可此链接下载https://pypi.tuna.tsinghua.edu.cn/simple/opencv-python/
pip3 install opencv-python==4.3.0.38 -i https://pypi.tuna.tsinghua.edu.cn/simple #安装源码包进行编译
[2021/ 21:43:23] Building wheels for collected packages: opencv-python
...
[2021/ 22:51:49] Building wheel for opencv-python (PEP 517) ... done
[2021/ 22:51:49] Successfully built opencv-python
[2021/ 22:51:49] Installing collected packages: opencv-python
[2021/ 22:51:50] Successfully installed opencv-python-4.3.0.38
#或将离线包下载后安装opencv-python
#pip3 install opencv_python-4.3.0.38-cp37-cp37m-linux_aarch64.whl #
大约要1小时左右方可编译完成,编译好的opencv_python-4.3.0.38-cp37-cp37m-linux_aarch64.whl包可从文末链接处获取。
执行以下命令,系统会根据RKNN的版本要求安装编译固定版本的依赖包,如 onnx1.4.1 (与1.6.0的版本所依赖的onnx包版本不同)scipy>=1.1.0 protobuf3.11.2 Pillow==5.3.0等。大概10-30分钟左右编译安装成功。(RKNN的各个版本可从此链接下载http://repo.rock-chips.com/pypi/simple/,其他编译好的whl依赖包不可直接用到AIO-3399ProC上,如onnx,scipy,numpy等,这些包在python中会因未知原因导入失败,并导致OpenCV、TensorFlow和RKNN-Toolkit无法使用)
(venv) firefly@firefly:~/RKNN1.6$ pip3 install rknn_toolkit-1.4.0-cp37-cp37m-linux_aarch64.whl
Successfully installed Jinja2-2.11.3 MarkupSafe-1.1.1 Pillow-5.3.0 PyYAML-5.4.1 Werkzeug-1.0.1 certifi-2020.12.5 chardet-3.0.4 click-7.1.2 decorator-4.4.2 dill-0.2.8.2 flask-1.0.2 flatbuffers-1.10 idna-2.8 itsdangerous-1.1.0 lmdb-0.93 networkx-1.11 onnx-1.4.1 onnx-tf-1.2.1 ply-3.11 protobuf-3.6.1 psutil-5.6.2 requests-2.22.0 rknn-toolkit-1.4.0 ruamel.yaml-0.15.81 scipy-1.5.4 typing-3.7.4.3 typing-extensions-3.7.4.3 urllib3-1.25.11
将下载好的tensorflow-1.14.0-cp37-none-linux_aarch64.whl 放置目录下,安装并编译。(若使用pip3 install tensorflow 会自动安装最新版本的tensorflow包,依赖的numpy包的版本与RKNN要求冲突,故需手动安装)
基于arm的已编译好的各版本tensorflow whl包也可从此地址下载https://github.com/lhelontra/tensorflow-on-arm/releases。
此步骤在编译grpcio时报错,导致无法成功安装tensorflow,具体报错日志如下:
Building wheel for grpcio (setup.py) ... error
ERROR: Command errored out with exit status 1:
raise CompileError(msg)
distutils.errors.CompileError: command 'aarch64-linux-gnu-gcc' failed with exit status 4
----------------------------------------
ERROR: Command errored out with exit status 1: /home/firefly/venv/bin/python3.7 -u -c 'import sys, setuptools, tokenize; ...
此时从清华源下载的gprcio源码包为grpcio-1.36.1.tar.gz,按以往调试经验安装了下列依赖包,grpcio仍然无法编译成功
(venv) firefly@firefly:~/RKNN1.4$ sudo apt-get install gcc
(venv) firefly@firefly:~/RKNN1.4$ sudo apt-get install Cython
(venv) firefly@firefly:~/RKNN1.4$ sudo apt-get install libhdf5-dev
通过查看官方发布的RKNN docker镜像中安装的包版本,发现grpcio版本为1.34.0,故尝试降低grpcio版本,pip3重新安装编译grpcio,具体日志如下:
(venv) firefly@firefly:~/RKNN1.4$ pip3 install grpcio==1.34.0
Building wheels for collected packages: grpcio
Building wheel for grpcio (setup.py) ... done
Created wheel for grpcio:
uccessfully built grpcio
Installing collected packages: grpcio
Successfully installed grpcio-1.34.0
再执行下列命令,重新安装tensorflow即可。
(venv) firefly@firefly:~/RKNN1.6$ pip3 install tensorflow-1.14.0-cp37-none-linux_aarch64.whl
...
Successfully installed absl-py-0.12.0 astor-0.8.1 gast-0.4.0 google-pasta-0.2.0 keras-applications-1.0.8 keras-preprocessing-1.1.2 tensorboard-1.14.0 tensorflow-1.14.0 tensorflow-estimator-1.14.0 termcolor-1.1.0 wrapt-1.12.1
在测试官方给出的rknn-toolkit\examples\darknet\yolov3路径下的demo时,报如下错误:
raceback (most recent call last):
File "test.py", line 5, in <module>
from matplotlib import gridspec
ModuleNotFoundError: No module named 'matplotlib'
可知需要安装Matplotlib依赖包,官方的docker镜像中,安装的Matplotlib版本为3.0.3,故指定该版本手动安装,但在编译过程中报错,无法成功安装,具体日志如下:
(venv) firefly@firefly:~/RKNN1.4/examples/darknet/yolov3$ pip3 install matplotlib==3.0.3 -i https://pypi.tuna.tsinghua.edu.cn/simple
ERROR: Command errored out with exit status 1:
command: /home/firefly/venv/bin/python3.7 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-zyrjt7ya/matplotlib_d32c2949086446449427874a51fff411/setup.py'"'"'; __file__='"'"'/tmp/pip-install-zyrjt7ya/matplotlib_d32c2949086446449427874a51fff411/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-v_dy1bs_
Complete output (43 lines):
============================================================================
Edit setup.cfg to change the build options
BUILDING MATPLOTLIB
matplotlib: yes [3.0.3]
python: yes [3.7.10 (default, Feb 20 2021, 21:17:23) [GCC
7.5.0]]
platform: yes [linux]
REQUIRED DEPENDENCIES AND EXTENSIONS
numpy: yes [version 1.16.3]
install_requires: yes [handled by setuptools]
libagg: yes [pkg-config information for 'libagg' could not be found. Using local copy.]
freetype: no [The C/C++ header for freetype2 (ft2build.h) could not be found. You may need to install the
development package.]
png: yes [version 1.6.34]
qhull: yes [pkg-config information for 'libqhull' could not be found. Using local copy.]
OPTIONAL SUBPACKAGES
sample_data: yes [installing]
toolkits: yes [installing]
tests: no [skipping due to configuration]
toolkits_tests: no [skipping due to configuration]
OPTIONAL BACKEND EXTENSIONS
agg: yes [installing]
tkagg: yes [installing; run-time loading from Python Tcl /
Tk]
macosx: no [Mac OS-X only]
windowing: no [Microsoft Windows only]
]
OPTIONAL PACKAGE DATA
dlls: no [skipping due to configuration]
============================================================================
* The following required packages can not be built:
* freetype
* Try installing freetype with `apt-get install
* libfreetype6-dev` and pkg-config with `apt-get
* install pkg-config`
----------------------------------------
Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement matplotlib==3.0.3
ERROR: No matching distribution found for matplotlib==3.0.3
由The C/C++ header for freetype2 (ft2build.h) could not be found,You may need to install the development package.报错日志可推测,是在开发环境缺少所依赖的freetype包,执行以下命令进行安装,具体日志如下:
sudo apt-get install pkg-config
sudo apt-get install libfreetype6-dev
pip3 install matplotlib==3.0.3 -i https://pypi.tuna.tsinghua.edu.cn/simple
Building wheel for matplotlib (setup.py) ... done
Created wheel for matplotlib: filename=matplotlib-3.0.3-cp37-cp37m-linux_aarch64.whl
Stored in directory: /home/firefly/.cache/pip/wheels/8a/bb/44/9d6804fb3c73fc4f624e76721ed032411f725ac812b070b5b9
Successfully built matplotlib
Installing collected packages: python-dateutil, pyparsing, kiwisolver, cycler, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.3.1 matplotlib-3.0.3 pyparsing-2.4.7 python-dateutil-2.8.1
成功安装了Matplotlib,可继续测试官方的yolo v3 demo。
因项目暂时用不上PyTorch,故先不安装,可参考下列链接进行编译安装。
https://github.com/yunfeizu/RK3399Pro-pytorch-opencv/
https://blog.csdn.net/zengwubbb/article/details/110936687
venv) firefly@firefly:~/RKNN1.6$ python3
Python 3.7.10 (default, )
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import h5py
>>> h5py.__version__
'2.8.0'
>>> import cv2
>>> import numpy
>>> from rknn.api import RKNN
>>>import tensorflow as tf
>>>tf.__version__
>>>1.14.0
>>>
(venv) firefly@firefly:~/RKNN1.6/examples/tensorflow/ssd_mobilenet_v1$ python3 test.py
Traceback (most recent call last):
File "test.py", line 61, in <module>
... ...
File "/home/firefly/venv/lib/python3.7/site-
packages/tensorflow/contrib/__init__.py", line 31, in <module>
from tensorflow.contrib import cloud
ImportError: cannot import name 'cloud' from 'tensorflow.contrib'
出现报错,参考此链接文章,将/home/firefly/venv/lib/python3.7/site-packages/tensorflow/contrib/init.py出错那行注释掉即可,并且,在下一句还需缩进,不然会提示以下错误代码:
File "/home/firefly/venv/lib/python3.7/site-packages/tensorflow/contrib/__init__.py", line 33
from tensorflow.contrib import cluster_resolver
^
IndentationError: expected an indented block
测试\examples\tensorflow\ssd_mobilenet_v1\路径下的demo,发现报错日志:
...
--> Init runtime environment
...
E restore_signals, start_new_session, preexec_fn)
E OSError: [Errno 12] Cannot allocate memory
Init runtime environment failed
此时新开一个终端窗口,top检测导入模型时的系统资源占用情况,具体如下图:
初步判断是CPU和内存不足,导致RKNN初始化运行环境失败,按照官方文档Rockchip_Trouble_Shooting_RKNN_Toolkit_V1.4.0_CN.pdf的方法,对脚本进行修改,将rknn.build(do_quantization=True, dataset=’./dataset.txt’)行,改为rknn.build(do_quantization=False, dataset=’./dataset.txt’)具体如下:
可以输出处理图片结果,但帧率很低,推测可能是安装的tensorflow 1.14.0版本太高,和RKNN的兼容性不够,导致无法成功运行。
tflite下的demo均可运行成功,如下图所示:
在运行yolo 官方的测试脚本时,发现一直停留在Downloading yolov3.weights…命令行,参考网上的文章,并查看测试脚本,发现需要先从https://pjreddie.com/media/files/yolov3.weights下载yolov3.weights,方可进行下一步测试,而由于未知原因一直无法下载,将此链接放入迅雷中将yolov3.weights文件下载至本地,通过Xftpd软件上传至对应路径即可,在PC端测试的结果如下图:
RKNN1.4.0版本 python3.7环境下安装tensorflow1.14.0版本可能还是存在兼容问题,在进行推理时开发板的系统资源不足导致无法运行,后续打算降到RKNN Toolkit 1.3.0版本,python 3.6环境下安装tensorflow 1.11.0版本,基本上可以运行官方提供的各个demo测试脚本。
部署过程中生成的whl包和下载的yolov3.weights文件如有需要可在下面链接自取,因为每个人的开发环境不同,相应的whl包不保证都能正常使用,对应的whl包仅可在RKNN1.4.0,python3.7环境下安装使用。请谨慎安装。
网盘地址 提取码: xa5y