PaddlePaddle/PP-LCNet_x1_0_doc_ori on Ascend NPU
1. 模型简介
- 模型名称: PaddlePaddle/PP-LCNet_x1_0_doc_ori
- 权重地址: https://huggingface.co/PaddlePaddle/PP-LCNet_x1_0_doc_ori
- 任务: 文档图像方向分类 (4类: 0°, 90°, 180°, 270°)
- 架构: PP-LCNet_x1_0 (轻量 CNN, DepthwiseSeparable + SE, 基于 PaddlePaddle)
- 参数量: ~6.5 MB (inference.pdiparams 6.5M), 4分类头
- 输入: 3x224x224 RGB, 输出: 4维 softmax (0/90/180/270)
- 精度指标: 官方平均准确率 99.06% (文档方向分类)
2. 验证环境
- 硬件: Ascend910 (Ascend910_9362), 2 devices, NPU 5 (Chip Phy-ID 10,11)
- CANN: 8.5.1
- OS: Linux (aarch64)
- Python: 3.11.14
- PyTorch: 2.9.0+cpu
- torch_npu: 2.9.0.post1+gitee7ba04
- PaddlePaddle: 3.2.2
- npu-smi: 25.5.5
- 设备: npu:0 (Ascend910_9362), 可用内存 65 GB
3. 安装依赖
pip install -r requirements.txt
# requirements.txt:
# torch>=2.0
# torch-npu>=2.0
# paddlepaddle==3.2.2
# huggingface-hub>=0.20
# Pillow>=9.0
# numpy>=1.20
4. NPU 推理
huggingface-cli download PaddlePaddle/PP-LCNet_x1_0_doc_ori --local-dir ./model_download --local-dir-use-symlinks False
- 转换: 首次运行
python inference.py 会自动将 Paddle PIR 权重转换为 PyTorch (pplcnet_torch.pth), 耗时 ~5s
- 推理:
python inference.py
python inference.py --mode validate
python inference.py --mode benchmark --batch-size 1
- 默认使用
npu:0, FP32, 1x3x224x224
预处理 (与官方一致):
- ResizeImage: resize_short=256 (等比缩放短边至256)
- CropImage: center crop 224x224
- NormalizeImage: scale 0.00392156, mean [0.485,0.456,0.406], std [0.229,0.224,0.225]
- ToCHWImage
5. 真实推理结果
- 输入: 合成 224x224 随机图像 (或任意文档图像), 经上述预处理为 1x3x224x224, FP32
- NPU 推理 (npu:0, FP32) 示例:
Model: PaddlePaddle/PP-LCNet_x1_0_doc_ori
Task: doc orientation classification (4 classes: 0,90,180,270)
Backend: torch
HAS_NPU: True
NPU device count: 2
NPU device name: Ascend910_9362
Device: npu:0
Dtype: float32
Input: 1x3x224x224
Model cache found at model_download
Loading torch weights from pplcnet_torch.pth
First param device: npu:0
Model dtype: torch.float32
Input shape: (1, 3, 224, 224), mean 0.2149, std 0.8612
Inference latency: 5.22 ms
Output shape: (1, 4)
Output (softmax): [0.27350044 0.25338474 0.2636051 0.20950978]
Predicted class: 0 (0 degrees), score: 0.2735
Topk ranking:
1. 0 (id 0) score 0.2735
2. 180 (id 2) score 0.2636
3. 90 (id 1) score 0.2534
4. 270 (id 3) score 0.2095
Status: PASS
- 与 Paddle 预测器对比 (同输入, 随机 dummy 1x3x224x224):
paddle out [[0.31338215 0.24533409 0.25822705 0.18305673]]
torch out [[0.31338215 0.24533391 0.25822705 0.1830568 ]]
diff max 1.79e-07, mean 6.33e-08 -> PASS (atol 1e-4)
6. CPU-NPU 一致性验证
- 同权重、同输入、同预处理、FP32、随机输入 1x3x224x224
- CPU 输出: [0.2734707 0.25331745 0.26365587 0.20955595]
- NPU 输出: [0.27350044 0.25338474 0.2636051 0.20950978]
- Max abs diff: 6.7e-05, Mean diff: 4.8e-05
- 阈值: atol 1e-3, rtol 1e-3 -> PASS
- 预测标签一致: 0 (0 degrees) -> PASS
- 说明: 单样本 smoke consistency, 非完整数据集评测; NPU 与 CPU 数值差异在 FP32 预期范围内。
7. 性能测试
- 设备: npu:0, FP32, 1x3x224x224, batch=1
- 预热: 3 次, 正式: 10 次, 同步计时 (torch.npu.synchronize 前后)
- 结果 (示例):
Avg: 4.62 ms, Min: 4.56 ms, Max: 4.65 ms
P50: 4.62 ms, P90: 4.64 ms, P95: 4.64 ms
Throughput: 216.68 images/s
Peak NPU memory: 13.22 MB
- 首次编译/转换不计入稳定推理计时; 预处理 (Resize/Crop/Normalize) 不计入 NPU 计时, 仅模型 forward 计时。
8. 自验证截图
三图由 xterm.js 基于本次真实日志渲染 (raw 模式), 提示符为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ (仅展示标签)。
9. 已知限制
- 仅验证单样本 smoke, 未在完整文档方向数据集上评测准确率; 官方准确率 99.06% 需在完整数据上验证。
- 仅支持 FP32, 未验证 FP16/BF16; NPU 与 CPU 在 FP32 下差异 ~1e-4, 在允许范围内。
- 输入固定 224x224, 不支持动态 shape; 多 batch 需手动扩展。
- 首次运行需联网下载 HuggingFace 权重 (或本地 model_download), 并执行 Paddle->Torch 转换 (~5s + 6.5M 权重)。
- Dropout 在推理时按 Paddle 逻辑固定缩放 0.8 (而非随机)。
- 仅单卡 npu:0, 未验证多卡或分布式。
10. 标签
- Hardware: NPU
- NPU
- Ascend
- Ascend910
- PaddlePaddle
- PP-LCNet
- doc-orientation
- image-classification
#NPU