RK3399 study notes 1.0.1—python environment Firefly Core-3399pro-jd4 rknn environment construction

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


There is a problem with the official whl package of numpy1.16.3, scipy, and onnx. Do not install it directly, compile it yourself.

1.1 Install Python3.7

sudo apt update #Check updateable files

sudo apt install software-properties-common #Install tools that can add sources

sudo add-apt-repository ppa:deadsnakes/ppa #Add the source, otherwise the python3.7-dev package will not be found

sudo apt install python3.7-dev #Install python3.7

1.2 Modify python to default to python3.7

Because when C++ calls python, it defaults to calling python2.7, which causes many python3.7 syntax errors. So you need to change python’s default soft connection

Check the path python, python3.7 path

which python python3.7

/usr/bin/python

/usr/bin/python3.7

sudo rm /usr/bin/python

sudo rm /usr/bin/python3

Create python3. Soft connection to python&Python3

sudo ln -s /usr/bin/python3.7 /usr/bin/python

sudo ln -s /usr/bin/python3.7 /usr/bin/python3

The Python3.7 environment configuration is completed.

2.0 Install related dependency packages (opencv, numpy, h5py, scipy)

2.0.1 Install and update related dependency packages

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 #Update the version of pip package

pip3 install wheel setuptools #Install Python packaging tools

Error: sudo apt-get update error
No module named ‘apt_pkg’ problem

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 Install and compile opencv, numpy, h5py

Note: When installing the offline installation package directly, you need to verify that the installation is complete to avoid finding the problem later.

(PS: The version of the numpy package that both opencv-python and h5py depend on must be 1.16.3, so the numpy package needs to be installed and compiled first)

Regarding opencv-python, which requires numpy>=1.19.3, you can install opencv first, then uninstall the higher version of numpy, and reinstall numpy==1.16.3. It will not have any impact. You can save opencv compilation time.

pip3 install opencv-python

You can also compile it yourself (steps can be omitted)
Various versions of opencv-python can be downloaded from this linkhttps://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 #Install the source code package for compilation

Note: There are problems with the official packages of numpy, scipy, and onnx. Try to compile them yourself.

pip3 install numpy==1.16.3 #Install the numpy package. After compilation is completed, proceed to the next step.
sudo apt-get install libhdf5-dev
pip3 install h5py==2.8.0 -i https://pypi.tuna.tsinghua.edu.cn/simple #Install the h5py package. After compilation is completed, proceed to the next step. 


#Verify whether the installation is successful
import numpy

numpy.__version__

import h5py

2.0.3 Install and compile scipy

sudo apt-get install gfortran

pip3 install scipy==1.3.0

2.0.4 Install RKNN-Toolkit 1.6.0

Execute the following command, and the system will install and compile fixed version dependency packages according to RKNN version requirements, such as psutil5.6.2 lmdb0.93 onnx1.6.0 scipy>=1.1.0 protobuf3.11.2 Pillow==5.3.0, etc. It will take about 10-30 minutes to compile and install successfully.

(Various versions of RKNN can be downloaded from this linkhttp://repo.rock-chips.com/pypi/simple/, other compiled whl dependency packages cannot be used directly, such as onnx, scipy, numpy, etc. These packages will fail to be imported in python and cause OpenCV, TensorFlow and RKNN-Toolkit to be unusable)

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

2.0.5 Install TensorFlow 1.14.0

download link:http://repo.rock-chips.com/pypi/simple/

Place the downloaded tensorflow-1.14.0-cp37-none-linux_aarch64.whl in the directory, install and compile it, and it will take about ten minutes to compile the grpcio dependency package. Just wait patiently. (If you use pip3 install tensorflow, the latest version of the tensorflow package will be automatically installed. The version of the dependent numpy package conflicts with RKNN requirements, so it needs to be installed manually)

Compiled versions of tensorflow whl packages based on arm can also be downloaded from this address 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 Install matplotlib

download link:http://repo.rock-chips.com/pypi/simple/

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

3.0 Test the demo under the official rknn-toolkit\examples\darknet\yolov3 path

3.1 Test whether each module is normal in 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 Run the official demo to test whether RKNN is successfully installed.

Start withhttps://pjreddie.com/media/files/yolov3.weightsDownload 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'

Solution:

Open
/usr/local/lib/python3.7/dist-packages/tensorflow/contrib/__init__.py

Find "from tensorflow.contrib import cloud" and comment it out

Indent the next line from tensorflow.contrib import cluster_resolver

The next sentence needs to be indented, otherwise the following error code will be prompted:

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

Enter the /example/tflite directory and run test.py to test whether the development environment is normal.

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

Solution:

Update NPU driver:

sudo apt install firefly-3399pronpu-driver

Reboot the device
Run test.py again

Execute test.py in tensorflow and Onnx folders

You can install other dependency packages as needed later. If the example runs successfully, it means that rknn has been installed.

When errors occur when compiling other dependent packages, it may be that the dependent tool chain is missing. You can try the following commands:

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

Related Posts

Basic use of Python Request get post agent

Exception: Python in worker has different version 2.7 than that in driver 3.6

Data analysis–Pandas③

Calculation of pi π in Python

Download and installation of Xshell

Detailed explanation of finding the difference between two dataframes using Pandas

Full platform adb tool installation method

Python3 crawler tutorial-basic use of aiohttp

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*