
当 Arm 与领先的开源深度学习平台强强联合,会带来什么?那就是推动创新的“火箭燃料”。Arm 携手百度,利用双方在高能效计算平台与 AI 模型的技术积累,助力广大开发者加快边缘 AI 解决方案的开发和部署。
为了加速边缘 AI 的创新,并将机器学习能力高效地应用于嵌入式设备,Arm 与百度紧密合作,在 Arm Ethos-U85 上成功实现了涵盖图像分类、目标检测、人脸检测、姿势检测、图像分割以及光学字符识别应用场景的经典 PaddleLite 视觉模型的流畅部署。
Arm-Examples 的 GitHub 公共仓库中已提供涵盖上述六个典型应用场景的完整开发环境。本文将重点说明将 OCR 场景中的识别模型部署在 Ethos-U85 处理器上的工作流程,以及部署其他模型时的一些注意事项。如需详细了解其他案例的技术细节,请参考仓库中对应模型的部署指南。
代码仓库: https://github.com/Arm-Examples/Paddle-on-Ethos-U
边缘侧的人工智能 (AI) 数据处理工作负载正在不断改变应用场景和用户体验。Ethos-U85正是为了满足未来边缘 AI 应用的需求而设计。为了加快芯片开发周期并简化流程,Arm 提供了包括 Arm Corstone-320物联网参考设计平台[1]在内的一整套参考设计。本篇技术博客的案例代码正是在 Corstone-320 的固定虚拟平台[2]上进行测试。
开始之前,需要确保你的运行环境的软件配置满足以下条件:
Python 3.9版本
Cmake 3.21或3.22版本
可以创建虚拟环境的工具,例如:venv(本文使用)、Anaconda 等
测试系统环境为 ubuntu 20.04或22.04
▎步骤1:创建虚拟运行环境,用于模型的训练或部署
# create virtual environment with Python 3.9
python3.9 -m venv ppocr_rec
source ppocr_rec/bin/activate
注意:代码仓库中部分其他模型由于存在模型微调步骤,其模型训练和部署环境可能有差异,请参考代码仓库内部署指南。
▎步骤2:从 GitHub 代码仓库下载示例代码,并安装所需软件包
# Download example source code
git clone https://github.com/Arm-Examples/Paddle-on-Ethos-U.git
cd Paddle-on-Ethos-U
git lfs pull
# Configure inference environment
bash install.sh
▎步骤 3:下载飞桨模型
# Download ppocr_rec model
wget -O ./model_zoo/PpocrRec_infer_int8/ch_ppocr_mobile_v2.0_rec_slim_opt.nb https://paddleocr.bj.bcebos.com/dygraph_v2.0/lite/ch_ppocr_mobile_v2.0_rec_slim_opt.nb
▎步骤 4:使用代码仓库中的模型转换脚本(write_model.py) 转换模型
本文所使用模型的主要包括以下三个模型转换步骤:
将飞桨格式的模型(后缀为 .nb 格式的文件)转换成中间表示 IR 文件(后缀为 .json 格式的文件),该步骤生成的中间表示 IR 文件将自动与输入的飞桨模型文件位于同一目录下(已知限制:--out_dir 参数针对此转换情况不生效)。
# Convert .nb model into IR file (.json file)
python ./readnb/write_model.py --model_path ./model_zoo/PpocrRec_infer_int8/ch_ppocr_mobile_v2.0_rec_slim_opt.nb --out_dir . # "g_ch_ppocr_mobile_v2.0_rec_slim_opt.json" is generated under the same directory with input model file
将中间表示 IR 模型进行手动调整,由于调整部分较分散,为便于开发者体验,可通过补丁包的方式快速完成模型调整。
# Modify the IR file with patch quickly. You could also do these modification maunally.
patch -p0 model_zoo/PpocrRec_infer_int8/g_ch_ppocr_mobile_v2.0_rec_slim_opt.json < readnb/test_asset/ppocr_rec/g_ch_ppocr_rec.patch
可选地,再次使用转换脚本将手动调整后的中间表示 IR 模型转换为 TOSA 图[3]并使用官方提供的编译器 Ethos-U Vela 进行模型的编译。更多关于 Ethos-U Vela 编译器的信息可以查看 PyPI 社区的相关介绍[4],或可访问 developer.arm.com 中的相关技术指南[5]。也可选择跳过此步骤,因为在步骤5中会自动执行该转换命令。
# Finish final model conversion and do model compilation with vela
python write_model.py --model_path ../model_zoo/PpocrRec_infer_int8/g_ch_ppocr_mobile_v2.0_rec_slim_opt.json --out_dir ../model_zoo/PpocrRec_infer_int8 --do_vela
▎步骤 5:构建 OCR 识别应用并查看结果
bash paddle_verify.sh -m ppocr_rec -p ./model_zoo/PpocrRec_infer_int8/test.jpg
示例测试结果如下:
telnetterminal0: Listening for serial connection on port 5000
telnetterminal1: Listening for serial connection on port 5001
telnetterminal5: Listening for serial connection on port 5002
telnetterminal2: Listening for serial connection on port 5003
handles.inputs->count is 1
input tensor scratch_addr address 0x7c11f840
input shapes 122880
copy input data into scratch_addr
handles.outputs->io[x] shapes is 655360
output tensor output_addr address 0x7c1bf840
output shapes 655360
output bin [0x7c1bf840 655360]
handles.outputs->count is 1
Shape : 655360
Rec Reuslut: 纯臻营养护发素
Confidence: 0.966813
============ NPU Inferences : 1 ============
Profiler report, CPU cycles per operator:
ethos-u : cycle_cnt : 2083105832 cycles
Operator(s) total: 574619648 CPU cycles
Inference runtime: -987073648 CPU cycles total
NOTE: CPU cycle values and ratio calculations require FPGA and identical CPU/NPU frequency
Inference CPU ratio: 100.00
Inference NPU ratio: 0.00
cpu_wait_for_npu_cntr : 574619648 CPU cycles
Ethos-U PMU report:
ethosu_pmu_cycle_cntr : 2083105832
ethosu_pmu_cntr0 : 479
ethosu_pmu_cntr1 : 21
ethosu_pmu_cntr2 : 118511
ethosu_pmu_cntr3 : 0
ethosu_pmu_cntr4 : 592
Ethos-U PMU Events:[ETHOSU_PMU_SRAM_RD_DATA_BEAT_RECEIVED,
ETHOSU_PMU_SRAM_WR_DATA_BEAT_WRITTEN,
ETHOSU_PMU_EXT_RD_DATA_BEAT_RECEIVED, ETHOSU_PMU_EXT_WR_DATA_BEAT_WRITTEN,
ETHOSU_PMU_NPU_IDLE]
============ Measurements end ============
Running Model Exit Successfully
Application exit code: 0.
Info: /OSCI/SystemC: Simulation stopped by user.
[0 Dump to out_tensors.bin ] Simulation complete,
在基于 Arm 架构的边缘 AI 设备上部署飞桨模型,开发者往往需要优化模型、准备软件并选择合适的硬件。上述介绍的步骤将有助于开发者高效地在边缘侧部署 AI 应用,实现贴近数据源的快速推理与处理。快来动手尝试吧!
■ 相关资源
[1]https://www.arm.com/products/silicon-ip-subsystems/corstone-320
[2]https://www.arm.com/products/development-tools/simulation/fixed-virtual-platforms
[3] https://www.mlplatform.org/tosa/
[4] https://pypi.org/project/ethos-u-vela/
[5]https://developer.arm.com/documentation/109267/0102/Tool-support-for-the-Arm-Ethos-U-NPU/Ethos-U-Vela-compiler



