一个demo,mindspore lite 部署在树莓派4B ubuntu22.04中,为后续操作开个门!
环境
- 开发环境:wsl-ubuntu22.04分发版
- 部署环境:树莓派4B,操作系统为ubuntu22.04
- mindspore lite版本:mindspore-lite-2.4.1-linux-aarch64.tar.gz
- demo模型:mnist手写数字识别
步骤
1. 准备交叉编译环境
- 安装交叉编译器
bash
sudo apt install gcc-aarch64-linux-gnu
# 验证是否安装成功
(base) joke@ShineZhang:~/mindspore_deploy_demo_aarch64$ aarch64-linux-gnu-gcc --version
aarch64-linux-gnu-gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- 设置交叉编译环境
makefile
# file: toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
2. 下载demo并交叉编译
- 下载
bash
# 有用的话给个star呗
git clone https://gitee.com/Shine_Zhang/mindspore_aarch64_runtime_demo.git
- 文件结构说明:
bash
.
├── CMakeLists.txt
├── README.md
├── build.sh # 执行编译
├── main.c
├── model
│ └── mnist.ms # mnist模型文件
└── toolchain.cmake # 编译工具链
- 模型文件转换:
MindSpore Lite端侧模型转换
执行交叉编译:
bash
chomd +x build.sh
./build.sh
3. 部署环境配置
- 准备环境
编译后目录如下:
bash
.
├── CMakeLists.txt
├── README.md
├── bin
│ └── mindspore_mnist_demo # es
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── Makefile
│ ├── bin
│ ├── cmake_install.cmake
│ ├── mindspore-lite-2.4.1-linux-aarch64
│ ├── mindspore-lite-2.4.1-linux-aarch64.tar.gz
│ ├── mindspore_mnist_demo
│ └── mindspore_quick_start_c
├── build.sh
├── include
│ ├── api
│ ├── c_api
│ ├── dataset
│ ├── ir
│ ├── kernel_interface.h
│ ├── mindapi
│ ├── registry
│ ├── schema
│ └── third_party
├── lib
│ ├── libmindspore-lite.so # es
│ ├── libmindspore_glog.so -> /home/mindspore_deploy_demo_aarch64/lib/libmindspore_glog.so.0
│ └── libmindspore_glog.so.0 # es
├── main.c
├── model
│ └── mnist.ms # es
└── toolchain.cmake
将目录中标记es
的文件拷贝至要部署的树莓派4B中
- 配置运行环境
将libmindspore-lite.so
和libmindspore_glog.so.0
拷贝至/usr/local/lib
bash
# 编辑~/.bashrc 增加环境变量
vim ~/.bashrc
# ~/.bashrc 末尾增加以下内容后保存:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
# 重新加载当前用户的 ~/.bashrc 配置文件
source ~/.bashrc
# 检验动态库是否加载成功
joke in ~ λ ldd mindspore_mnist_demo
linux-vdso.so.1 (0x0000ffffb580b000)
libmindspore-lite.so => /usr/local/lib/libmindspore-lite.so (0x0000ffffb5040000)
libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000ffffb4e90000)
/lib/ld-linux-aarch64.so.1 (0x0000ffffb57d2000)
libmindspore_glog.so.0 => /usr/local/lib/libmindspore_glog.so.0 (0x0000ffffb4e30000)
libdl.so.2 => /lib/aarch64-linux-gnu/libdl.so.2 (0x0000ffffb4e10000)
libstdc++.so.6 => /lib/aarch64-linux-gnu/libstdc++.so.6 (0x0000ffffb4be0000)
libm.so.6 => /lib/aarch64-linux-gnu/libm.so.6 (0x0000ffffb4b40000)
libgcc_s.so.1 => /lib/aarch64-linux-gnu/libgcc_s.so.1 (0x0000ffffb4b10000)
libpthread.so.0 => /lib/aarch64-linux-gnu/libpthread.so.0 (0x0000ffffb4af0000)
4. 执行demo
bash
joke in ~ λ ./mindspore_mnist_demo mnist.ms
----------------------------------------------------------
Model Inputs (1):
Input 0:
Name: serving_default_keras_tensor:0
Shape: [1, 28, 28]
Data Type: 43
Data Size: 3136 bytes
Model Outputs (1):
Output 0:
Name: StatefulPartitionedCall_1:0
Shape: [-1]
Data Type: 43
Data Size: 0 bytes
Estimated Model Size: 3136 bytes
----------------------------------------------------------
inputs: Tensor 1:
Tensor shape: [1, 28, 28]
outputs: Tensor 1:
Tensor shape: [1, 10]
outputs[1]: -5.782
outputs[2]: 0.069
outputs[3]: 1.802
outputs[4]: -12.207
outputs[5]: -7.331
outputs[6]: -18.879
outputs[7]: 6.847
outputs[8]: -5.083
outputs[9]: -4.575
Predicted digit: 7 # 预测结果为数字 7