小亿 发布的文章

  • RV1126 支持4核CPU(armv7l, 32位架构,很多应用不一定支持),2Tops算力(4路IPC摄像头推理,不在话下),低功耗无风扇,是低端 边缘计算 + AI 应用的低成本选择;
  • rknn-toolkit-lite 支持在 RV1126 上跑 python 应用,方便快速开发和部署
  • 我司的 边缘计算+AI 网关 YiGATE A600 就是基于此架构设计而成。

1. 准备工作

RV1126 开发板或整机

  • 一台整机是必不可少的要求,可以从网店购买;
  • 安装好支持 docker 环境的 debian 或 ubuntu系统

X86或ARM64 开发机

  • 最好是 X86 的宿主主机开发,否则交叉编译环境不好处理;
  • ARM64 主机开发比较麻烦,要慎重选择;

RV1126 SDK

  • 找设备供应商获取 RV1126 的 SDK 开发包;
  • 搭建开发环境请参考开发包说明;

2. docker 环境

ubuntu 安装 docker 环境

  • 参照 docker 官方手册,几条命令安装好;
  • 如果需要的话,安装一个 portainer 容器管理器,方便远程 web 管理 image 镜像和容器;

编辑自己的 Dockerfile,用于编译 RV1126 上的运行微服务环境;

  • RV1126 的 rknn-toolkit-lite 需要的环境很特殊,需要 opencv 3.4.3 运行库(SDK版本限制了);
  • 自己的 Dockerfile,可以随意选择需要的安装包,自己编译 docker image,占用空间可控;

3. 示例代码

Dockerfile 参考示例

FROM debian:10-slim

ARG OPENCV_VERSION
ARG DEBIAN_FRONTEND=noninteractive

WORKDIR /opt/build

