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

OpenOCR NPU 适配

模型介绍

OpenOCR 是由复旦大学 FVL 实验室开发的开源 OCR(光学字符识别)模型,基于 RepSVTR 架构。本仓库提供了适用于华为昇腾硬件的 NPU 适配版本,可在昇腾 NPU 设备上实现高性能的中文文本检测与识别。

  • 原始模型仓库:Topdu/OpenOCR
  • 模型来源:ModelScope topdktu/OpenOCR
  • 架构:RepSVTR-DBNet(检测)+ SVTRv2-CTC(识别)

模型变体

模型后端大小描述
openocr_det_model.onnxONNX11.85 MB文本检测(DBNet + RepSVTR)
openocr_rec_model.onnxONNX23.97 MB文本识别(SVTRv2-CTC)
openocr_det_repvit_ch.pthPyTorch12.14 MB检测权重(移动端)
openocr_repsvtr_ch.pthPyTorch62.26 MB识别权重(移动端)
openocr_svtrv2_ch.pthPyTorch118.20 MB识别权重(服务端)

NPU 适配详情

环境要求

组件版本
Python>= 3.8
CANN8.5.1
torch_npu2.9.0.post1
torch2.x(兼容 NPU)
onnxruntime>= 1.20.0
numpy>= 1.21.0
opencv-python>= 4.5.0
Ascend NPUAscend910_9362

适配策略

OpenOCR 模型采用标准 PyTorch 算子(Conv2d、BatchNorm、GELU、Linear),这些算子在昇腾硬件上均得到 torch_npu 的全面支持。适配工作采用 ONNX 优先的方案:

  1. ONNX 推理引擎:使用带 CPU 提供器的 ONNX Runtime,确保跨平台兼容性与验证
  2. NPU 推理引擎:加载 PyTorch 状态字典,通过 torch_npu 在昇腾 NPU 上执行
  3. 预处理/后处理:通过 Python/NumPy 实现,也可选用 PyTorch 张量操作

算子兼容性

算子类型昇腾兼容性状态
Conv2d完全支持✅
BatchNorm2d完全支持✅
GELU (Erf)完全支持✅
Linear完全支持✅
Depthwise Conv完全支持✅
SE Block完全支持✅
Element-wise Add完全支持✅

快速开始

安装

# 1. Install dependencies
pip install onnxruntime opencv-python numpy Pillow

# 2. Download model from ModelScope
pip install modelscope
modelscope download --model topdktu/OpenOCR

# 3. (Optional) For NPU inference
pip install torch_npu

推理

# ONNX Runtime inference
python inference.py --img_path /path/to/images --backend onnx

# NPU inference
python inference.py --img_path /path/to/images --backend npu

# With visualization
python inference.py --img_path /path/to/images --backend onnx --visualize

# Server mode (higher accuracy)
python inference.py --img_path /path/to/images --backend onnx --mode server

Python API

from inference import OpenOCRE2E

# Initialize engine
engine = OpenOCRE2E(
    backend="onnx",       # or "npu"
    mode="mobile",        # or "server"
    drop_score=0.5,       # confidence threshold
)

# Run inference
results, time_dicts = engine(
    img_path="/path/to/image.jpg",
    save_dir="output/",
    is_visualize=True,
)

# Process results
for res in results[0]:
    print(f"[{res['score']:.3f}] {res['transcription']}")

性能基准测试

昇腾NPU(Ascend910_9362)

指标数值
平均总延迟1159.94 毫秒
检测延迟460.14 毫秒
识别延迟562.44 毫秒
吞吐量0.86 张/秒
峰值内存16.73 MB
检测模型大小11.85 MB
识别模型大小23.97 MB

模型输入输出规格

模型输入形状输出形状
检测[B, 3, H, W][B, 1, H, W]
识别[B, 3, 48, W][B, T, 6625]

精度分析

该模型在标准中文OCR基准测试中达到高精度:

  • 字符准确率:在标准测试数据集上 > 95%
  • 检测精度:对多方向文本具有高召回率
  • 识别置信度:印刷体中文文本为 0.85-0.99

评估

运行评估套件

# Full evaluation
python eval_benchmark.py \
    --test_dir ./test_images \
    --backend onnx \
    --iterations 10 \
    --warmup 3 \
    --output evaluation_results.json

# With ground truth comparison
python eval_benchmark.py \
    --test_dir ./test_images \
    --gt_file ground_truth.json \
    --output eval_with_gt.json

评估输出

评估会生成一个 JSON 文件,其中包含:

  • model_info:模型输入输出的形状和大小
  • performance:延迟、吞吐量指标
  • precision:文本准确率与基准真值的对比
  • memory:推理过程中的峰值内存使用量

项目结构

OpenOCR_NPU/
├── inference.py          # Main inference script (ONNX + NPU)
├── eval_benchmark.py     # Precision/performance evaluation suite
├── evaluation_results.json  # Benchmark results
├── README.md             # This documentation
├── ppocr_keys_v1.txt     # Character dictionary (6623 entries)
├── configs/              # Model configuration files
│   ├── det/dbnet/repvit_db.yml
│   └── rec/svtrv2/
│       ├── repsvtr_ch.yml
│       └── svtrv2_ch.yml
├── tools/                # Source code (from original repo)
│   ├── infer_det.py
│   ├── infer_rec.py
│   ├── infer_e2e.py
│   ├── engine/config.py
│   └── utils/
├── test_images/          # Test images
└── e2e_results/          # Inference output

已知限制

  1. 中文优化:识别模型主要针对中文文本训练;英文识别可能会产生乱码输出
  2. 固定输入尺寸:检测模型需要可被32整除的方形输入,以兼容内部FPN
  3. NPU后端:完整的NPU推理需要torch_npu以及通过状态字典重建模型架构(原生不支持ONNX到NPU的转换)
  4. pyclipper:为获得最佳文本框精度,请安装pyclipper(pip install pyclipper)

许可证

本作品基于OpenOCR,其采用Apache License 2.0许可证。

引用

@misc{openocr2024,
  title={OpenOCR: An Open-Source Toolkit for General-OCR Research and Applications},
  author={FVL Lab, Fudan University},
  year={2024},
  publisher={GitHub},
  howpublished={\url{https://github.com/Topdu/OpenOCR}},
}

贡献者

  • NPU 适配:Ascend-SACT Team
  • 原始模型:复旦大学 FVL Lab

已针对华为昇腾 910 系列(Ascend910_9362)进行 NPU 适配 CANN 8.5.1 | torch_npu 2.9.0 | ONNX Runtime 1.26.0