模型:
microsoft/trocr-base-printed
TrOCR模型在 SROIE dataset 上进行了微调。它是由Li等人在 TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models 中提出的,并于 this repository 首次发布。
免责声明:发布TrOCR的团队没有为该模型撰写模型卡,因此此模型卡是由Hugging Face团队撰写的。
TrOCR模型是一个编码器-解码器模型,由图像Transformer作为编码器和文本Transformer作为解码器组成。图像编码器从BEiT的权重初始化,而文本解码器从RoBERTa的权重初始化。
图像以固定大小的补丁序列(分辨率为16x16)的形式呈现给模型,然后进行线性嵌入。在将序列馈送到Transformer编码器的层之前,还添加了绝对位置嵌入。接下来,Transformer文本解码器自回归生成标记。
您可以使用原始模型对单行文本图像进行光学字符识别(OCR)。查看 model hub 以查找您感兴趣的任务的微调版本。
以下是在PyTorch中使用此模型的方法:
from transformers import TrOCRProcessor, VisionEncoderDecoderModel from PIL import Image import requests # load image from the IAM database (actually this model is meant to be used on printed text) url = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg' image = Image.open(requests.get(url, stream=True).raw).convert("RGB") processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-printed') model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-printed') pixel_values = processor(images=image, return_tensors="pt").pixel_values generated_ids = model.generate(pixel_values) generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
@misc{li2021trocr, title={TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models}, author={Minghao Li and Tengchao Lv and Lei Cui and Yijuan Lu and Dinei Florencio and Cha Zhang and Zhoujun Li and Furu Wei}, year={2021}, eprint={2109.10282}, archivePrefix={arXiv}, primaryClass={cs.CL} }