前言

实验室的项目需要用到Firefly的AIO-3399 ProC开发板,该型号开发板集成了硬件加速芯片NPU(神经网络加速单元),经tensorflow、PyTorch等训练好的模型可用官方提供的RKNN Toolkit工具转换为.rknn格式的模型,运行在开发板上利用NPU进行对数据的后向推理加速处理。RKNN Toolkit开发套件安装繁琐,前前后后折腾了一周的时间才算部署成功,故记录下部署过程及遇到的问题,以供自己和各位参考。

一、开发部署环境

开发环境

软件版本/配置

开发板型号

Firefly AIO-3399 ProC开发板 (3+16GB)

开发板操作系统

Ubuntu 18.04LTS

开发板固件版本

AIO-RK3399PROC-UBUNTU18.04-GPT-20200525-1016.img

RKNN Toolkit版本

1.6.0 (rknn_toolkit-1.6.0-cp37-cp37m-linux_aarch64.whl)

Python版本

Python 3.7.10

电脑远程软件

Xshell 6&Xftpd 6

二、RKNN 三件套介绍

1、RKNN-Toolkit → 基于 Python 环境的模型转换推理开发工具
2、DRV → NPU 和上位机通信的服务
3、API → 调用NPU 硬件的 API 接口

三、安装过程

3.1 查看开发板中DRV的版本(1.3.1)

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>

3.2 关闭开发板当前系统的图形界面

使用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
)

3.3 安装Python3.7

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

3.4 用virtualenv管理python版本

