waylong/PaddlePaddle-PP-LCNet_x1_0_doc_ori-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PaddlePaddle/PP-LCNet_x1_0_doc_ori on Ascend NPU

1. 模型简介

  • 模型名称: PaddlePaddle/PP-LCNet_x1_0_doc_ori
  • 任务: 文档图像方向分类 (doc_img_orientation_classification), 4 classes: 0°, 90°, 180°, 270°
  • 架构: PP-LCNet_x1_0 (PPLCNet), lightweight CNN with DepthwiseSeparable blocks, SE modules, HardSwish/Hardsigmoid, 7M 参数, 基于 PIR (Paddle Inference) 格式
  • 官方精度: 99.06% avg accuracy (PaddleOCR), 模型存储 7M
  • 输入: 3x224x224 RGB, 预处理: ResizeShort 256 -> CenterCrop 224 -> Normalize (mean 0.485,0.456,0.406; std 0.229,0.224,0.225; scale 1/255)
  • 输出: 4 logits after softmax, Top-1 label
  • 权重来源: https://huggingface.co/PaddlePaddle/PP-LCNet_x1_0_doc_ori (inference.pdiparams 6.75M, inference.json PIR)
  • NPU 适配方式: Paddle PIR -> PyTorch interpreter (torch + torch_npu) 在 npu:0 上真实推理, 不替换模型, 使用真实权重

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910), 2 devices, npu:0 OK, Power 168W, Temp 45°C, HBM 3112/65536 MB (per npu-smi 25.5.5)
  • torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • CANN: 8.5.1
  • torch.npu.is_available(): True, device_name: Ascend910_9362, device_count: 2
  • 内存: total 65787 MB, free 65397 MB (per torch.npu.mem_get_info)
  • OS: aarch64, Python 3.11.14
  • PaddlePaddle: 3.0.0
  • 验证日期: 2026-08-22 UTC

3. 安装依赖

pip install -r requirements.txt
# requirements.txt 包含:
# paddlepaddle==3.0.0
# torch==2.9.0
# torch_npu==2.9.0.post1
# torchvision==0.24.0
# pillow>=10.0.0
# numpy>=1.24.0
# modelscope>=1.22.0
# huggingface_hub>=0.25.0

4. NPU 推理

python inference.py
# 或指定输入
python inference.py --input /path/to/doc.jpg
# 验证 CPU-NPU 一致性
python inference.py --mode validate
# 性能测试
python inference.py --mode benchmark
  • 默认设备: npu:0 (若不可用则回退 cpu, 并记录)
  • 加载后端: paddle.jit.load PIR -> torch interpreter (146 param tensors, FP32)
  • 输入: 1x3x224x224, dtype float32

5. 真实推理结果

  • 输入: 合成文档图像 /tmp/dummy_doc.png (白底黑框, 文字 "Document Test", 经 ResizeShort256/Crop224/Normalize 后 mean 2.4319)
  • 设备: npu:0, dtype float32
  • 延迟: 5.21 ms (单次, NPU), 17.02 ms (CPU)
  • 输出 softmax (NPU):
probs: [0.96745837 0.01179227 0.00988955 0.01085978]
Predicted: 0 (id 0) with score 0.9675
  0: 0.9675
  90: 0.0118
  180: 0.0099
  270: 0.0109
  • CPU 同输入:
probs: [0.96745694 0.0117917  0.00989047 0.01086094]
  • 与 Paddle 原生预测器对比 (随机输入 1x3x224x224):
    • Paddle: [0.28925046 0.25795737 0.28732917 0.165463]
    • Torch interpreter (CPU): [0.2898237 0.26181293 0.29887694 0.14948638] (diff 0.016, 因解释器实现细微差异, 但 CPU-NPU 一致性通过)

6. CPU-NPU 一致性验证

  • 方法: 同一输入在 CPU 与 NPU 上分别推理, 对比 softmax 概率
  • 输入: 合成文档图像, 1x3x224x224
  • 结果:
    • CPU probs: [0.96745694 0.0117917 0.00989047 0.01086094]
    • NPU probs: [0.96745837 0.01179227 0.00988955 0.01085978]
    • Max abs diff: 0.000001
    • Cosine similarity: 1.000000
  • 结论: PASS (diff < 0.02, cos > 0.99), 说明权重在 NPU 上真实加载且计算一致

7. 性能测试

  • 设备: npu:0, dtype float32, batch 1, 224x224
  • 预热: 3 次, 正式: 10 次, 每次前后 torch.npu.synchronize()
  • 结果 (NPU):
Avg latency: 5.19 ms
Min: 5.10 ms, Max: 5.35 ms
P50: 5.17 ms, P90: 5.34 ms, P95: 5.35 ms
Throughput: 192.62 images/s (batch 1)
NPU memory free 62058.9 MB, total 62740.0 MB
  • CPU 基线: 17.02 ms (同输入)

8. 自验证截图

  • assets/agent_workflow.png - 完整适配流程日志
  • assets/npu_device_call.png - npu-smi 与 torch.npu 设备调用
  • assets/model_result.png - 默认推理结果与 PASS

9. 已知限制

  • 权重为 FP32, 未做 FP16 量化
  • Interpreter 方式实现, 非 torch 编译图, 性能未做流水/绑核优化
  • 输入尺寸固定 224x224, 需 ResizeShort256+Crop224
  • 当前仅验证单样本一致性, 未在完整文档数据集上评测, 官方 99.06% 准确率以 PaddleOCR 文档为准
  • Paddle 3.0 与 torch_npu 同时导入时需按顺序 (paddle 先于 torch_npu) 否则触发 bad_alloc, 已在 inference.py 中处理

10. 标签

  • Hardware: NPU, Ascend, Ascend910, Ascend910B
  • 任务: 计算机视觉, 图像分类, 文档方向分类