RUN set -ex \
    && apt-get -qq update \
    && apt-get -qq install -y --no-install-recommends \
        build-essential cmake \
        gcc g++ \
        curl wget unzip \
        libhdf5-103 libhdf5-dev \
        libopenblas-base libopenblas-dev \
        libprotobuf17 libprotobuf-dev \
        libjpeg62-turbo libjpeg62-turbo-dev \
        libpng16-16 libpng-dev \
        libtiff5 libtiff-dev \
        libwebp6 libwebp-dev \
        libopenjp2-7 libopenjp2-7-dev \
        libtbb2 libtbb-dev \
        libeigen3-dev \
        tesseract-ocr tesseract-ocr-por libtesseract-dev \
        python3 python3-pip python3-dev \
    && wget -q --no-check-certificate https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip -O opencv.zip \
    && wget -q --no-check-certificate https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip -O opencv_contrib.zip \
    && unzip -qq opencv.zip -d /opt && rm -rf opencv.zip \
    && unzip -qq opencv_contrib.zip -d /opt && rm -rf opencv_contrib.zip \
    && cmake \
        -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib-${OPENCV_VERSION}/modules \
        -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \
        -D OPENCV_ENABLE_NONFREE=ON \
        -D WITH_JPEG=ON \
        -D WITH_PNG=ON \
        -D WITH_TIFF=ON \
        -D WITH_WEBP=ON \
        -D WITH_JASPER=ON \
        -D WITH_EIGEN=ON \
        -D WITH_TBB=ON \
        -D WITH_LAPACK=ON \
        -D WITH_PROTOBUF=ON \
        -D WITH_V4L=OFF \
        -D WITH_GSTREAMER=OFF \
        -D WITH_GTK=OFF \
        -D WITH_QT=OFF \
        -D WITH_CUDA=OFF \
        -D WITH_VTK=OFF \
        -D WITH_OPENEXR=OFF \
        -D WITH_FFMPEG=OFF \
        -D WITH_OPENCL=OFF \
        -D WITH_OPENNI=OFF \
        -D WITH_XINE=OFF \
        -D WITH_GDAL=OFF \
        -D WITH_IPP=OFF \
        -D BUILD_OPENCV_PYTHON3=ON \
        -D BUILD_OPENCV_PYTHON2=OFF \
        -D BUILD_OPENCV_JAVA=OFF \
        -D BUILD_TESTS=OFF \
        -D BUILD_IPP_IW=OFF \
        -D BUILD_PERF_TESTS=OFF \
        -D BUILD_EXAMPLES=OFF \
        -D BUILD_ANDROID_EXAMPLES=OFF \
        -D BUILD_DOCS=OFF \
        -D BUILD_ITT=OFF \
        -D INSTALL_PYTHON_EXAMPLES=OFF \
        -D INSTALL_C_EXAMPLES=OFF \
        -D INSTALL_TESTS=OFF \
        /opt/opencv-${OPENCV_VERSION} \
    && make -j$(nproc) \
    && ln -s /usr/local/lib/python3.7/site-packages/cv2 /usr/local/lib/python3.7/dist-packages/cv2 \
    && make install \
    && rm -rf /opt/build/* \
    && rm -rf /opt/opencv-${OPENCV_VERSION} \
    && rm -rf /opt/opencv_contrib-${OPENCV_VERSION} \
    && apt-get -qq remove -y \
        software-properties-common \
        libhdf5-dev \
        libprotobuf-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libtiff-dev \
        libwebp-dev \
        libopenjp2-7-dev \
        libtbb-dev \
        libtesseract-dev \
    && apt-get -qq autoremove \
    && apt-get -qq clean

编译 docker image

  • --build-arg OPENCV_VERSION=3.4.3,其中 OPENCV_VERSION 为你需要的版本号,这里根据 SDK 里面的链接库选择 3.4.3

  • --platform=linux/arm/v7,你所需要的平台架构

    用 X86 或 ARM64 交叉编译(推荐,速度快很多,具体看机器配置,MacBook pro M1 Max 大概10分钟)

    docker buildx build --platform=linux/arm/v7 --build-arg OPENCV_VERSION=3.4.3 . -t debian10-python3-opencv3.4.3

用 RV1126 开发板本地编译(至少需要1个小时)

docker build --build-arg OPENCV_VERSION=3.4.3 . -t debian10-python3-opencv3.4.3

运行容器

  • /dev/dri/card0 是 RV1126 的 算力模块的设备,需要映射到容器中;

  • /opt/devel 是 RV1126 上的 rknn-toolkit-lite 的工具包;

    docker run -it --rm --name rv1126 --privileged -v /dev/dri/card0:/dev/dri/card0 -v /opt/devel/:/opt/devel debian10-python3-opencv3.4.3 /bin/bash

4. 安装 rknn-toolkit-lite 环境和运行结果

安装 rknn-toolkit-lite 环境

Dockerfile 示例

FROM debian10-python3-opencv3.4.3

RUN apt install libdrm-dev -y

RUN /usr/bin/python3 -m pip install --upgrade pip
RUN pip3 install setuptools

COPY ./rknn_toolkit_lite-1.7.1-cp37-cp37m-linux_armv7l.whl /tmp
RUN pip3 install /tmp/rknn_toolkit_lite-1.7.1-cp37-cp37m-linux_armv7l.whl
RUN rm -rf /tmp/rknn_toolkit_lite-1.7.1-cp37-cp37m-linux_armv7l.whl

COPY ./lib/* /usr/lib

编译容器

docker buildx build --platform=linux/arm/v7 . -t rv1126

运行容器

docker run -it --rm --name rv1126 --privileged -v /dev/dri/card0:/dev/dri/card0 -v /opt/devel/:/opt/devel rv1126 /bin/bash

运行效果展示

效果展示

似乎耗时有点久了,整个过程花了25秒左右。

root@743eeb4de2f7:/opt/devel/rknn-toolkit/rknn-toolkit-lite/examples/inference_with_lite# time python3 test.py 
--> list devices:
*************************
None devices connected.
*************************
done
--> query support target platform
**************************************************
Target platforms filled in RKNN model:         ['RV1109']
Target platforms supported by this RKNN model: ['RV1109', 'RK1109', 'RV1126', 'RK1126']
**************************************************
done
--> Load RKNN model
done
--> Init runtime environment
librknn_runtime version 1.7.1 (97198ce build: 2021-11-24 09:32:17 base: 1131)
done
--> get sdk version:
==============================================
RKNN VERSION:
  API: librknn_runtime version 1.7.1 (97198ce build: 2021-11-24 09:32:17 base: 1131)
  DRV: 6.4.6.5.351518
==============================================

done
--> Running model
resnet18
-----TOP 5-----
[812]: 0.9993900656700134
[404]: 0.0004593880439642817
[657 833]: 2.9284517950145528e-05
[657 833]: 2.9284517950145528e-05
[895]: 1.850890475907363e-05

done

real    0m24.747s
user    0m24.492s
sys 0m0.705s

对比 C 编译版本的 yolo 推理

整体耗时只有 0.7秒左右,差距很大啊

root@743eeb4de2f7:/opt/devel/yolov5_detect_demo_release# time ./yolov5_detect_demo 
librknn_runtime version 1.7.1 (97198ce build: 2021-11-24 09:32:17 base: 1131)
time_use is 126.657997
car @ (258 909 583 1109) 0.841335
car @ (534 736 739 859) 0.831931
bus  @ (930 413 1206 531) 0.818415
car @ (1766 578 1872 657) 0.810571
car @ (721 760 927 921) 0.792803
car @ (563 950 801 1115) 0.770443
car @ (956 809 1138 968) 0.752421
car @ (801 991 1071 1115) 0.719339
person @ (1431 619 1461 686) 0.699269
car @ (0 495 137 598) 0.674166
person @ (1819 651 1863 739) 0.639357
car @ (1208 431 1261 472) 0.632317
car @ (862 402 936 469) 0.618040
car @ (1781 446 1845 504) 0.575020
person @ (1543 677 1590 771) 0.561444
person @ (375 481 399 528) 0.538842
car @ (1684 431 1737 478) 0.505462
motorbike  @ (1508 748 1564 836) 0.475376
person @ (1200 589 1232 677) 0.471330
car @ (1839 451 1878 501) 0.438286
traffic light @ (1587 548 1608 607) 0.411564
car @ (715 390 777 425) 0.394160
truck  @ (472 451 745 557) 0.392559
car @ (812 416 874 469) 0.387184
car @ (466 425 575 481) 0.358873
car @ (1622 410 1669 454) 0.350634
car @ (1481 331 1578 378) 0.340664
car @ (1191 357 1235 404) 0.321087

real    0m0.690s
user    0m0.454s
sys 0m0.191s

注意事项

  • 相比 C 编译的二进制代码,python 用于测试和演示还是比较好的方案;
  • python 脚本运行速度比较慢,尤其是在 rknn init 阶段,大概需要20多秒;
  • inference 速度还是很快的,1秒都不需要;
  • 模型文件转换导出要注意能够支持 RV1126 环境

如果用 rknn-toolkit-lite 1.7.3 版本,会出现一个 armv7l 不支持的错误,导致无法完成推理:Exception: Unsupported run platform: Linux armv7l

--> Load RKNN model
done
--> Init runtime environment
E Catch exception when init runtime!
E Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/rknnlite/api/rknn_lite.py", line 144, in init_runtime
    async_mode=async_mode, rknn2precompile=rknn2precompile)
  File "rknnlite/api/rknn_runtime.py", line 292, in rknnlite.api.rknn_runtime.RKNNRuntime.__init__
  File "rknnlite/api/rknn_runtime.py", line 491, in rknnlite.api.rknn_runtime.RKNNRuntime._load_library
  File "rknnlite/api/rknn_runtime.py", line 486, in rknnlite.api.rknn_runtime.RKNNRuntime._get_rknn_api_lib_path
Exception: Unsupported run platform: Linux armv7l

Init runtime environment failed

Open Source is here

GitHub respository: https://github.com/yiqisoft/rv1126-rknn_toolkit_lite-1.7.1.git
Docker Hub respository: https://hub.docker.com/r/jiekechoo/rv1126-rknn_toolkit_lite