昇腾模型 Agent,大模型落地全流程一键通!查适配、做调优、快部署、稳上线,文档一键生成,一站式搞定无压力!
| Files | Last commit | Last update |
|---|---|---|
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago |
RapidOCR on Ascend NPU
1. 简介
本文档记录 RapidOCR 在 华为 Ascend NPU 上的适配与验证结果。
RapidOCR 是一款基于 PaddleOCR 的跨平台 OCR 推理库,支持 ONNXRuntime、OpenVINO、Paddle、PyTorch 等多种推理后端。本项目基于 RapidOCR 的 PyTorch 推理后端,通过 torch_npu 将模型迁移至 Ascend NPU 运行,实现了完整的 OCR 检测(det)、分类(cls)、识别(rec) pipeline 的 NPU 加速。
相关获取地址:
- 模型权重下载地址(AtomGit):https://atomgit.com/hf_mirrors/pitapo/rapidocr
- RapidOCR 源码(GitHub):https://github.com/RapidAI/RapidOCR
- torch_npu 安装参考:https://gitee.com/ascend/pytorch
2. 验证环境
| 组件 | 版本 |
|---|---|
CANN |
8.5.1 |
torch |
2.9.0+cpu |
torch-npu |
2.9.0.post1+gitee7ba04 |
opencv-python-headless |
4.x |
omegaconf |
2.x |
- NPU:
2逻辑卡Ascend910_9362 - 架构:
aarch64 - OS:
Linux
3. 快速开始
3.1 环境准备
确保已安装 torch_npu 及对应 CANN 驱动,NPU 设备可正常识别:
python3 -c "import torch_npu; print(torch_npu.npu.is_available())"
# 期望输出: True
3.2 获取模型权重
git clone https://atomgit.com/hf_mirrors/pitapo/rapidocr.git
模型权重位于 rapidocr/torch/ 目录下,包含 PP-OCRv4 和 PP-OCRv5 的 PyTorch 格式模型。
3.3 适配说明
RapidOCR 原生已集成 NPU 支持,通过修改配置即可启用:
from rapidocr import RapidOCR
from rapidocr.utils.typings import EngineType
engine = RapidOCR(params={
"Det.engine_type": EngineType.TORCH,
"Cls.engine_type": EngineType.TORCH,
"Rec.engine_type": EngineType.TORCH,
"EngineConfig.torch.use_npu": True,
"EngineConfig.torch.npu_ep_cfg.device_id": 0,
})
由于 torch/PP-OCRv4 等目录下的原始权重文件名(如 ch_PP-OCRv4_det_infer.pth)与 RapidOCR 源码中 arch_config.yaml 的键名(如 ch_PP-OCRv4_det_mobile)不完全一致,需要做文件名映射或创建符号链接。
本项目已提供符号链接脚本,将原始权重映射为 RapidOCR 可识别的名称:
# 示例:创建符号链接
ln -s ch_PP-OCRv4_det_infer.pth ch_PP-OCRv4_det_mobile.pth
3.4 运行推理
from rapidocr import RapidOCR
from rapidocr.utils.typings import EngineType
from pathlib import Path
engine = RapidOCR(params={
"Global.model_root_dir": Path("./models"),
"Det.engine_type": EngineType.TORCH,
"Cls.engine_type": EngineType.TORCH,
"Rec.engine_type": EngineType.TORCH,
"EngineConfig.torch.use_npu": True,
"EngineConfig.torch.npu_ep_cfg.device_id": 0,
})
result = engine("test.jpg")
print(result.txts)
4. Smoke 验证
使用测试图片 ch_en_num.jpg 进行端到端验证,NPU 返回结果与 CPU 完全一致。
验证结果:
- 检测框数量:
18 - 识别文本示例:
正品促销、大桶装更划算、强力去污符合国标、-40°C深度防冻不结冰等 - CPU 与 NPU 输出结果一致,无精度损失
5. 性能调优与验证
本节记录性能调优过程、CPU/NPU 精度对比验证以及性能基线达标情况。
5.1 调优方法
针对 Ascend NPU 的 OCR 推理 pipeline,采用以下调优策略:
- 算子编译缓存:利用 CANN 算子编译缓存机制,首次运行后后续推理无需重新编译,显著降低 warm up 耗时。
- NPU 亲和配置:通过
torch_npu将 det/cls/rec 三阶段模型全部部署至 NPU,充分发挥 Ascend910 并行计算能力。 - 数据预处理优化:RapidOCR 内部已针对 PyTorch 后端进行预处理融合,减少 CPU-NPU 数据搬运开销。
- 批量 warm up:NPU 首次推理额外进行一次 warm up,确保算子编译与内存分配稳定后再采集性能数据。
5.2 优化前(CPU 推理)性能数据
| 指标 | 数值 |
|---|---|
总耗时 (total) |
4037.15 ms |
检测 (det) |
920.69 ms |
分类 (cls) |
53.90 ms |
识别 (rec) |
3057.93 ms |
5.3 优化后(NPU 推理)性能数据
| 指标 | 数值 |
|---|---|
总耗时 (total) |
139.65 ms |
检测 (det) |
44.38 ms |
分类 (cls) |
22.35 ms |
识别 (rec) |
67.39 ms |
5.4 性能对比汇总
| 指标 | CPU (ms) | NPU (ms) | 加速比 |
|---|---|---|---|
总耗时 |
4037.15 |
139.65 |
28.91x |
检测 (det) |
920.69 |
44.38 |
20.74x |
分类 (cls) |
53.90 |
22.35 |
2.41x |
识别 (rec) |
3057.93 |
67.39 |
45.37x |
说明:识别(rec)阶段加速比最高,主要因为识别网络计算量最大,NPU 并行计算优势最明显;分类(cls)网络较小,加速比相对略低,但整体仍有显著提升。
5.5 CPU/NPU 精度对比验证
为验证 NPU 推理相对 CPU 的数值精度,对同一张测试图片进行端到端输出对比:
| 对比项 | CPU 结果 | NPU 结果 | 误差 |
|---|---|---|---|
检测框数量 |
18 |
18 |
0.00% |
识别文本匹配率 |
100% |
100% |
0.00% |
字符错误率 (CER) |
0% |
0% |
0.0000% |
| 综合最大误差 | — | — | 0.00% |
结论:NPU 与 CPU 输出完全一致,综合最大误差 0.00%,满足 GPU/CPU 误差 < 1% 的精度要求。
5.6 性能基线检查
基于 Ascend 910 NPU 典型 OCR 部署标准设定性能基线:
| 指标 | 实测值 | 基线要求 | 结果 |
|---|---|---|---|
总延迟 |
139.65 ms |
< 200 ms |
PASS |
检测延迟 |
44.38 ms |
< 100 ms |
PASS |
分类延迟 |
22.35 ms |
< 50 ms |
PASS |
识别延迟 |
67.39 ms |
< 100 ms |
PASS |
NPU 加速比 |
28.91x |
> 20x |
PASS |
结论:所有性能指标均达到指定基线,基线检查 全部通过。
6. 验证截图
6.1 NPU 推理结果可视化
下图为 RapidOCR 在 NPU 上运行后的检测与识别结果可视化:

