模型:
microsoft/trocr-small-stage1
TrOCR仅预训练的模型。它是由Li等人在 TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models 论文中提出,并于 this repository 首次发布。
TrOCR模型是一个编码器-解码器模型,由图像Transformer作为编码器和文本Transformer作为解码器组成。图像编码器从DeiT的权重初始化,而文本解码器从UniLM的权重初始化。
图像以固定大小的补丁序列(分辨率为16x16)的形式呈现给模型,然后通过线性嵌入进行嵌入。在将序列输入到Transformer编码器的层之前,还添加了绝对位置嵌入。接下来,Transformer文本解码器自回归生成标记。
您可以使用原始模型对单行文本图像进行光学字符识别(OCR)。请查看 model hub 以寻找您感兴趣的任务的微调版本。
以下是如何在PyTorch中使用此模型的示例:
from transformers import TrOCRProcessor, VisionEncoderDecoderModel from PIL import Image import requests import torch # load image from the IAM database 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-small-stage1') model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-stage1') # training pixel_values = processor(image, return_tensors="pt").pixel_values # Batch size 1 decoder_input_ids = torch.tensor([[model.config.decoder.decoder_start_token_id]]) outputs = model(pixel_values=pixel_values, decoder_input_ids=decoder_input_ids)
@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} }