百木园-与人分享,
就是让自己快乐。

Firefly Core-3399pro-jd4 rknn环境搭建

官方numpy1.16.3,scipy,onnx的whl包有问题,不要直接安装,自己编译。

1.1安装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

1.2修改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环境配置完成。

 

2.0 安装相关依赖包(numpy、h5py 、opencv、scipy)

2.0.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 打包工具

 

报错:sudo apt-get update出错

No module named \'apt_pkg\'问题

sudo vi /usr/bin/apt-add-repository

#! /usr/bin/python3.6

cd /usr/lib/python3/dist-packages

sudo cp apt_pkg.cpython-36m-aarch64-linux-gnu.so apt_pkg.so

 

2.0.2 安装并编译numpy、h5py

注:直接安装离线安装包安装完成要验证一下,避免后续找不到问题出在哪。

 

(PS:opencv-python和h5py同时依赖的numpy包的版本必须是1.16.3,故需先安装编译numpy包)

重要 :安装包的顺序不可调换,否则默认安装的依赖包版本与RKNN所要求的冲突

执行以下命令:

 注意:numpy,scipy,onnx官方给的包有问题,尽量自己编译。

pip3 install numpy==1.16.3 #安装numpy包,编译完成即可进行下一步

sudo apt-get install libhdf5-dev
pip3 install h5py==2.8.0 -i https://pypi.tuna.tsinghua.edu.cn/simple #安装h5py包,编译完成即可进行下一步 


#验证是否安装成功
import numpy

numpy.__version__

import h5py

 

2.0.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 #安装源码包进行编译

 

#或将离线包下载后安装opencv-python

#pip3 install opencv_python-4.3.0.38-cp37-cp37m-linux_aarch64.whl #

大约要1小时左右方可编译完成。

 

2.0.4 安装并编译scipy

sudo apt-get install gfortran

pip3 install scipy==1.3.0

2.0.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无法使用)

 

 pip3 install rknn_toolkit-1.6.0-cp37-cp37m-linux_aarch64.whl

 

2.0.5 安装TensorFlow 1.14.0

下载地址:http://repo.rock-chips.com/pypi/simple/

将下载好的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。

pip3 install grpcio==1.36.1 --default-timeout=200 

pip3 install tensorflow-1.14.0-cp37-none-linux_aarch64.whl --default-timeout=200

 

2.0.6 安装matplotlib

下载地址:http://repo.rock-chips.com/pypi/simple/

pip3 install matplotlib-3.2.1-cp37-cp37m-linux_aarch64.whl

 

3、测试官方给出的rknn-toolkit\\examples\\darknet\\yolov3路径下的demo

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

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\'

 

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

 先从https://pjreddie.com/media/files/yolov3.weights下载yolov3.weights

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\'

 

解决方法:

打开

/usr/local/lib/python3.7/dist-packages/tensorflow/contrib/__init__.py 找到 \"from tensorflow.contrib import cloud\",注释掉

缩进下一行 from tensorflow.contrib import cluster_resolver

 

在下一句还需缩进,不然会提示以下错误代码:

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,测试开发环境是否正常

 

firefly@firefly:~/RKNN1.6/examples/tflite/mobilenet_v1$ python3 test.py

--> config model
done
--> Loading model
W:tensorflow:From /usr/local/lib/python3.7/dist-packages/rknn/api/rknn.py:104: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.


W:tensorflow:From /usr/local/lib/python3.7/dist-packages/rknn/api/rknn.py:104: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.


W:tensorflow:From /usr/local/lib/python3.7/dist-packages/rknn/api/rknn.py:104: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.


done
--> Building model
W The target_platform is not set in config, using default target platform rk1808.
W:tensorflow:From /usr/local/lib/python3.7/dist-packages/rknn/api/rknn.py:244: The name tf.reset_default_graph is deprecated. Please use tf.compat.v1.reset_default_graph instead.


W:tensorflow:From /usr/local/lib/python3.7/dist-packages/rknn/api/rknn.py:244: The name tf.FIFOQueue is deprecated. Please use tf.queue.FIFOQueue instead.


W:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/control_flow_ops.py:1814: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.
Instructions for updating:
tf.py_func is deprecated in TF V2. Instead, there are two
options available in V2.
- tf.py_function takes a python function which manipulates tf eager
tensors instead of numpy arrays. It\'s easy to convert a tf eager tensor to
an ndarray (just call tensor.numpy()) but having access to eager tensors
means `tf.py_function`s can use accelerators such as GPUs as well as
being differentiable using a gradient tape.
- tf.numpy_function maintains the semantics of the deprecated tf.py_func
(it is not differentiable, and manipulates numpy arrays). It drops the
stateful argument making all functions stateful.


W:tensorflow:From /usr/local/lib/python3.7/dist-packages/rknn/api/rknn.py:244: The name tf.nn.avg_pool is deprecated. Please use tf.nn.avg_pool2d instead.


W:tensorflow:From /usr/local/lib/python3.7/dist-packages/rknn/api/rknn.py:244: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
done
--> Export RKNN model
done
--> Init runtime environment
E Only support ntb mode on ARM64 platform. But can not find device with ntb mode.
E Catch exception when init runtime!
E Traceback (most recent call last):
E File \"rknn/api/rknn_base.py\", line 815, in rknn.api.rknn_base.RKNNBase.init_runtime
E File \"rknn/api/rknn_runtime.py\", line 170, in rknn.api.rknn_runtime.RKNNRuntime.__init__
E File \"rknn/api/rknn_platform_utils.py\", line 307, in rknn.api.rknn_platform_utils.start_ntp_or_adb
E Exception: Init runtime environment failed!
Init runtime environment failed

解决方法:

更新NPU驱动 :

sudo apt install firefly-3399pronpu-driver

重启设备

再次运行test.py

 

执行tensorflow和Onnx文件夹下的test.py

后续可以根据需要安装其他依赖包,示例运行成功一个就说明rknn已经安装好了。

 

当编译其他依赖包出错时,可能是缺少依赖工具链,可尝试下列命令:

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

 

 

 

4、总结

不同平台,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/路径下,可将这些包导出保存。

 

 

项目可能用到的指令

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 #列出视频设备挂载节点信息

 


来源:https://www.cnblogs.com/wangchuan753/p/16160836.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » Firefly Core-3399pro-jd4 rknn环境搭建

相关推荐

  • 暂无文章