TrOCR
This model was released on 2021-09-21 and added to Hugging Face Transformers on 2021-10-13.
TrOCR is a text recognition model for both image understanding and text generation. It doesn’t require separate models for image processing or character generation. TrOCR is a simple single end-to-end system that uses a transformer to handle visual understanding and text generation.
You can find all the original TrOCR checkpoints under the Microsoft organization.
TrOCR architecture. Taken from the original paper.
Click on the TrOCR models in the right sidebar for more examples of how to apply TrOCR to different image and text tasks.
The example below demonstrates how to perform optical character recognition (OCR) with the AutoModel class.
from transformers import TrOCRProcessor, VisionEncoderDecoderModelimport requestsfrom PIL import Image
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
# load image from the IAM dataseturl = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg"image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
pixel_values = processor(image, return_tensors="pt").pixel_valuesgenerated_ids = model.generate(pixel_values)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]print(generated_text)Quantization
Section titled “Quantization”Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the Quantization overview for more available quantization backends.
The example below uses bitsandbytes to quantize the weights to 8-bits.
# pip install bitsandbytes acceleratefrom transformers import TrOCRProcessor, VisionEncoderDecoderModel, BitsandBytesConfigimport requestsfrom PIL import Image
# Set up the quantization configurationquantization_config = BitsandBytesConfig(load_in_8bit=True)
# Use a large checkpoint for a more noticeable impactprocessor = TrOCRProcessor.from_pretrained("microsoft/trocr-large-handwritten")model = VisionEncoderDecoderModel.from_pretrained( "microsoft/trocr-large-handwritten", quantization_config=quantization_config)
# load image from the IAM dataseturl = "[https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg](https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg)"image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
pixel_values = processor(image, return_tensors="pt").pixel_valuesgenerated_ids = model.generate(pixel_values)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]print(generated_text)- TrOCR wraps
ViTImageProcessor/DeiTImageProcessorandRobertaTokenizer/XLMRobertaTokenizerinto a single instance ofTrOCRProcessorto handle images and text. - TrOCR is always used within the VisionEncoderDecoder framework.
Resources
Section titled “Resources”- A blog post on Accelerating Document AI with TrOCR.
- A blog post on how to Document AI with TrOCR.
- A notebook on how to finetune TrOCR on IAM Handwriting Database using Seq2SeqTrainer.
- An interactive-demo on TrOCR handwritten character recognition.
- A notebook on inference with TrOCR and Gradio demo.
- A notebook on evaluating TrOCR on the IAM test set.
TrOCRConfig
Section titled “TrOCRConfig”[[autodoc]] TrOCRConfig
TrOCRProcessor
Section titled “TrOCRProcessor”[[autodoc]] TrOCRProcessor - call - from_pretrained - save_pretrained - batch_decode - decode
TrOCRForCausalLM
Section titled “TrOCRForCausalLM”[[autodoc]] TrOCRForCausalLM - forward