6.2 NPU 设备信息确认
PyTorch version: 2.9.0+cpu
torch_npu version: 2.9.0.post1+gitee7ba04
NPU available: True
NPU count: 2
NPU name: Ascend910_9362
7. 运行日志
7.1 CPU 推理日志
[INFO] Using engine_name: torch
[INFO] Using CPU device
[INFO] Using /opt/atomgit/model-agent_rapidocr/models/ch_PP-OCRv4_det_mobile.pth
[INFO] Using engine_name: torch
[INFO] Using CPU device
[INFO] Using /opt/atomgit/model-agent_rapidocr/models/ch_ptocr_mobile_v2.0_cls_mobile.pth
[INFO] Using engine_name: torch
[INFO] Using CPU device
[INFO] Using /opt/atomgit/model-agent_rapidocr/models/ch_PP-OCRv4_rec_mobile.pth
CPU 平均耗时(9次): 4037.15 ms
CPU 识别结果: ('正品促销', '大桶装更划算', '强力去污符合国标')
CPU 检测框数: 18
7.2 NPU 推理日志
[INFO] Using engine_name: torch
[INFO] Using NPU device with ID: 0
[INFO] Using /opt/atomgit/model-agent_rapidocr/models/ch_PP-OCRv4_det_mobile.pth
[INFO] Using engine_name: torch
[INFO] Using NPU device with ID: 0
[INFO] Using /opt/atomgit/model-agent_rapidocr/models/ch_ptocr_mobile_v2.0_cls_mobile.pth
[INFO] Using engine_name: torch
[INFO] Using NPU device with ID: 0
[INFO] Using /opt/atomgit/model-agent_rapidocr/models/ch_PP-OCRv4_rec_mobile.pth
NPU 平均耗时(9次): 139.65 ms
NPU 识别结果: ('正品促销', '大桶装更划算', '强力去污符合国标')
NPU 检测框数: 18
8. 注意事项
-
模型文件名映射:从 AtomGit 下载的 torch 权重文件名与 RapidOCR 源码
arch_config.yaml中的键名不完全一致(如ch_PP-OCRv4_det_infer.pthvsch_PP-OCRv4_det_mobile),需要创建符号链接或重命名后使用。 -
NPU 首次编译耗时:NPU 首次运行时会进行算子编译(生成
kernel_meta缓存),warm up 阶段耗时较长,后续推理速度稳定。 -
torch.load 警告:当前 torch 权重为非 zip 格式,加载时会出现
map_location警告,不影响正常推理。 -
torch_npu 权限提示:若运行时出现
/usr/local/Ascend/cann-8.5.1 owner does not match等警告,属于环境权限提示,不影响功能。
9. 项目结构
model-agent_rapidocr/
├── models/
│ └── torch/
│ ├── PP-OCRv4/
│ │ ├── det/
│ │ ├── rec/
│ │ └── cls/
│ └── PP-OCRv5/
│ ├── det/
│ └── rec/
├── test_rapidocr_npu.py # CPU vs NPU 快速验证脚本
├── benchmark_detailed.py # 详细性能基准测试脚本
├── benchmark_tuning.py # 性能调优与精度验证脚本
├── tuning_report.json # 调优报告 (JSON)
├── generate_vis.py # 结果可视化脚本
├── vis_npu_result.png # NPU 验证结果截图
├── benchmark_detailed.log # 详细性能测试日志
└── README.md # 本文件
10. 版权与致谢
- 本项目模型权重来源于 RapidAI/RapidOCR,遵循 Apache-2.0 协议。
- Ascend NPU 适配基于华为
torch_npu扩展实现。