(开发板系统中同时存在多个 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环境配置完成。

3.5 安装相关依赖包(numpy、h5py &opencv)

3.5.1 安装并更新相关依赖包

sudo apt-get update
sudo apt-get install cmake gcc g++ libprotobuf-dev protobuf-compiler 
sudo apt-get install liblapack-dev libjpeg-dev zlib1g-dev 
pip3 install --upgrade pip #更新pip包的版本
pip3 install wheel setuptools #安装 Python 打包工具

3.5.2 安装并编译numpy、h5py

(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包报错

/tmp/pip-install-g8zeu2wp/h5py_e742916510854ca6b68f847f94f04c44/h5py/api_compat.h:27:10: fatal error: hdf5.h: No such file or directory
     #include "hdf5.h"
              ^~~~~~~~
     compilation terminated
 error: command 'aarch64-linux-gnu-gcc' failed with exit status 1
   ----------------------------------------
ERROR: Command errored out with exit status 1:

需执行以下操作,安装缺少的依赖包,重新编译h5py

sudo apt-get install libhdf5-dev
pip3 install h5py==2.8.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
#或将离线包下载后安装h5py
#pip3 install h5py-2.8.0-cp37-cp37m-linux_aarch64.whl

3.5.3 安装并编译opencv-python

安装并编译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 --default-timeout=200 #安装源码包进行编译
[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包可从文末链接处获取。

3.5.4 安装RKNN-Toolkit 1.6.0

执行以下命令,系统会根据RKNN的版本要求安装编译固定版本的依赖包,如psutil5.6.2 lmdb0.93 onnx1.6.0 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.6.0-cp37-cp37m-linux_aarch64.whl

3.5.5 安装TensorFlow 1.14.0

将下载好的tensorflow-1.14.0-cp37-none-linux_aarch64.whl 放置目录下,安装并编译,编译grpcio依赖包大约十多分钟,耐心等待即可。(若使用pip3 install tensorflow 会自动安装最新版本的tensorflow包,依赖的numpy包的版本与RKNN要求冲突,故需手动安装)
基于arm的已编译好的各版本tensorflow whl包也可从此地址下载https://github.com/lhelontra/tensorflow-on-arm/releases

(venv) firefly@firefly:~/RKNN1.6$ pip3 install tensorflow-1.14.0-cp37-none-linux_aarch64.whl 
  Building wheel for grpcio (setup.py) ... done
  Successfully installed absl-py-0.11.0 astor-0.8.1 gast-0.4.0 google-pasta-0.2.0 grpcio-1.36.1 importlib-metadata-3.7.2 keras-applications-1.0.8 keras-preprocessing-1.1.2 markdown-3.3.4 tensorboard-1.14.0 tensorflow-1.14.0 tensorflow-estimator-1.14.0 termcolor-1.1.0 wrapt-1.12.1 zipp-3.4.1

3.5.6 安装matplotlib

在测试官方给出的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。

3.5.7 安装PyTorch

因项目暂时用不上PyTorch,故先不安装,可参考下列链接进行编译安装。
https://github.com/yunfeizu/RK3399Pro-pytorch-opencv/


四、测试过程

4.1 在python中测试各模块是否正常

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
 >>>
 >>> import matplotlib
 >>> matplotlib.__version__
'3.0.3'

4.2 运行官方提供的demo,测试RKNN是否安装成功

(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

进入/example/tflite目录下,运行test.py,测试开发环境是否正常

(venv) firefly@firefly:~/RKNN1.6/examples/tflite/mobilenet_v1$ python3 test.py
 --> config model
 done
 --> Loading model
 W:tensorflow:From /home/firefly/venv/lib/python3.7/site-packages/rknn/api/rknn.py:104: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

W:tensorflow:From /home/firefly/venv/lib/python3.7/site-packages/rknn/api/rknn.py:104: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.
...                  ...                    ...
 --> Building model
 W The target_platform is not set in config, using default target platform rk1808.
...                        ...                     
 done
 --> Export RKNN model
[ done
 --> Init runtime environment
 I NPUTransfer: Starting NPU Transfer Client, Transfer version 2.1.0 (b5861e7@2020-11-23T11:50:51)
 D RKNNAPI: ==============================================
 D RKNNAPI: RKNN VERSION:
 D RKNNAPI:   API: 1.6.0 (79320de build: 2020-12-29 10:57:09)
 D RKNNAPI:   DRV: 1.6.0 (159d2d3 build: 2021-01-12 15:23:09)
 D RKNNAPI: ==============================================
 done
 --> Running model
 D RKNNAPI: __can_use_fixed_point: use_fixed_point = 1.
 
 mobilenet_v1
 -----TOP 5-----
 [156]: 0.8505859375
 [155]: 0.0916748046875
 [205]: 0.013580322265625
 [284]: 0.00646209716796875
 [194]: 0.0024871826171875

 done
 --> Evaluate model performance
 W When performing performance evaluation, inputs can be set to None to use fake inputs.
 ========================================================================
                                Performance                              
 ========================================================================
 Total Time(us): 5561
 FPS: 179.82
 ========================================================================
 
 done

执行tensorflow和Onnx文件夹下的test.py时,系统提示

E OSError: [Errno 12] Cannot allocate memory
 Init runtime environment failed

导入模型时同时用top工具动态检测开发板的系统状态,发现CPU占用率达到了100%,内存占用率最高达到了78%,估计是由于开发板的配置内存太小(2GB),内存不足导致demo运行失败,后续可考虑安装RKNN-Toolkit-Lite版本或安装低版本的tensorflow尝试解决此问题。

python onnxruntime python onnxruntime 清除缓存_python onnxruntime


在运行yolo 官方的测试脚本时,发现一直停留在Downloading yolov3.weights…命令行,参考网上的文章,并查看测试脚本,发现需要先从https://pjreddie.com/media/files/yolov3.weights下载yolov3.weights,方可进行下一步测试,而由于未知原因一直无法下载,将此链接放入迅雷中将yolov3.weights文件下载至本地,通过Xftpd软件上传至对应路径即可,正常运行测试脚本。

4.3 编译安装时遇到的报错及可能解决的方法

  • 按照RK提供的安装文档方法,自行编译h5py时,会报错,提示 You must put some ‘source’ URIs in your sources.list,可按下列命令操作解决此问题。
sudo apt-get build-dep python3-h5py && \
pip3 install h5py
sudo vim /etc/apt/sources.list
#复制以下内容到编辑器中,然后ESC ,:wq保存退出
deb-src http://archive.ubuntu.com/ubuntu trusty main restricted #Added by software-properties
deb-src http://gb.archive.ubuntu.com/ubuntu/ trusty restricted main universe multiverse #Added by software-properties
deb-src http://gb.archive.ubuntu.com/ubuntu/ trusty-updates restricted main universe multiverse #Added by software-properties
deb-src http://gb.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse #Added by software-properties
deb-src http://security.ubuntu.com/ubuntu trusty-security restricted main universe multiverse #Added by software-properties
deb-src http://gb.archive.ubuntu.com/ubuntu/ trusty-proposed restricted main universe multiverse #Added by software-properties
sudo apt-get update
sudo apt-get build-dep python3-h5py && \
pip3 install h5py
  • 当编译其他依赖包出错时,可能是缺少依赖工具链,可尝试下列命令:
pip3 install Cython
sudo apt-get install gcc python3-dev
sudo apt-get install libhdf5-dev
sudo apt-get install cmake gcc g++ libprotobuf-dev protobuf-compiler libgfortran5-dbg libopenblas-dev gfortran libprotoc-dev
  • 安装官方提供的基于python3.5版本编译的opencv-headless 4.0.12.3版本时,如果import cv2报错,提示 找不到libjpeg.so.62,可执行以下命令即可解决该问题。
sudo apt update
sudo apt-get install libjpeg62-dev

五、总结

不同平台,RKNN提供的基于不同Python版本的安装包,安装时不同平台对应的依赖关系需要处理好,具体如下:
1.Windows只提供 Python3.6的安装包。
2. MacOS提供 python3.6和 python3.7的安装包。
3. ARM64平台(安装 Debian 9或 10操作系统)提供 Python3.5( Debain 9)和 Python3.7(Debian10)的安装包。
4. Linux_x64平台提供基于 python3.5和 python3.6的安装包。
5.开发板编译好的whl包一般会存放在/.cache/pip/wheels/路径下,可将这些包导出保存。

Building wheel for onnx (setup.py) ... done
 Created wheel for onnx: filename=onnx-1.6.0-cp37-cp37m-linux_aarch64.whl s
   Stored in directory: /home/firefly/.cache/pip/wheels/...
  cp -ri ./wheel* /home/firefly
  1. 项目可能用到的指令
sudo rm -rf ~/.cache/pip  #删除pip3缓存文件
source /home/firefly/venc/bin/activate #进入venc环境
#加速Python pip安装速度,更改Python软件源,操作方法:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install pip -U

#RKNN启动可视化窗口命令  
python3 -m rknn.bin.visualization\

#打印内核调试信息
cat /proc/kmsg
dmesg | grep ov13850 #查看ov13850驱动是否正常
cd /usr/local/bin/  #相机测试脚本路径 
ls /dev/video #列出设备挂载到video节点信息
v4l2-ctl -D #列出摄像头的驱动信息
v4l2-ctl -d /dev/video0 --list-formats-ext #列出视频节点设备所支持的视频格式,分辨率
v4l2-ctl --list-devices #列出视频设备挂载节点信息

部署过程中生成的whl包和下载的yolov3.weights文件如有需要可在下面链接自取,因为每个人的开发环境不同,相应的whl包不保证都能正常使用,对应的whl包仅可在RKNN1.6.0,python3.7环境下安装使用。请谨慎安装。
编译打包好的软件包的下载链接,提取码: a1ca