🚀 TrOCR (small-sized model, pre-trained only)
TrOCR is a pre-trained only model for optical character recognition (OCR). It offers a novel approach using Transformer architectures for OCR tasks.
🚀 Quick Start
TrOCR is a pre - trained model introduced in the paper TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models by Li et al. and first released in this repository. You can utilize it for OCR on single text - line images.
✨ Features
- Encoder - Decoder Architecture: The TrOCR model is an encoder - decoder model. It uses an image Transformer as the encoder and a text Transformer as the decoder.
- Initialization from Pre - trained Models: The image encoder is initialized from the weights of DeiT, and the text decoder is initialized from the weights of UniLM.
- Patch - based Image Input: Images are presented to the model as a sequence of fixed - size patches (resolution 16x16), which are linearly embedded.
📚 Documentation
Model description
The TrOCR model is an encoder - decoder model, consisting of an image Transformer as encoder, and a text Transformer as decoder. The image encoder was initialized from the weights of DeiT, while the text decoder was initialized from the weights of UniLM.
Images are presented to the model as a sequence of fixed - size patches (resolution 16x16), which are linearly embedded. One also adds absolute position embeddings before feeding the sequence to the layers of the Transformer encoder. Next, the Transformer text decoder autoregressively generates tokens.
Intended uses & limitations
You can use the raw model for optical character recognition (OCR) on single text - line images. See the model hub to look for fine - tuned versions on a task that interests you.
💻 Usage Examples
Basic Usage
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
import requests
import torch
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')
pixel_values = processor(image, return_tensors="pt").pixel_values
decoder_input_ids = torch.tensor([[model.config.decoder.decoder_start_token_id]])
outputs = model(pixel_values=pixel_values, decoder_input_ids=decoder_input_ids)
BibTeX entry and citation info
@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}
}