您现在的位置是:首页 >技术教程 >RK3308B部署mobilenetv2_ssdlite网站首页技术教程

RK3308B部署mobilenetv2_ssdlite

咚咚锵咚咚锵 2024-06-17 10:19:00
简介RK3308B部署mobilenetv2_ssdlite

  • 目标板:RK3308B
  • PC:Ubuntu 16.04
  • opencv版本:4.7.0 https://opencv.org/releases/
  • ncnn下载:git clone --depth=1 https://github.com/Tencent/ncnn.git
  • 交叉编译器:gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu(下载链接:https://gitlab.com/TeeFirefly/rk3308-linux)
  • mobilenetv2_ssdlite项目:https://github.com/zhouayi/mobilenetv2_ssdlite_ncnn.git
  • 主要参考嵌入式开发板上部署目标检测模型mobilenetv2_ssdlite

1. 在PC端运行mobilenetv2_ssdlite模型

1.1 安装NCNN和Opencv

参考:Ubuntu16.04安装NCNN和Opencv

1.1.1 安装NCNN

安装NCNN的依赖项

sudo apt install build-essential git cmake libprotobuf-dev protobuf-compiler libvulkan-dev vulkan-utils libopencv-dev

安装NCNN

# 下载
git clone --depth=1 https://github.com/Tencent/ncnn.git

cd ncnn
mkdir build-host-gcc-linux
cd build-host-gcc-linux
mkdir install

# 编译
cmake -DNCNN_VULKAN=OFF -D CMAKE_INSTALL_PREFIX=./install ..
make -j8
make install

测试NCNN是否安装成功

cd ../examples
../build/examples/squeezenet ../images/256-ncnn.png
 
## 输出
532 = 0.165951
920 = 0.094098
716 = 0.062193

cd ../benchmark
../build/benchmark/benchncnn 10 $(nproc) 0 0
 
## 输出
loop_count = 10
num_threads = 8
powersave = 0
gpu_device = 0
cooling_down = 1
          squeezenet  min =    2.89  max =    3.19  avg =    2.98
     squeezenet_int8  min =    5.37  max =    8.72  avg =    5.93
           mobilenet  min =    3.75  max =    4.26  avg =    3.91
      mobilenet_int8  min =    5.91  max =    6.08  avg =    5.98
        mobilenet_v2  min =    3.51  max =    4.31  avg =    3.83
        mobilenet_v3  min =    3.09  max =    3.70  avg =    3.22
          shufflenet  min =    3.03  max =    3.26  avg =    3.10
       shufflenet_v2  min =    2.51  max =    2.81  avg =    2.59
             mnasnet  min =    3.42  max =    3.90  avg =    3.66
     proxylessnasnet  min =    3.73  max =    4.26  avg =    3.88
     efficientnet_b0  min =    5.86  max =   10.86  avg =    6.76
   efficientnetv2_b0  min =    6.66  max =    7.58  avg =    7.00
        regnety_400m  min =    7.83  max =   12.75  avg =    8.52
           blazeface  min =    0.89  max =    0.96  avg =    0.91
           googlenet  min =   10.47  max =   11.00  avg =   10.85
      googlenet_int8  min =   19.94  max =   20.60  avg =   20.11
            resnet18  min =    8.22  max =   28.34  avg =   10.50
       resnet18_int8  min =   18.58  max =   53.93  avg =   22.29
             alexnet  min =    8.48  max =   41.33  avg =   14.13
               vgg16  min =   46.46  max =   54.78  avg =   49.38
          vgg16_int8  min =   48.13  max =   61.21  avg =   50.25
            resnet50  min =   19.17  max =   20.06  avg =   19.43
...
1.1.2 安装Opencv

首先从官网下载对应版本的压缩包,并进行解压
在这里插入图片描述
安装Opencv

cd opencv-4.7.0
mkdir build-host-gcc-linux
cd build-host-gcc-linux
mkdir install

# 编译
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=./install ..
make -j8
make install

安装过程还参考了下ubuntu 系统 OpenCV 4 安装教程

环境配置

# 先在/etc/ld.so.conf.d/文件夹下新建一个opencv4.conf文件,然后写入/opencv4.7.0/build-host-gcc-linux/lib
cd /etc/ld.so.conf.d/
sudo touch opencv4.conf
sudo vim opencv4.conf # 将/opencv4.7.0/build-host-gcc-linux/lib写入

# 更新pkg-config
sudo ldconfig

# 编辑bashrc文件,添加下方两条语句
sudo vim /etc/bash.bashrc
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opencv4.7.0/build-host-gcc-linux/lib/pkgconfig
export PKG_CONFIG_PATH

# 更新环境
source /etc/bash.bashrc
sudo updatadb

测试Opencv是否安装成功

pkg-config --libs opencv4
## 输出
-L/opencv4.7.0/build-host-gcc-linux/lib -lopencv_photo -lopencv_highgui -lopencv_videoio -lopencv_objdetect -lopencv_gapi -lopencv_video -lopencv_ml -lopencv_stitching -lopencv_calib3d -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_imgcodecs -lopencv_imgproc -lopencv_core

pkg-config --cflags opencv4
## 输出
-I/opencv4.7.0/build-host-gcc-linux/include/opencv4

opencv_version
## 输出
4.7.0

1.2 运行mobilenetv2_ssdlite模型

修改mobilenetv2_ssdlite_ncnn/CMakeLists.txt文件中NCNN和Opencv的路径

...
else()
    include_directories(
        # 将下面这些修改为你自己的ncnn和opencv相应的路径
        /ncnn/build-host-gcc-linux/install/include/ncnn
        /ncnn/build-host-gcc-linux/install/include
        /ncnn/src
        /opencv4.7.0/build-host-gcc-linux/install/include/opencv4
        /opencv4.7.0/build-host-gcc-linux/install/include/opencv4/opencv2
        /opencv4.7.0/build-host-gcc-linux/install/include
    )
...
else()
    link_directories(
        # 将下面这些修改为你自己的ncnn和opencv相应的路径
        /ncnn/build-host-gcc-linux/install/lib
        /opencv4.7.0/build-host-gcc-linux/install/lib
    )
...

生成可执行文件ssdlite和ssdlite_192x192

zhouying@ubuntu:~/Desktop/mobilenetv2_ssdlite_ncnn$ ./build_host_gcc_linux.sh 
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zhouying/Desktop/mobilenetv2_ssdlite_ncnn/build-host-gcc-linux
Scanning dependencies of target ssdlite_192x192
Scanning dependencies of target ssdlite
[ 25%] Building CXX object CMakeFiles/ssdlite_192x192.dir/ssdlite_192x192.cpp.o
[ 50%] Building CXX object CMakeFiles/ssdlite.dir/mobilenetv2ssdlite.cpp.o
[ 75%] Linking CXX executable ssdlite_192x192
[100%] Linking CXX executable ssdlite
[100%] Built target ssdlite
[100%] Built target ssdlite_192x192

运行文件进行识别

# 修改run_demo.sh中的LD_LIBRARY_PATH路径为你的opencv的动态库路径,然后运行该脚本
zhouying@ubuntu:~/Desktop/mobilenetv2_ssdlite_ncnn$ ./run_demo.sh
time of init is 69.75 ms
start evaluate speed...
time of detect is 109.35 ms
15 = 0.97621 at 789.58 102.49 185.49 x 422.15
15 = 0.96929 at 589.56 119.56 220.24 x 433.76
15 = 0.91274 at 183.79 89.89 120.60 x 273.05
15 = 0.91003 at 289.27 138.48 263.19 x 464.86
2 = 0.89248 at 581.45 326.53 209.65 x 326.72
2 = 0.85226 at 286.76 340.52 277.23 x 359.78
2 = 0.64936 at 818.64 279.61 157.07 x 284.42
2 = 0.44616 at 0.00 0.00 0.00 x 0.00
2 = 0.40192 at 397.34 0.00 1722.70 x 0.00
15 = 0.39684 at 0.00 0.00 380.79 x 0.00
2 = 0.36823 at 181.93 0.00 1578.19 x 0.00
time of init is 74.98 ms

能得到识别结果图result.jpg
在这里插入图片描述

2. 交叉编译部署到RK3308B板子上并运行模型

2.1 交叉编译NCNN和Opencv

2.1.1 交叉编译Opencv

主要参考OpenCv4.x + opencv_contrib-4.x交叉编译

使用cmake-gui进行编译,下载cmake-gui命令:sudo apt install cmake-gui

cd opencv4.7.0
mkdir build-aarch64-linux-gnu
cd build-aarch64-linux-gnu
mkdir install
cmake-gui ..

在这里插入图片描述

首先先设置CMAKE_TOOLCHAIN_FILE
在这里插入图片描述
点击Configure
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
修改其中一些配置后再点击Configure

CMAKE_INSTALL_PREFIX /opencv-4.7.0/build/install
勾选BUILD_opencv_world,BUILD_PNG,BUILD_ZLIB,WITH_PNG,WITH_JPEG, WITH_OPENJPEG,OPENCV_ENABLE_NONFREE
取掉勾选BUILD_TESTS,BUILD_PERF_TESTS,BUILD_TIFF

点击Generate
在这里插入图片描述

make -j8
make install
2.1.2 交叉编译ONNX
cd ncnn
mkdir build-aarch64-linux-gnu
cd build-aarch64-linux-gnu
mkdir install
cmake-gui ..

在这里插入图片描述

首先先设置CMAKE_TOOLCHAIN_FILE
在这里插入图片描述
点击Configure
在这里插入图片描述
在这里插入图片描述

会报个错
在这里插入图片描述
在这里插入图片描述
修改下面的配置参数,再点击Configure

取掉勾选NCNN_THREADS,NCNN_SSE2

在这里插入图片描述

点击Generate
在这里插入图片描述

make -j8
make install

2.2 交叉编译mobilenetv2_ssdlite模型

修改mobilenetv2_ssdlite_ncnn/CMakeLists.txt文件中NCNN和Opencv的路径

...
if(${BUILD_SYSTEM} STREQUAL "aarch64-linux-gnu")
    include_directories(
        # 将下面这些修改为你自己的ncnn和opencv相应的路径
        /ncnn/build-aarch64-linux-gnu/install/include/ncnn
        /ncnn/build-aarch64-linux-gnu/install/include
        /ncnn/src
        /opencv-4.7.0/build-aarch64-linux-gnu/install/include/opencv4
        /opencv-4.7.0/build-aarch64-linux-gnu/install/include/opencv4/opencv2
        /opencv-4.7.0/build-aarch64-linux-gnu/install/include
    )
...
if(${BUILD_SYSTEM} STREQUAL "aarch64-linux-gnu")
    link_directories(
        # 将下面这些修改为你自己的ncnn和opencv相应的路径
        /ncnn/build-aarch64-linux-gnu/install/lib
        /opencv-4.7.0/build-aarch64-linux-gnu/install/lib
    )
...

生成可执行文件ssdlite和ssdlite_192x192

# 修改build_aarch64_linux_gnu.sh中的PATH为你的交叉编译器的路径
zhouying@ubuntu:~/Desktop/mobilenetv2_ssdlite_ncnn$ ./build_aarch64_linux_gnu.sh 
-- The C compiler identification is GNU 6.3.1
-- The CXX compiler identification is GNU 6.3.1
-- Check for working C compiler: /home/zhouying/Firefly-RK3308/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
-- Check for working C compiler: /home/zhouying/Firefly-RK3308/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /home/zhouying/Firefly-RK3308/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++
-- Check for working CXX compiler: /home/zhouying/Firefly-RK3308/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zhouying/Desktop/mobilenetv2_ssdlite_ncnn/build-aarch64-linux-gnu
Scanning dependencies of target ssdlite_192x192
Scanning dependencies of target ssdlite
[ 25%] Building CXX object CMakeFiles/ssdlite_192x192.dir/ssdlite_192x192.cpp.o
[ 50%] Building CXX object CMakeFiles/ssdlite.dir/mobilenetv2ssdlite.cpp.o
[ 75%] Linking CXX executable ssdlite_192x192
[100%] Linking CXX executable ssdlite
[100%] Built target ssdlite_192x192
[100%] Built target ssdlite

2.3 将得到的可执行文件、模型、图片和动态库push到RK3308B

将板子与PC连接,将模型、可执行文件、图片push到板子上

# 将模型文件push到板子的/data/models路径下
zhouying@ubuntu:~/Desktop/mobilenetv2_ssdlite_ncnn$ adb push models/mbv2_ssdlite /data/models

# 将可执行文件push到板子的/data路径下
zhouying@ubuntu:~/Desktop/mobilenetv2_ssdlite_ncnn$ adb push build-aarch64-linux-gnu/ssdlite /data/

zhouying@ubuntu:~/Desktop/mobilenetv2_ssdlite_ncnn$ adb push images/bicycle.jpg /data/

将opencv的动态库push到板子,NCNN生成的是静态库

zhouying@ubuntu:~/Downloads/opencv-4.7.0/build-aarch64-linux-gnu/install$ adb push lib /data/lib

2.4 在板子上运行模型进行识别

adb shell
# 配置环境
export LD_LIBRARY_PATH=/data/lib
cd data/

# 运行程序
./ssdlite models/mbv2_ssdlite bicycle.jpg
## 输出结果
time of init is 151.85 ms
start evaluate speed...
time of detect is 659.10 ms
15 = 0.97621 at 789.58 102.49 185.49 x 422.15
15 = 0.96929 at 589.56 119.56 220.24 x 433.76
15 = 0.91275 at 183.79 89.89 120.60 x 273.05
15 = 0.91003 at 289.27 138.48 263.19 x 464.86
2 = 0.89248 at 581.45 326.53 209.65 x 326.72
2 = 0.85226 at 286.76 340.52 277.23 x 359.78
2 = 0.64936 at 818.64 279.61 157.07 x 284.42
2 = 0.44617 at 0.00 0.00 839.33 x 0.00
2 = 0.40192 at 0.00 0.00 0.00 x 143.39
15 = 0.39684 at 0.00 0.00 0.00 x 0.00
2 = 0.36823 at 0.00 0.00 0.00 x 0.00

将识别结果push到PC端

zhouying@ubuntu:~/Downloads/opencv-4.7.0/build-aarch64-linux-gnu/install$ adb push /data/result.jpg
972 KB/s (308222 bytes in 0.309s)

在这里插入图片描述

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。