参考文章:树莓派摄像头的安装、配置与验证
完成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
在这一步如果下载很慢,这里我也提供了cmake-3.10.3.tar.gz的包,需要的可以自行下载。
#解压
tar -zxvf cmake-3.10.3.tar.gz
#进入文件夹
cd cmake-3.10.3
#环境配置
sudo ./configure
#make
sudo make
sudo make install
到这里就完成了所有的环境准备。
git clone很慢的同学参考博客: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
cd Paddle-Lite
./lite/tools/build_linux.sh --arch=armv7hf --with_python=ON --python_version=3.7 --with_extra=ON --with_cv=ON
./lite/tools/build_linux.sh help
--------------------------------------------------------------------------------------------------------------------------------------------------------
| 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 |
--------------------------------------------------------------------------------------------------------------------------------------------------------
进入dist目录下
cd /Paddle-Lite/build.lite.linux.armv7hf.gcc/inference_lite_lib.armlinux.armv7hf/python/install/dist
pip3 install paddlelite-2708c2fe-cp37-cp37m-linux_armv7l.whl
wget http://paddle-inference-dist.bj.bcebos.com/mobilenet_v1.tar.gz
tar zxf mobilenet_v1.tar.gz
paddle_lite_opt --model_dir=./mobilenet_v1 \
--optimize_out=mobilenet_v1_opt \
--optimize_out_type=naive_buffer \
--valid_targets=arm
需要注意的是,在这里有两个demo文件,他们的区别在于
full_api.py
中需要的模型文件为__model__
和__param__
文件,API详解:CxxPredictorlight_api.py
中需要的模型文件为opt转换后的model.nb
文件,API详解:LightPredictor# light api的输入为优化后模型文件mobilenet_v1_opt.nb
python3 mobilenetv1_light_api.py --model_dir=mobilenet_v1_opt.nb
Paddle用于推理的模型是通过save_inference_model
这个API保存下来的,保存的格式有两种,在这里将在AI studio上运行生成的模型参数文件下载并挂载在树莓派上:
non-combined形式:参数保存到独立的文件,如设置 model_filename
为 None
, params_filename
为 None
combined形式:参数保存到同一个文件,如设置 model_filename
为 model
, params_filename
为 params
在树莓派端编译:
cd Paddle-Lite
./lite/tools/build.sh build_optimize_tool
paddle_lite_opt --model_dir=./mobilenet_v1 \
--valid_targets=arm \
--optimize_out_type=naive_buffer \
--optimize_out=mobilenet_v1_opt
具体的API详情参考:
编译过程参考: