arm平台上的MNN编译与运行
0.成果物
直接获取成果物见:https://download.csdn.net/download/u012824853/87867665
以下为编译、运行过程
1.编译准备
在GitHub - alibaba/MNN: MNN is a blazing fast, lightweight deep learning framework, battle-tested by business-critical use cases in Alibaba下载code->downloadZIP
unzip MNN-master.zipcd MNN-mastervim CMakeLists.txt
将其中的MNN_BUILD_DEMO的option改为ON
2.生成Makefile
查看arm的架构,如armv7、v8等,赋值给CMAKE_SYSTEM_PROCESSOR
确定编译结果保存文件的路径赋值给CMAKE_INSTALL_PREFIX
确定编译工具链的位置,赋值给CMAKE_C_COMPILER、CMAKE_CXX_COMPILER
whereis arm-linux-gnueabihf-gcc
arm-linux-gnueabihf-gcc: /root/arm-linux-compiler/gcc-linaro-12.2.1-2023.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
whereis arm-linux-gnueabihf-g++
arm-linux-gnueabihf-g++: /root/arm-linux-compiler/gcc-linaro-12.2.1-2023.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++
mkdir install && mkdir build && cd build
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DMNN_BUILD_DEMO=ON \
-DMNN_USE_INT8_FAST=true \
-DMNN_BUILD_TEST=true\
-DMNN_OPENCL=ON \
-DCMAKE_SYSTEM_NAME=Linux \
-DCMAKE_SYSTEM_VERSION=1 \
-DCMAKE_SYSTEM_PROCESSOR=armv7 \
-DCMAKE_INSTALL_PREFIX=/root/mnn/MNN-master/install \
-DCMAKE_C_COMPILER=/root/arm-linux-compiler/gcc-linaro-12.2.1-2023.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc \
-DCMAKE_CXX_COMPILER=/root/arm-linux-compiler/gcc-linaro-12.2.1-2023.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++
3.编译
执行make
如果遇到报错:
/root/mnn/MNN-master/source/math/Vec.hpp:342:71: error: cannot convert ‘int16x8_t’ to ‘int8x16_t’342 | auto m0m1 = vtrnq_s8(reinterpret_cast<int8x16_t>(vec0.value), reinterpret_cast<int16x8_t>(vec1.value));| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| || int16x8_t
In file included from /root/mnn/MNN-master/source/math/Vec.hpp:15:
/root/arm-linux-compiler/gcc-linaro-12.2.1-2023.01-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/12.2.1/include/arm_neon.h:9532:36: note: initializing argument 2 of ‘int8x16x2_t vtrnq_s8(int8x16_t, int8x16_t)’9532 | vtrnq_s8 (int8x16_t __a, int8x16_t __b)| ~~~~~~~~~~^~~
/root/mnn/MNN-master/source/math/Vec.hpp:343:71: error: cannot convert ‘int16x8_t’ to ‘int8x16_t’343 | auto m2m3 = vtrnq_s8(reinterpret_cast<int8x16_t>(vec2.value), reinterpret_cast<int16x8_t>(vec3.value));| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| || int16x8_t
则将/root/mnn/MNN-master/source/math/Vec.hpp的342、343行
从
auto m0m1 = vtrnq_s8(reinterpret_cast<int8x16_t>(vec0.value), reinterpret_cast<int16x8_t>(vec1.value));
auto m2m3 = vtrnq_s8(reinterpret_cast<int8x16_t>(vec2.value), reinterpret_cast<int16x8_t>(vec3.value));
改为
auto m0m1 = vtrnq_s8(reinterpret_cast<int8x16_t>(vec0.value), reinterpret_cast<int8x16_t>(vec1.value));
auto m2m3 = vtrnq_s8(reinterpret_cast<int8x16_t>(vec2.value), reinterpret_cast<int8x16_t>(vec3.value));
重新make -j4
如果发生make进度卡住的情况,则不要加-j4
成果物是libMNN.so
4.模型准备
在Model Zoo · 语雀中找合适的模型,比如:GitHub - Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB: 💎1MB lightweight face detection model (1MB轻量级人脸检测模型)
code->downloadZIP,解压
将刚才编译的build的目录/MNN-master/build下的libMNN.so拷贝到Ultra-Light-Fast-Generic-Face-Detector-1MB-master/MNN/mnn/lib下
在Ultra-Light-Fast-Generic-Face-Detector-1MB-master/MNN/CMakeLists.txt中添加:
set(OpenCV_DIR /opencv-3.4.1/build)
opencv3.4.1在ARM平台上的编译方法见:
opencv arm交叉编译与仿真验证详细流程_opencv交叉编译arm_I_am_Damon的博客-CSDN博客
将Ultra-Light-Fast-Generic-Face-Detector-1MB-master/MNN/src/main.cpp中的cv:imshow和cv::waitkey注释掉,因为嵌入式端一般不能显示图像
在Ultra-Light-Fast-Generic-Face-Detector-1MB-master/MNN/下执行:
mkdir build;
cd build;
cmake .. -DCMAKE_SYSTEM_PROCESSOR=armv7 \
-DCMAKE_C_COMPILER=/root/arm-linux-compiler/gcc-linaro-12.2.1-2023.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc \
-DCMAKE_CXX_COMPILER=/root/arm-linux-compiler/gcc-linaro-12.2.1-2023.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++
make -j4;
可生成可执行程序:Ultra-face-mnn
5.板端运行
将opencv生成的所有动态库拷贝到执行设备(arm一般是嵌入式板子)上的/lib/目录下
将libMNN.so也拷贝到执行设备上的/lib/目录下
然后按照Ultra-Light-Fast-Generic-Face-Detector-1MB-master/MNN/README.md执行测试用例:
./Ultra-face-mnn ./model/version-RFB/RFB-320.mnn ./imgs/1.jpg
即可生成代码检测框的图片: