飞桨针对不同场景,提供了多个预测引擎部署模型(如下图),更多详细信息请参考文档。
本文以人像分割在安卓端的部署为例,介绍如何使用Paddle-Lite对分割模型进行移动端的部署。文档第2章介绍如何使用人像分割安卓端的demo,第3章介绍如何进行二次开发,更新Paddle-Lite预测库,或将新的PaddleSeg模型部署到安卓设备。
注:此安卓demo基于Paddle-Lite-Demo开发,更多的细节请参考该repo。
注意:demo中拍照时照片会自动压缩,想测试拍照原图效果,可使用手机相机拍照后从相册中打开进行预测。
您可按需要更新预测库或模型进行二次开发,其中更新模型分为模型导出和模型转换两个步骤。
Paddle-Lite的编译目前支持Docker,Linux和Mac OS开发环境,建议使用Docker开发环境,以免存在各种依赖问题,同时也提供了预编译版本的预测库。准备Paddle-Lite在安卓端的预测库,主要包括三个文件:
下面分别介绍两种方法:
使用预编译版本的预测库,最新的预编译文件参考:release,此demo使用的版本
解压上面文件,PaddlePredictor.jar位于:java/PaddlePredictor.jar;
arm64-v8a相关so位于:java/libs/arm64-v8a;
armeabi-v7a相关so位于:java/libs/armeabi-v7a;
手动编译Paddle-Lite预测库
开发环境的准备和编译方法参考:Paddle-Lite源码编译。
准备好上述文件,即可参考java_api在安卓端进行推理。具体使用预测库的方法可参考Paddle-Lite-Demo中更新预测库部分的文档。
此demo的人像分割模型为基于HRNet w18 small v1的humanseg模型(下载链接),更多的分割模型导出可参考:模型导出
准备好PaddleSeg导出来的模型和参数文件后,需要使用Paddle-Lite提供的opt对模型进行优化,并转换成Paddle-Lite支持的文件格式,这里有两种方式来实现:
详细的模型转换方法参考paddlelite提供的官方文档:模型转化方法,从PaddleSeg里面导出来的模型使用opt即可导出以.nb
名称结尾的单个文件。
(1)参照 编译安装 进行环境配置和编译
(2)进入docker中PaddleLite根目录,git checkout [release-version-tag]切换到release分支。此demo使用的版本为V2.8
(3)执行如下命令编译opt
./lite/tools/build.sh build_optimize_tool
(4)编译完成,优化工具在Paddle-Lite/build.opt/lite/api/opt
将优化好的.nb
文件,替换app/src/main/assets/image_segmentation/
models/hrnet_small_for_cpu下面的文件即可。
Q: 构建Android工程时提示权限不足
/Users/xxx/human_segmentation_demo/app/cache/71j4bd3k08cpahbhs9f81a9gj9/cxx/libs/arm64-v8a/libhiai_ir_build.so (Permission denied)
A: 开放缓存权限
chmod -R 777 /Users/xxx/human_segmentation_demo/app/cache/
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》