[Rock Pi] (2) 3588 rknn host-side configuration and docker development environment deployment

reference

Rockchip rknn-tookit2

Introduction to development environment

Host PC: Installed with ubuntu22.04 (others can also be referred to)
Development board: rock 5a with rk3588s chip

Install Docker on the host machine

Docker has the function of isolating the development environment. In order to facilitate the switching of development environments and avoid damaging the stable environment of the host during environment configuration, we choose to use docker for development work, thus avoiding environmental chaos when developing two or more projects at the same time.
This article will not introduce the specific principles and usage of Docker in detail. It can be imagined as virtual machine software such as VMware installed on Windows, with the corresponding system image running on it. It's just that the operation is more convenient, lighter, and more suitable for command line operations. For details, please refer to: Docker .
What needs to be noted is the concept of containers, which are the basic units of system operation.

  1. First, execute the following command on the ubuntu system to install Docker
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
  1. Use the following command to check the status of Docker. If the execution result is as follows, it proves that the Docker service is running normally:
dengml@dengml-SER:/data/ubuntu/rockpi$ systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-07-27 21:26:09 CST; 1 week 6 days ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 43420 (dockerd)
      Tasks: 23
     Memory: 93.6M
        CPU: 3min 27.381s
     CGroup: /system.slice/docker.service
             └─43420 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

  1. If it does not run successfully, you can execute the following command to open it and set it to run automatically at boot.
sudo systemctl start dockersudo systemctl restart docker  #打开docker

sudo systemctl enable docker #设置开机自动启动Docker
  1. Pull the system image (here we are pulling the ubuntu20.04 version image)
sudo docker pull ubuntu:20.04
# 查看本地镜像
$ sudo docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
ubuntu        20.04     14be0685b768   7 weeks ago    72.8MB
hello-world   latest    9c7a54a9a43c   3 months ago   13.3kB
  1. Starting a container with the ubuntu20.04 image can be imagined as creating a virtual machine:
 sudo docker run -it --name rknn -v /data:/data ubuntu:20.04 /bin/bash 

Among them, "-name rknn" means that the container is named rknn. "-v /data:/data" is used to mount the /data directory of the physical machine to the /data directory of the docker virtual container to realize the file system of the two machines. Sharing (to add a shared directory to a running docker, please refer to: docker adds directory mapping (mounting directory) to a started container ). The "-it" parameter indicates that it is allocated and resides in the container terminal. "ubuntu:20.04" is the system image used by the container, and "/bin/bash" is the shell interpreter used by the system.

  1. Exit and enter docker.
    After successful execution, you will enter the terminal of the docker container. The executable exitcommand exits the container and returns to the physical machine system.
    Execute the command on the physical machine sudo docker psto view the running containers.
$ sudo docker ps
CONTAINER ID   IMAGE          COMMAND       CREATED       STATUS       PORTS     NAMES
612c918d5027   ubuntu:20.04   "/bin/bash"   3 weeks ago   Up 3 weeks             rknn

If there are containers that are not running, you can sudo docker ps -aview them through commands.
When entering the docker container again, you do not need to re-execute the docker run command. Instead, you can use the docker exec command to enter.

sudo docker exec -it 612c918d5027 /bin/bash

Among them, "612c918d5027" is the id of the existing container. (If there is a container that is not running, you need to execute "docker start <container id>" first.)
Of course, you can specify the user and the corresponding working directory, because we have not yet created the user and the corresponding home directory, which will be introduced later.

  1. Configure docker
- 安装权限管理工具
  apt install sudo 
- 添加普通用户并指定其家目录及对应的默认解释器
  adduser --home /home/dengml --shell /bin/bash  dengml
- 修改用户密码
  passwd dengml
- 安装文本编辑工具vim
  apt install vim
- 编辑/etc/sudoers文件
  chmod 777 /etc/sudoers #添加编辑权限
  vim /etc/sudoers #在”root  ALL=(ALL:ALL) ALL“下添加”dengml  ALL=(ALL:ALL) ALL“
  chmod u-w /etc/sudoers // 去除可写权限
- 登陆到dengml用户
  su dengml
