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)。
| 项目 | 值 |
|---|---|
| NPU | Atlas Ascend910B(Ascend910_9362),单卡 npu:0 |
| 驱动/CANN | npu-smi 25.5.5 / CANN 8.5.1 |
| torch / torch_npu | 2.9.0+cpu / 2.9.0.post1 |
| paddlepaddle / paddle2onnx | 3.0.0 / 2.1.0(仅用于图转换与官方参考基线) |
| onnx / onnx2torch | 1.17.0 / 1.5.15 |
| dtype | FP32 |
pip install -r requirements.txt
# torch 与 torch_npu 由昇腾 CANN 环境提供(本环境预装 2.9.0 + 2.9.0.post1)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 条合成文本行(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 精度验证,不是完整数据集评测;官方指标请参见原始模型卡。
同一输入、同一 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 的差异属于跨后端浮点正常波动,且解码结果完全一致。
网络前向耗时(不含预处理/解码),输入 [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 MiBassets/agent_workflow.png:下载、侦察、ONNX 转换、构建、NPU 冒烟、验证与性能各阶段真实日志assets/npu_device_call.png:npu-smi 信息、torch.npu 可用性、设备名、模型参数设备与 dtypeassets/model_result.png:默认 python inference.py 的真实输出(识别结果、一致性、性能、PASS)Hardware: NPU NPU Ascend Ascend910