cube-ocr 是一个基于 MinerU 的 OCR 工具包,包含多个子模型,支持文档方向分类、表格分类、表格结构识别和表格识别等功能。
| 子模型 | 任务类型 | 模型架构 | 输入形状 | 输出形状 |
|---|---|---|---|---|
| OriCls (ori) | 文档方向分类 | PP-LCNet x1.0 | (1, 3, 224, 224) | (1, 4) |
| TabCls (tab_cls) | 表格分类 | PP-LCNet x1.0 | (1, 3, 224, 224) | (1, 2) |
| Unet (unet) | 表格结构识别 | Unet | (1, 3, 480, 480) | (1, 1, 480, 480) |
| Slanet-Plus (slanet) | 表格识别 | Slanet-Plus | (1, 3, 480, 480) | - |
| 子模型 | NPU 适配 | 状态 |
|---|---|---|
| PP-LCNet 文档方向分类器 | 已完成 | ✅ |
| PP-LCNet 表格分类器 | 已完成 | ✅ |
| Unet 表格结构识别 | 已完成 | ✅ |
| Slanet-Plus 表格识别 | 转换失败 (ATC 解析错误,不支持的算子) | ❌ |
PP-LCNet_x1_0_doc_ori.onnx → pplcnet_ori.omPP-LCNet_x1_0_table_cls.onnx → pplcnet_tab_cls.omunet.onnx → unet.om本模型已通过以下步骤完成昇腾 NPU 适配:
pip install onnxruntime numpy Pillow# 文档方向分类器
atc --model=PP-LCNet_x1_0_doc_ori.onnx \
--output=pplcnet_ori \
--input_shape="x:1,3,224,224" \
--soc_version=Ascend910_9362 \
--framework=5
# 表格分类器
atc --model=PP-LCNet_x1_0_table_cls.onnx \
--output=pplcnet_tab_cls \
--input_shape="x:1,3,224,224" \
--soc_version=Ascend910_9362 \
--framework=5
# Unet 表格结构识别
atc --model=unet.onnx \
--output=unet \
--input_shape="x:1,3,480,480" \
--soc_version=Ascend910_9362 \
--framework=5# CPU 推理 (文档方向分类器)
python inference.py --mode cpu --submodel ori --image test_input.jpg --output ./results
# NPU 推理 (文档方向分类器)
python inference.py --mode npu --submodel ori --image test_input.jpg --output ./results# CPU 推理全部子模型
python inference.py --mode cpu --submodel all --image test_input.jpg --output ./results
# NPU 推理全部子模型
python inference.py --mode npu --submodel all --image test_input.jpg --output ./results| 子模型 | CPU 耗时 | NPU 耗时 | 加速比 |
|---|---|---|---|
| PP-LCNet 文档方向分类器 | 3.16 ms | 0.32 ms | 9.9x |
| PP-LCNet 表格分类器 | 2.68 ms | 0.31 ms | 8.6x |
NPU 推理速度相比 CPU 提升约 8-10 倍。
详见 inference.py,支持 CPU 和 NPU 两种运行模式:
# CPU 推理
from inference import run_cpu
output, elapsed = run_cpu("ori", "input.jpg", "model.onnx", "./results")
# NPU 推理
from inference import run_npu
output, elapsed = run_npu("ori", "input.jpg", "model.om", "./results").npy)compare_cpu_npu.py 计算各项精度指标# 对比单个子模型
python compare_cpu_npu.py --submodel ori
# 对比全部子模型
python compare_cpu_npu.py --submodel all| 指标 | 数值 |
|---|---|
| CPU 最小值 | 0.240963 |
| CPU 最大值 | 0.257399 |
| CPU 平均值 | 0.250000 |
| NPU 最小值 | 0.240601 |
| NPU 最大值 | 0.257568 |
| NPU 平均值 | 0.249969 |
| MAE (平均绝对误差) | 0.000150 |
| Max AE (最大绝对误差) | 0.000362 |
| 平均相对误差 | 0.06% |
| 余弦相似度 | 1.000000 |
| 相对误差 < 1% 比例 | 100.00% |
| 分类一致率 | 100.00% |
| 指标 | 数值 |
|---|---|
| CPU 最小值 | 0.270690 |
| CPU 最大值 | 0.729310 |
| CPU 平均值 | 0.500000 |
| NPU 最小值 | 0.270020 |
| NPU 最大值 | 0.729980 |
| NPU 平均值 | 0.500000 |
| MAE (平均绝对误差) | 0.000670 |
| Max AE (最大绝对误差) | 0.000670 |
| 平均相对误差 | 0.17% |
| 余弦相似度 | 0.999999 |
| 相对误差 < 1% 比例 | 100.00% |
| 分类一致率 | 100.00% |
| 指标 | 数值 |
|---|---|
| MAE (平均绝对误差) | 0.000000 |
| 平均相对误差 | 0.00% |
| 相对误差 < 1% 比例 | 100.00% |
精度测试结论:各子模型 NPU 与 CPU 推理平均相对误差均小于 1%,符合精度要求。

本仓库提供完整的推理脚本,支持 CPU 和 NPU 双平台推理:
# NPU 推理
python3 inference.py --device npu
# CPU 推理
python3 inference.py --device cpu推理完成后会输出推理结果和耗时,表明模型在 NPU 上推理成功。