w
willwell888/trocr-base-printed
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TrOCR on Ascend NPU

1. 简介

本文档记录 TrOCR-base-printed 在昇腾 NPU 环境的快速部署与验证结果。

TrOCR 是一个基于 Transformer 的光学字符识别 (OCR) 模型,由 Microsoft 开发。该模型在 SROIE 数据集上进行了微调,专门用于印刷体文字识别。

相关获取地址:

  • 权重下载地址(HuggingFace):https://huggingface.co/microsoft/trocr-base-printed
  • 原始模型仓库:https://github.com/microsoft/unilm/tree/master/trocr

2. 验证环境

组件版本
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
transformers5.15.1
pillowlatest
sentencepiece0.2.1
  • NPU:910B3
  • 模型路径:/workspace/weights/tcroft-base-printed

3. 环境准备

3.1 安装依赖

pip install torch torch_npu -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers pillow sentencepiece -i https://pypi.tuna.tsinghua.edu.cn/simple

3.2 检查 NPU 设备

npu-smi info

4. 推理步骤

4.1 运行推理

python inference.py --input ./test_image.jpg --output ./output

4.2 推理参数说明

参数类型默认值说明
--inputstr-输入图像路径
--outputstr./output输出目录
--model_pathstr/workspace/weights/tcroft-base-printed模型路径
--devicestrauto推理设备 (npu/cuda/cpu)

5. 测试样例及输出结果

样例 1:印刷体文字识别

输入:

./test_image.jpg (包含印刷体文字的图像)

输出:

[识别结果] 图像中的文字内容
[INFO] 推理完成,耗时: x.xxs

6. 注意事项

  • 推理脚本通过 transformers 和 torch_npu 调用 NPU 进行推理
  • 输入图像支持本地路径
  • 模型会自动从 HuggingFace 下载 Processor 权重