sheepss/PaddlePaddle-en_PP-OCRv5_mobile_rec-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PaddlePaddle/en_PP-OCRv5_mobile_rec on Ascend NPU

1. 模型简介

en_PP-OCRv5_mobile_rec 是 PaddleOCR 团队 PP-OCRv5 系列中的英文移动端文本行识别模型(官方标注整体准确率 85.3%)。模型输入为 BGR 文本行图像(高度 48,宽度按比例缩放并右侧补零至 320),输出 CTC 概率序列 [B, T, 438](blank + 436 个字符 + 空格),经贪心 CTC 解码得到识别文本。

本仓库将官方 Paddle 推理图(PIR 格式 inference.json + inference.pdiparams)转换为等价的 PyTorch 模块,并在华为昇腾 npu:0 上完成真实的 FP32 推理、CPU-NPU 一致性验证与性能测试。

转换链路:snapshot_download → paddle2onnx (opset 15) → Constant 折叠为 initializer → onnx2torch。权重不落盘提交,运行时从官方 HuggingFace 仓库下载后现场生成(约 1.65M 参数 / 7.8 MB)。

2. 验证环境

项目值
NPUAtlas Ascend910B(Ascend910_9362),单卡 npu:0
驱动/CANNnpu-smi 25.5.5 / CANN 8.5.1
torch / torch_npu2.9.0+cpu / 2.9.0.post1
paddlepaddle / paddle2onnx3.0.0 / 2.1.0(仅用于图转换与官方参考基线)
onnx / onnx2torch1.17.0 / 1.5.15
dtypeFP32

3. 安装依赖

pip install -r requirements.txt
# torch 与 torch_npu 由昇腾 CANN 环境提供(本环境预装 2.9.0 + 2.9.0.post1)

4. NPU 推理

python inference.py                # 默认:完整任务验证 + 性能测试
python inference.py --mode validate
python inference.py --mode benchmark

脚本自动完成:官方权重下载(走 HF_ENDPOINT 镜像)→ ONNX 转换与缓存 → 构建 PyTorch 模块 → 渲染固定测试文本行 → 官方 RecResizeImg 预处理 → npu:0 真实推理 → CTC 贪心解码 → 一致性与精度判定。

注意:import paddle 不能与初始化后的 torch_npu 同进程共存(C++ 运行时冲突),因此官方参考基线通过子进程 paddle_ref_worker.py 运行。

5. 真实推理结果

固定 5 条合成文本行(DejaVu 字体渲染,ground truth 已知),batch=1、FP32、npu:0:

GT='Ascend 910B NPU'         NPU pred='Ascend 910B NPU'         conf=0.9846 exact_match:True
GT='Hello World 123'         NPU pred='Hello World 123'         conf=0.9833 exact_match:True
GT='TOTAL: $45.99'           NPU pred='TOTAL: $45.99'           conf=0.9820 exact_match:True
GT='https://www.gitcode.com' NPU pred='https://www.gitcode.com' conf=0.9458 exact_match:True
GT='Model: PP-OCRv5'         NPU pred='Model: PP-OCRv5'         conf=0.9984 exact_match:True

line-level exact match (NPU) : 5/5
char-level accuracy (NPU)    : 100.00%
[VALIDATE] PASS   FINAL RESULT: PASS

说明:这是固定样例的 smoke 精度验证,不是完整数据集评测;官方指标请参见原始模型卡。

6. CPU-NPU 一致性验证

同一输入、同一 PyTorch 模块分别在 CPU 与 npu:0 前向:

torch-cpu vs paddle-cpu probs : max_abs_diff=0.000014 mean_abs_diff=0.00000000  (与官方 Paddle 运行时对齐)
NPU-vs-CPU prob consistency   : max_abs_diff=0.016518 mean_abs_diff=0.00000107
CPU/NPU greedy decode equal   : True

与官方 Paddle CPU 运行时的概率差异 ≤ 1.4e-5,证明图转换正确;NPU 与 CPU 的差异属于跨后端浮点正常波动,且解码结果完全一致。

7. 性能测试

网络前向耗时(不含预处理/解码),输入 [1,3,48,320] FP32 batch=1,预热 3 次、计时 10 次,前后 torch.npu.synchronize():

latency ms  : avg=31.22 min=30.91 max=32.07 p50=31.07 p90=32.07 p95=32.07
throughput  : 32.0 img/s (batch=1)
peak NPU memory allocated: 24.8 MiB

8. 自验证截图

  • assets/agent_workflow.png:下载、侦察、ONNX 转换、构建、NPU 冒烟、验证与性能各阶段真实日志
  • assets/npu_device_call.png:npu-smi 信息、torch.npu 可用性、设备名、模型参数设备与 dtype
  • assets/model_result.png:默认 python inference.py 的真实输出(识别结果、一致性、性能、PASS)

9. 已知限制

  • 仅支持英文及常见符号文本行(字符表由官方 config.json 定义,438 类)。
  • 输入需为水平文本行图;检测框裁剪、方向分类不在本模块范围内。
  • FP32 验证;未做 FP16/BF16 量化。
  • paddle 与 torch_npu 不能同进程混用(见第 4 节),已在脚本中以子进程隔离。
  • 固定样例验证(5 条)不代表完整数据集精度。

10. 标签

Hardware: NPU NPU Ascend Ascend910