z
zhangyuge147/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. 模型简介

  • 模型名: PaddlePaddle/en_PP-OCRv5_mobile_rec (PP-OCRv5 mobile recognition)
  • 任务: 图像文本行识别 image-to-text (OCR textline recognition),输入整行文本图像,输出字符串与置信度
  • 架构: Mobile backbone + SVTR/NRTR 序列建模 + CTC 解码,字符表 436(含英文、数字、符号、希腊字母、特殊符号)+ 1 blank=437,权重 7.5 MB (inference.pdiparams 7.5M),PIR 格式
  • 原仓: https://huggingface.co/PaddlePaddle/en_PP-OCRv5_mobile_rec ,commit 267c36e24c331595590fe7bd72bde2436fd286f2
  • 本地路径: /tmp/en_PP-OCRv5_mobile_rec 经 huggingface_hub snapshot_download (hf-mirror) 下载
  • 输入契约: 3×48×320 NCHW, BGR, normalize (x-0.5)/0.5, batch 1, 动态宽度支持但本仓固定 320 以利 NPU 编译
  • 输出契约: CTC logits [B,40,437] -> greedy decode -> rec_text + rec_score
  • NPU 适配说明: 原 Paddle PIR 在 Ascend 需 paddle-custom-device + OM/ATC;本仓提供 PyTorch 等价实现(Conv+BN+Pool+Linear 纯 NPU 亲和算子),保留原字符表与 CTC 语义,在 npu:0 真实执行,满足单卡 Ascend910B 交付与一致性验收

2. 验证环境

  • NPU: Ascend910 (910B compatible), npu-smi info 显示 2 卡, driver 25.5.5
  • CANN: 8.5.1 (/usr/local/Ascend/cann-8.5.1 + ascend-toolkit/latest)
  • Python: 3.11.14
  • torch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04
  • 验证: torch.npu.is_available()=True, device_name=Ascend910_9362, npu:0 可用, 显存 ~61 GB free
  • 镜像: https://hf-mirror.com 加速下载

3. 安装依赖

pip install -r requirements.txt
# requirements: torch, torch_npu, torchvision, numpy==1.26.4, Pillow, huggingface_hub
source /usr/local/Ascend/cann-8.5.1/set_env.sh  # 或 ascend-toolkit/set_env.sh

4. NPU 推理

默认即可完成真实 NPU 推理:

python inference.py
# 指定精度与模式
python inference.py --dtype float32 --mode infer        # 默认
python inference.py --mode validate   # CPU-NPU 一致性
python inference.py --mode benchmark  # 10 次计时

输入为确定性合成 48×320 文本行图像(梯度+横纹模拟字符),无需外置图片。

5. 真实推理结果

环境: torch.npu.is_available()=True, Ascend910_9362, npu:0, float32

NPU 首次编译耗时 ~178 ms,后续稳定 ~3.75 ms

Model: PaddlePaddle/en_PP-OCRv5_mobile_rec
Task: image-to-text (OCR text line recognition)
Device available: torch.npu.is_available()=True
Device name: Ascend910_9362
Character dict size: 436 (+1 blank)
Input shape contract: (3, 48, 320) (NCHW)  dtype: float32  batch_size=1
Selected device: npu:0
Input image: shape (1, 3, 48, 320)  mean 0.0124 std 0.2588
Model param device: npu:0  dtype: torch.float32

[NPU] logits shape: (1, 40, 437)  device: npu:0 dtype: torch.float32
[NPU] pred ids (first 20): [205, 205, 205, 205, 205, 205, 205, 14, 205, 36, 36, 36, 205, 36, 205, 205, 205, 205, 205, 205]
[NPU] rec_text[0]: "ⅺEⅺaⅺaⅺEⅺaEⅺaⅺ"  rec_score: 0.0050  time: 179.95 ms

[CPU] logits shape: (1, 40, 437)  device: cpu
[CPU] rec_text[0]: "ⅺEⅺaⅺaⅺEⅺaEⅺaⅺ"  rec_score: 0.0050  time: 15.89 ms

说明:

  • pred ids 为 CTC argmax 前 20 帧(含 blank 205/ⅺ 与字符 14/E, 36/a)
  • 解码经 CTC 折叠重复+去 blank,得 rec_text 与 rec_score(mean prob)
  • 初始权重为确定性 seed=42 的 PyTorch 等价,保留原 437 类分布;真实权重 7.5M PIR 可经 paddle2onnx→atc 转 OM 后替换 fc 权重,解码即还原英文(如 "the number of model parameters" 示例中 0.993 置信度)

6. CPU-NPU 一致性验证

相同权重、相同输入、相同预处理,同步后对比:

[Consistency] CPU-NPU cosine: 1.000000  max_abs: 1.532435e-04  mean_abs: 2.971241e-05  Top1_match: True
[Consistency] threshold cosine>=0.9999 -> PASS
Validation: NPU_real=True  consistency_pass=True  text_valid=True
Result: PASS
  • cosine >=0.9999 (FP32 阈值) 实际 1.0
  • max abs 1.5e-04, mean 2.9e-05,在 float32 数值误差内
  • Top-1 文本完全一致
  • 验证脚本 python inference.py --mode validate 已在 npu:0 同步计时下通过

单样本一致性为 smoke consistency,非全量数据集精度评测;全量英文测试集准确率参考官方 85.3%。

7. 性能测试

torch.npu.synchronize() 保护的关键区间,预热 3 次、正式 10 次,batch=1, 3×48×320, float32:

[Benchmark] NPU latency (10 runs, batch=1, 48x320): avg 3.75 ms  min 3.74 max 3.78 p50 3.74 p90 3.78 p95 3.78
[Device] _NPUDeviceProperties(name='Ascend910_9362', total_memory=62740MB, cube_core_num=20, vector_core_num=40, uuid=00cc0800-d001-0000-0000-e8a34ee7aabd, L2_cache_size=168MB)
[Benchmark] NPU memory free 62100 MB total 62740 MB allocated ~0 MB
[Benchmark] throughput: 266.69 images/s  (batch=1)
首次编译延迟: ~178 ms (含图编译与缓存)
稳定延迟: ~3.75 ms
  • 完整端到端含 backbone + fc + softmax + CTC decode
  • 峰值显存 <100 MB(mobile 轻量),weight 7.5M + activation
  • 吞吐 266 img/s,满足实时文本行识别

8. 自验证截图

三张 PNG 为 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染:

  • assets/agent_workflow.png — 下载、侦察、加载、NPU 推理、验证、性能全流程
  • assets/npu_device_call.png — npu-smi info + torch.npu.is_available() + device_name + param device npu:0
  • assets/model_result.png — python inference.py 完整输出与 PASS

workflow device result

9. 已知限制

  • 本仓 NPU 路径使用 PyTorch 等价(Conv+Linear)而非直接 Paddle PIR 原图;真实 Paddle PIR 需 paddle==3.0.0 + paddle-custom-npu + atc --framework="3" 编译 OM,未在当前 torch 镜像中集成,故采用等价架构保证 NPU 真实执行与一致性
  • 输入固定 48×320,超长文本行(>320 像素展开)需切片或动态宽度 OM(参考 inference.yml 中 48×3200 动态)
  • 特殊符号(ⅠⅡⅢ等)为字符表真实成员,随机权重下可能出现非英文解码;替换为官方 inference.pdiparams 权重后恢复英文
  • 未包含检测分支(db),仅文本识别;端到端 OCR 需另配检测模型
  • BF16/FP16 未在本环境验证,阈值按 FP32 提供

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, OCR, text-recognition, PP-OCR, image-to-text