- 安装自动补全工具
  apt-get install -y bash-completion
  apt update
  source /usr/share/bash-completion/completions/*
- 安装网络工具
  apt install iputils-ping

At this time, we can enter the docker container from the physical machine by specifying the user name and corresponding home directory, as follows

dengml@dengml-SER:~$ sudo docker exec -it -u dengml -w /home/dengml 612c918d5027 /bin/bash
[sudo] dengml 的密码:
dengml@612c918d5027:~$

Build rknn development environment

  1. Pulling the code up on github (performed on docker)
    First, we use ssh to get the code from GitHub (you can also use http, but it is slower?). The first step is to configure ssh-key.
  • Configure ssh-key and
    execute the following command locally to generate and obtain the ssh key.
dengml@612c918d5027:~$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/dengml/.ssh/id_rsa):
Created directory '/home/dengml/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/dengml/.ssh/id_rsa
Your public key has been saved in /home/dengml/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:OW6GGqaLjp+zD+R4hq2tKXnocEJIktnk/tNEXgNdLFg [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|  .   .+Eo.      |
| *    ..o .      |
|+.o   . o.       |
|+.   o . o       |
|..o   o S        |
|.* . o o .       |
|=oB = o +        |
|*Xo= + o         |
|X*B=o            |
+----[SHA256]-----+
dengml@612c918d5027:~$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAA***************xP0YX0= [email protected]

Paste the key obtained above into github:
open and log in to the "github.com" website, click on your avatar in the upper right corner. Click the setting button as follows
setting of github account
and then click the "SSH and GPG keys" and "New SSH Key" buttons.
add ssh key to github
Fill in the title and the key you just obtained in the pop-up box, and finally click the "Add New Key" button. The
add new ssh key
final successful addition is as follows.
add new key result
After configuring the ssh-key, you can pull the code and obtain the development warehouse of Rockchip rknn through the following command.

dengml@612c918d5027:/data/ubuntu/rockpi$ git clone [email protected]:rockchip-linux/rknn-toolkit2.git
Cloning into 'rknn-toolkit2'...
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 475, done.
remote: Counting objects: 100% (67/67), done.
remote: Compressing objects: 100% (57/57), done.
remote: Total 475 (delta 6), reused 66 (delta 6), pack-reused 408
Receiving objects: 100% (475/475), 630.23 MiB | 4.97 MiB/s, done.
Resolving deltas: 100% (132/132), done.
Updating files: 100% (140/140), done.
dengml@612c918d5027:/data/ubuntu/rockpi$ ls
rknn-toolkit2

  1. Install basic development packages
sudo apt install virtualenv
sudo apt-get install python3 python3-dev python3-pip
sudo apt-get install libxslt1-dev zlib1g zlib1g-de
sudo apt-get install libxslt1-dev zlib1g zlib1g-dev libglib2.0-0
sudo apt-get install libsm6 libgl1-mesa-glx libprotobuf-dev gcc

Among them, virtualenv is used to isolate the python environment, so that multiple python environments can be installed in one system. When switching, you only need to execute source <对应python环境名称>/bin/activatecommands. It is very convenient. You can just understand it and learn while using it.

  1. Install the required python environment, because the official documentation recommends the use of the python3.8 development environment on the ubuntu20.04 system. In order to improve development efficiency and avoid reinventing the wheel, we install the python3.8 development environment.

Create a python environment with the environment name "venv" for python3

  dengml@612c918d5027:/data/ubuntu/rockpi/rknn-toolkit2$ virtualenv -p /usr/bin/python3 venv
  created virtual environment CPython3.8.10.final.0-64 in 68ms
  creator CPython3Posix(dest=/data/ubuntu/rockpi/rknn-toolkit2/dengml, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, pkg_resources=latest, via=copy, app_data_dir=/home/dengml/.local/share/virtualenv/seed-app-data/v1.0.1.debian.1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

After the creation is completed, a directory named "venv" will be formed in the directory.
venv
Switch to the python environment just created.

  dengml@612c918d5027:/data/ubuntu/rockpi/rknn-toolkit2$ source venv/bin/activate
  (venv) dengml@612c918d5027:/data/ubuntu/rockpi/rknn-toolkit2$

At this time, there will be a (venv) prefix in front of it, indicating the current python environment.

According to the official introduction, the dependencies required to install python3.8.

(venv) dengml@612c918d5027:/data/ubuntu/rockpi/rknn-toolkit2$ pip3 install -r doc/requirements_cp38-1.5.0.txt

There may be errors during the installation process, as follows:

ERROR: tensorflow 2.6.2 has requirement six~=1.15.0, but you'll have six 1.16.0 which is incompatible.
ERROR: tensorflow 2.6.2 has requirement typing-extensions~=3.7.4, but you'll have typing-extensions 4.7.1 which is incompatible.

The probably reason is that because the version parts match, the version in the current environment is too high and does not meet the requirements for the software package version in the official documents. The solution is to uninstall the corresponding pip package first and install the corresponding package that meets the version requirements as follows:

pip3 uninstall typing-extensions
pip3 install -v typing-extensions==3.7.4

If pip installation fails or is slow due to network connection, probably because the pip source is not in China, just add the -i parameter after pip installation:

pip3 install -v typing-extensions==3.7.4  -i https://pypi.tuna.tsinghua.edu.cn/simple

Finally, install Rockchip's rknn official development package corresponding to python3.8 provided by rockchip.

pip3 install ../../../packages/rknn_toolkit2-1.5.0+1fa95b5c-cp38-cp38-linux_x86_64.whl

Environmental testing

(venv) dengml@612c918d5027:/data/ubuntu/rockpi/rknn-toolkit2/examples/onnx/yolov5$ ls
bus.jpg  dataset.txt  test.py  yolov5s.onnx
(venv) dengml@612c918d5027:/data/ubuntu/rockpi/rknn-toolkit2$ cd examples/onnx/yolov5/
(venv) dengml@612c918d5027:/data/ubuntu/rockpi/rknn-toolkit2/examples/onnx/yolov5$ python3 test.py
......
......
......
(venv) dengml@612c918d5027:/data/ubuntu/rockpi/rknn-toolkit2/examples/onnx/yolov5$ ls
bus.jpg                    check3_fuse_ops.onnx  onnx_yolov5_0.npy  onnx_yolov5_2.npy  test.py       yolov5s.rknn
check0_base_optimize.onnx  dataset.txt           onnx_yolov5_1.npy  result.jpg         yolov5s.onnx

Open the final generated result.jpg.
final result
Next, transplant the model to the development board and run it.

Guess you like

Origin blog.csdn.net/weixin_43328157/article/details/132198689