Paddle-Lite-Deploy Paddle model based on python API on Raspberry Pi

Paddle Lite environment preparation

Hardware preparation

  • Raspberry Pi 4B
  • usb camera
  • SD card with Buster mirror source installed

Basic software environment preparation

Camera preparation

Reference article: Installation, configuration and verification of Raspberry Pi camera

Compilation library preparation

Complete the installation of gcc, g++, opencv, cmake:

sudo apt-get update
sudo apt-get install gcc g++ make wget unzip libopencv-dev pkg-config
#下载cmake
wget https://www.cmake.org/files/v3.10/cmake-3.10.3.tar.gz

If the download is slow at this step, I also provide the cmake-3.10.3.tar.gz package , and you can download it yourself .

#解压
tar -zxvf cmake-3.10.3.tar.gz
#进入文件夹
cd cmake-3.10.3
#环境配置
sudo ./configure
#make
sudo make

Insert picture description here

sudo make install

Insert picture description here
All the environmental preparations have been completed here.

Download Paddle-Lite

For students who have a slow git clone, please refer to the blog: How to speed up git clone

# 1. 下载Paddle-Lite源码 并切换到release分支
git clone https://github.com/PaddlePaddle/Paddle-Lite.git
cd Paddle-Lite && git checkout release/v2.6

# 删除此目录,编译脚本会自动从国内CDN下载第三方库文件
rm -rf third-party

Compile

cd Paddle-Lite
./lite/tools/build_linux.sh --arch=armv7hf --with_python=ON --python_version=3.7 --with_extra=ON --with_cv=ON

Insert picture description here

Compilation options

./lite/tools/build_linux.sh help
  • Specific options
--------------------------------------------------------------------------------------------------------------------------------------------------------
| Methods of compiling Padddle-Lite Linux library:                                                                                                     |
--------------------------------------------------------------------------------------------------------------------------------------------------------
|  compile linux library: (armv8, gcc)                                                                                                                 |
|     ./lite/tools/build_linux.sh                                                                                                                      |
|  print help information:                                                                                                                             |
|     ./lite/tools/build_linux.sh help                                                                                                                 |
|                                                                                                                                                      |
|  optional argument:                                                                                                                                  |
|     --arch: (armv8|armv7hf|armv7), default is armv8                                                                                                  |
|     --toolchain: (gcc|clang), defalut is gcc                                                                                                         |
|     --with_extra: (OFF|ON); controls whether to publish extra operators and kernels for (sequence-related model such as OCR or NLP), default is OFF  |
|     --with_python: (OFF|ON); controls whether to build python lib or whl, default is OFF                                                             |
|     --python_version: (2.7|3.5|3.7); controls python version to compile whl, default is None                                                         |
|     --with_cv: (OFF|ON); controls whether to compile cv functions into lib, default is OFF                                                           |
|     --with_log: (OFF|ON); controls whether to print log information, default is ON                                                                   |
|     --with_exception: (OFF|ON); controls whether to throw the exception when error occurs, default is OFF                                            |
|                                                                                                                                                      |
|  arguments of striping lib according to input model:                                                                                                 |
|     ./lite/tools/build_linux.sh --with_strip=ON --opt_model_dir=YourOptimizedModelDir                                                                |
|     --with_strip: (OFF|ON); controls whether to strip lib accrding to input model, default is OFF                                                    |
|     --opt_model_dir: (absolute path to optimized model dir) required when compiling striped library                                                  |
|  detailed information about striping lib:  https://paddle-lite.readthedocs.io/zh/latest/user_guides/library_tailoring.html                           |
|                                                                                                                                                      |
|  arguments of opencl library compiling:                                                                                                              |
|     ./lite/tools/build_linux.sh --with_opencl=ON                                                                                                     |
|     --with_opencl: (OFF|ON); controls whether to compile lib for opencl, default is OFF                                                              |
|                                                                                                                                                      |
|  arguments of rockchip npu library compiling:                                                                                                        |
|     ./lite/tools/build_linux.sh --with_rockchip_npu=ON --rockchip_npu_sdk_root=YourRockchipNpuSdkPath                                                |
|     --with_rockchip_npu: (OFF|ON); controls whether to compile lib for rockchip_npu, default is OFF                                                  |
|     --rockchip_npu_sdk_root: (path to rockchip_npu DDK file) required when compiling rockchip_npu library                                            |
|                                                                                                                                                      |
|  arguments of baidu xpu library compiling:                                                                                                           |
|     ./lite/tools/build_linux.sh --with_baidu_xpu=ON --baidu_xpu_sdk_root=YourBaiduXpuSdkPath                                                         |
|     --with_baidu_xpu: (OFF|ON); controls whether to compile lib for baidu_xpu, default is OFF                                                        |
|     --baidu_xpu_sdk_root: (path to baidu_xpu DDK file) required when compiling baidu_xpu library                                                     |
--------------------------------------------------------------------------------------------------------------------------------------------------------

Compilation is complete
Insert picture description here

Install the compiled python paddle-lite package

进入dist目录下
cd /Paddle-Lite/build.lite.linux.armv7hf.gcc/inference_lite_lib.armlinux.armv7hf/python/install/dist

Insert picture description here

pip3 install paddlelite-2708c2fe-cp37-cp37m-linux_armv7l.whl

Insert picture description here

Run the demo program based on python API

Prepare model files

  • Download model
wget http://paddle-inference-dist.bj.bcebos.com/mobilenet_v1.tar.gz
tar zxf mobilenet_v1.tar.gz
  • Convert model with opt tool
paddle_lite_opt --model_dir=./mobilenet_v1  \
                --optimize_out=mobilenet_v1_opt \
                --optimize_out_type=naive_buffer \
                --valid_targets=arm

Successful conversion
Insert picture description here

Run the model

It should be noted that there are two demo files here, the difference between them is

  • full_api.pyThe model file needed in the file is the __model__and __param__file, and the detailed API: CxxPredictor
  • light_api.pyThe model file needed in opt is the model.nbfile after opt conversion . Detailed API: LightPredictor
# light api的输入为优化后模型文件mobilenet_v1_opt.nb
python3 mobilenetv1_light_api.py --model_dir=mobilenet_v1_opt.nb

Insert picture description here

Deploy your own model

The model used by Paddle for reasoning is save_inference_modelsaved through this API. There are two formats for saving. Here, the model parameter file generated by running on AI studio is downloaded and mounted on the Raspberry Pi:

Two model formats

  • non-combined form : a separate file to save parameters, such as set model_filenameto None, params_filenametoNone
    Insert picture description here

  • combined form : the arguments on the same file, such as setting model_filenameis model, params_filenameasparams

Insert picture description here

Compile the opt tool

Compile on Raspberry Pi:

cd Paddle-Lite
./lite/tools/build.sh build_optimize_tool

Insert picture description here

Convert model using opt

paddle_lite_opt --model_dir=./mobilenet_v1 \
      --valid_targets=arm \
      --optimize_out_type=naive_buffer \
      --optimize_out=mobilenet_v1_opt

Specific API details refer to:

Compilation process reference:

Guess you like

Origin blog.csdn.net/qq_45779334/article/details/112006696