g
gcw_AVRCax4T/PP-LCNet_x1_0_doc_ori-ONNX
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

PP-LCNet_x1_0_doc_ori - 昇腾NPU适配

适用于华为昇腾NPU推理的PP-LCNet文档方向检测模型。 将文档图像分类为4种方向角度:0°、90°、180°、270°。

模型详情

  • 模型:PP-LCNet_x1_0_doc_ori
  • 来源:Liyulingyue/PP-LCNet_x1_0_doc_ori-ONNX
  • 架构:PP-LCNet(PaddleClas)
  • 输入:RGB图像,224x224
  • 输出:4分类softmax(0°、90°、180°、270°)
  • 原始格式:ONNX(opset 7)
  • 许可证:Apache 2.0

昇腾NPU适配

该模型已适配华为昇腾NPU(Atlas 800 A2/A3),使用以下工具:

  • PyTorch 2.9.0 + torch_npu 2.9.0
  • CANN 8.5.1
  • onnx2torch 用于ONNX到PyTorch的转换
  • onnxruntime 用于CPU参考验证

适配流程

  1. ONNX模型(opset 7)→ opset 14版本转换
  2. 通过onnx2torch将ONNX转换为PyTorch模型
  3. 通过torch_npu将PyTorch模型部署到昇腾NPU
  4. 对照CPU参考(onnxruntime)进行精度验证

快速开始

环境

  • 华为昇腾NPU(Atlas 800 A2/A3)
  • CANN 8.5.1+
  • Python 3.11+
  • PyTorch 2.9.0 + torch_npu
  • onnx、onnxruntime、onnx2torch

安装

# Install dependencies
pip install torch torch_npu onnx onnxruntime onnx2torch pillow numpy

# Download model from ModelScope
pip install modelscope
modelscope download --model Liyulingyue/PP-LCNet_x1_0_doc_ori-ONNX

推理

# Quick test (random input)
python3 inference.py

# Single image inference
python3 inference.py --image /path/to/document.jpg

# Accuracy comparison (NPU vs CPU)
python3 inference.py --compare

# Performance benchmark
python3 inference.py --benchmark --num-runs 100

Python API

from inference import load_pytorch_model, PPImagePreprocessor, run_npu_inference
from PIL import Image
import torch

# Load model
model = load_pytorch_model()

# Preprocess
preprocessor = PPImagePreprocessor()
img = Image.open("document.jpg")
input_arr = preprocessor(img)
input_tensor = torch.from_numpy(input_arr)

# NPU inference
output = run_npu_inference(model, input_tensor)
orientation = ["0", "90", "180", "270"][output.argmax()]
print(f"Document orientation: {orientation} degrees")

评估结果

硬件环境

  • 设备:Ascend910_9362(Atlas 800 A2)
  • NPU数量:2
  • CANN版本:8.5.1

精度(NPU与CPU参考值对比)

指标数值状态
测试样本数60-
最大绝对误差0.000682通过
平均绝对误差0.000104通过
最大相对误差0.300%通过(< 1%)
标签匹配率100.00%通过

性能(NPU推理)

指标数值
最小延迟5.23 ms
中位延迟5.98 ms
平均延迟~5.6 ms(稳定状态)
吞吐量(bs=1)~172 样本/秒

批处理规模扩展

批处理大小延迟(ms)吞吐量(样本/秒)加速比
15.82171.931.00x
25.55360.112.09x
45.64708.914.12x
85.661414.808.23x
165.582867.5316.68x

总结

  • 精度:通过(误差阈值 < 1%)- 最大相对误差 0.300%
  • 标签准确率:100%(60/60 样本)
  • 性能:~172 样本/秒吞吐量,~5.8ms 每样本延迟
  • 批处理规模扩展:在批处理大小为16时,实现接近线性的16.68倍加速

预处理流水线

Input Image → Resize(short=256) → CenterCrop(224×224) →
Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225]) → CHW → Model

文件结构

pplcnet_npu/
├── inference.py          # Main inference script (NPU/CPU)
├── evaluate.py           # Precision & performance evaluation
├── README.md             # This document
└── evaluation/
    ├── precision_results.json    # Precision evaluation data
    ├── performance_results.json  # Performance benchmark data
    ├── batch_results.json        # Batch scaling data
    └── evaluation_report.md      # Auto-generated evaluation report

模型架构

PP-LCNet 是 PaddleClas 推出的一种轻量级 CNN 架构,专为高效图像分类任务设计。 本特定模型经过训练,用于文档方向检测,可将输入图像分为 4 个方向类别(0°、90°、180°、270°)。

主要架构特点:

  • 深度可分离卷积
  • HardSwish 激活函数
  • 全局平均池化
  • 线性分类器(4 类 softmax 输出)

引用

@misc{pplcnet_ascend_npu,
  title={PP-LCNet Document Orientation Detection - Ascend NPU Adaptation},
  author={Ascend NPU Adaptation},
  year={2026},
  howpublished={\url{https://www.modelscope.cn/models/Liyulingyue/PP-LCNet_x1_0_doc_ori-ONNX}},
}