ohhoohho/en_PP-OCRv5_mobile_rec
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

en_PP-OCRv5_mobile_rec on Ascend NPU

模型适配 · 文本识别模型 (PP-LCNet v3 + SVTR + CTC) · 昇腾 NPU

Beta 版本头信息

{hardware NPU}

1. 简介

  • 模型来源: PaddlePaddle/en_PP-OCRv5_mobile_rec
    • AtomGit 镜像: https://ai.gitcode.com/paddlepaddle/en_PP-OCRv5_mobile_rec
    • Safetensors 权重 (transformers 兼容): https://huggingface.co/PaddlePaddle/en_PP-OCRv5_mobile_rec_safetensors
  • 模型类型: 文本识别(text recognition),输入单行文本行图片,输出识别文本字符串
  • 模型结构: PP-LCNet v3 主干 (scale 0.95) + SVTR encoder + Linear CTC 识别头(438 类,含 blank + 数字 + 大小写字母 + 符号),约 5.51M 参数
  • 适配目标: 在昇腾 910B NPU 上跑通文本识别推理(torch_npu 2.9.0 + CANN 8.5.1)

2. 适配方案说明

官方发布包含两套权重:

权重格式加载方式本仓库结论
Paddle 推理模型(inference.json + inference.pdiparams,PIR 格式)PaddlePaddle + paddle-custom-npu⚠️ paddle-custom-npu 3.2.0 官方按 CANN 8.0 编译,与 CANN 8.5.1 存在 GE/JIT ABI 不兼容,不可用
Safetensors(transformers 兼容)transformers 5.15 内置 PPOCRV5MobileRecForTextRecognition 架构✅ 本仓库采用:torch_npu 直接加载推理,全程零改造

采用路线: transformers 5.15 已官方内置 pp_ocrv5_mobile_rec 架构(modeling + image processor), 配合 PaddlePaddle/en_PP-OCRv5_mobile_rec_safetensors 权重,用 torch_npu 直接在昇腾 NPU 上推理, 无需 PaddlePaddle 运行时。预处理(Resize to 48×320 / normalize / pad)与后处理(CTC 贪心解码) 全部由内置 PPOCRV5ServerRecImageProcessor + PPOCRV5MobileRecForTextRecognition 完成。

3. 环境依赖

  • Python 3.11 + torch 2.9.0 + torch_npu 2.9.0.post1 + CANN 8.5.1
  • transformers >= 5.15.0(内置 PP-OCRv5 mobile rec 识别架构)
  • 依赖清单见 requirements.txt
# 建议独立 venv(继承系统 torch_npu)
python -m venv --system-site-packages venv && source venv/bin/activate
pip install -r requirements.txt

4. 权重获取

权重自动按以下通道回退(--model 传本地目录或 HF repo id):

  1. 本地目录(推荐): --model /path/to/en_PP-OCRv5_mobile_rec_safetensors
  2. HuggingFace (hf-mirror): 自动 snapshot_download("PaddlePaddle/en_PP-OCRv5_mobile_rec_safetensors"),带 3 次重试
  3. gitcode 镜像: git clone https://gitcode.com/hf_mirrors/PaddlePaddle/en_PP-OCRv5_mobile_rec_safetensors.git
    • LFS batch API 下载 model.safetensors(约 22 MB)

5. 推理

5.1 单图推理(NPU)

python inference.py --image demo.png --device npu:0

5.2 CPU/NPU 精度对比

python inference.py --image demo.png --device npu:0 --compare

5.3 HTTP 服务化

python inference.py --device npu:0 --server --port 8200
curl -s -X POST http://127.0.0.1:8200/recognize \
     -H 'Content-Type: application/json' \
     -d '{"image":"demo.png"}'
# 返回 {"text":"HELLOWOND","infer_ms":20.74}

6. 验证结果(昇腾 910B, npu:0)

测试图 320×48(含文本 "Hello World"),CPU vs NPU 对比:

指标值
NPU 推理耗时(预热后)~20 ms
CPU 推理耗时(对比)~188 ms
logits cosine0.999998
logits max_diff0.006396
logits mean_diff0.000003
识别文本CPU 与 NPU 一致("HELLOWOND")

NPU 首次推理有 warm-up 偏差,脚本默认预热 1 次后再计时。

7. 项目结构

en_PP-OCRv5_mobile_rec/
├── inference.py        # 推理脚本(单图 / 对比 / HTTP 服务)
├── README.md           # 本文件
├── requirements.txt    # 环境依赖清单
└── assets/             # 截图素材(agent_workflow / npu_device_call / model_result)