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

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


报错解决:
libcblas.so.3: cannot open shared object file: No such file or directory

sudo apt-get install libatlas-base-dev

官方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 -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout=200#安装 Python 打包工具

sudo apt install python3-pip
sudo apt-get install python-setuptools

报错: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

报错:ModuleNotFoundError: No module named ‘distutils.util’

sudo apt-get install python3.7-distutils
#当前python版本python3.*-distutils

2.0.2 安装并编译opencv、numpy、h5py

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

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

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

pip3 install numpy==1.16.3 -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout=200 #安装numpy包,编译完成即可进行下一步
pip3 install --user -i https://pypi.tuna.tsinghua.edu.cn/simple Cython
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

不安装opencv-contrib-python会rtsp读取不到视频

说明:事实验证发现安装opencv-contrib-python已经编译了ffmpega解码时间大概在600毫秒
pip3 install opencv-contrib-python -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout=200

python3
import cv2
print(cv2.getBuildInformation())
#查看opencv是否编译ffmpeg或gstreamer

-----------------------------------------------------------可以不编译opencv和gstreamer------------------------------------------------------------
18.04系统编译成功了,使用gstreamer加载硬解码时间大概在400-600毫秒,使用mmp时未知原因不加载rga_api(未解决)
20.04默认安装opencv_contrib_python-4.7.0.72-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl ,里面包含ffmpeg

#手动编译opencv-python 调用gstreamer-rockchip使用mpp硬解码

安装gstreamer-rochchip依赖

sudo apt install build-essential automake autoconf autopoint libtool pkg-config git
sudo apt install libgtk-3-dev liborc-0.4-dev libunwind8-dev
sudo apt-get install -y libgstreamer1.0-0 \
            gstreamer1.0-plugins-base \
            gstreamer1.0-plugins-good \
            gstreamer1.0-plugins-bad \
            gstreamer1.0-plugins-ugly \
            gstreamer1.0-libav \
            gstreamer1.0-doc \
            gstreamer1.0-tools \
            libgstreamer1.0-dev \
            libgstreamer-plugins-base1.0-dev

#libunwind8-dev 没有这个会不成功

编译opencv

cd opencv-4.5.0
mkdir build
cd build

设置cmake
注意看执行后的信息,避免浪费时间

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local  \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.7m \
-D PYTHON3_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/python3.7m \
-D PYTHON3_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.7m.so \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D PYTHON_EXECUTABLE=/usr/bin/python3  \
-D PYTHON_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.7/dist-packages/numpy/core/include/ \
-D WITH_GSTREAMER=ON \
-D WITH_GTK=ON  \
-D WITH_GTHREAD=ON \
-D WITH_TBB=ON   \
-D WITH_OPENGL=ON  ..

注意:make -j6后 在 /home/firefly/1/opencv-python-3.4.0.14/opencv/build/lib/ 下是否生成 python3 文件夹
没有生成的话就中断删除build文件下所有内容,检查依赖项,甚至要修改cmake,避免浪费时间不生成cv*.so

make -j6
make install

编译报错:/home/firefly/1/opencv-python-3.4.0.14/opencv/modules/python/src2/cv2.cpp:885:34:
error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
char* str = PyString_AsString(obj);

找到	cv2.cpp		那个文件885行
改为	char* str = (char *)PyString_AsString(obj);
保存,重新sudo make
找到编译地址 /home/firefly/opencv-python-3.4.0.14/opencv/build/lib/python3/
将编译好的cv2.cpython-37m-aarch64-linux-gnu.so
复制到dist-packages(有的话就不用复制了)
import cv2

------------------------------------------------------------------------------------------------------------------------------------------------

2.0.3 安装并编译scipy

pip3 install Cython
sudo apt-get install gfortran
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev libblas-dev

pip3 install scipy==1.3.0 -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout=200

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 -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout=200

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 -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout=200 

pip3 install tensorflow-1.14.0-cp37-none-linux_aarch64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple --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 -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout=200

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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值