RK3399学习笔记 1.0.1---python环境 Firefly Core-3399pro-jd4 rknn环境搭建

RK3399学习笔记 1.0.1---python环境 Firefly Core-3399pro-jd4 rknn环境搭建,第1张

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录
  • 官方numpy1.16.3,scipy,onnx的whl包有问题,不要直接安装,自己编译。
  • 1.1安装Python3.7
  • 1.2修改python默认为python3.7
    • 2.0 安装相关依赖包(opencv、numpy、h5py 、scipy)
    • 注:直接安装离线安装包安装完成要验证一下,避免后续找不到问题出在哪。
    • 3.0 测试官方给出的rknn-toolkit\examples\darknet\yolov3路径下的demo


官方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 安装相关依赖包(opencv、numpy、h5py 、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 安装并编译opencv、numpy、h5py

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

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

关于opencv-python要求numpy>=1.19.3,可以先装上opencv之后再卸载高版本numpy,重新安装numpy==1.16.3.不会造成影响。可以省去opencv编译时间。

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

注意: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 安装并编译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依赖包不可直接用,如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.0 测试官方给出的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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/791860.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存