panhg/ofa_ocr-recognition_handwriting_base_zh
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

OFA-OCR Handwriting Recognition (Ascend NPU 适配)

基于 OFA (One-For-All) 的中文手写体文字识别模型,已完成华为昇腾 Ascend 910 NPU 适配。

模型简介

OFA-OCR 是阿里达摩院提出的通用多模态预训练模型 OFA (One-For-All) 在手写文字识别任务上的应用。该模型使用序列到序列的学习框架统一多模态和多个任务,在手写文字识别公开数据集上达到了业界领先的准确率。

本仓库提供了该模型在华为昇腾 Ascend 910 NPU 上的推理适配代码与评测结果。

模型信息

属性值
模型名称ofa_ocr-recognition_handwriting_base_zh
模型来源ModelScope (iic/ofa_ocr-recognition_handwriting_base_zh)
模型类型OFA (ResNet101 + Transformer Encoder-Decoder)
参数量~300M
输入手写文字图片 (480x480)
输出识别文字字符串
提示词"图片上的文字是什么?"

环境要求

  • 硬件: 华为 Ascend 910 NPU (Atlas 800 A2 或其他 Ascend 服务器)
  • CANN: 8.5.1 或以上
  • Python: 3.9+
  • PyTorch: 2.9.0
  • torch_npu: 2.9.0.post1
  • modelscope: 1.0+

快速开始

1. 环境准备

# 安装 ModelScope
pip install modelscope

# 安装依赖
pip install zhconv unicodedata2 torchvision Pillow numpy

2. 下载模型

modelscope download --model iic/ofa_ocr-recognition_handwriting_base_zh --local_dir ./ofa_ocr_model

3. 推理

# NPU 单次推理
python3 inference.py --device npu:0 --image "https://modelscope.oss-cn-beijing.aliyuncs.com/demo/images/ocr_handwriting_demo.png"

# NPU vs CPU 精度对比
python3 inference.py --device npu:0 --compare

# NPU 性能基准测试
python3 inference.py --device npu:0 --benchmark

4. Python API

from inference import OFAOCRInference

# 初始化 (自动加载到 NPU)
infer = OFAOCRInference("/path/to/ofa_ocr_model", device="npu:0")

# 推理
result = infer.inference("path/to/image.png")
print(result['text'])  # 输出: "这 件 好 事 像 节 日"

精度评测

NPU 推理结果与 CPU 基线进行对比,确保推理精度一致。

测试项CPU 输出NPU 输出文本匹配Token匹配结论
handwriting_demo这 件 好 事 像 节 日这 件 好 事 像 节 日True100.00%✓ PASS

精度结论: NPU 输出与 CPU 基线完全一致,误差 < 1%,满足精度要求。

性能评测

测试环境: Ascend 910 NPU × 2

指标值
平均延迟274.0 ms
中位延迟266.6 ms
最小延迟260.3 ms
最大延迟301.2 ms
吞吐量3.65 samples/s
vs CPU 加速比10.21x

交付件说明

文件说明
inference.pyNPU 推理脚本,支持单次推理/精度对比/性能基准测试
evaluate.py精度评测脚本,生成评测报告 JSON
README.md模型部署文档 (本文件)
evaluation_results.json评测结果数据
npu_inference.logNPU 推理运行日志

模型来源

本模型来自 ModelScope:

  • 模型页面: https://www.modelscope.cn/models/iic/ofa_ocr-recognition_handwriting_base_zh
  • 原始论文: OFA: Unifying Architectures, Tasks, and Modalities Through a Simple Sequence-to-Sequence Learning Framework (ICML 2022)
  • 官方仓库: https://github.com/OFA-Sys/OFA

引用

@article{wang2022ofa,
  author    = {Peng Wang and An Yang and Rui Men and Junyang Lin and Shuai Bai and
               Zhikang Li and Jianxin Ma and Chang Zhou and Jingren Zhou and Hongxia Yang},
  title     = {OFA: Unifying Architectures, Tasks, and Modalities Through a
               Simple Sequence-to-Sequence Learning Framework},
  journal   = {CoRR},
  volume    = {abs/2202.03052},
  year      = {2022}
}

许可

Apache License 2.0