Views
No views yet
microsoft/trocr-base-handwritten,
a transformer-based optical character recognition model, adapted to work with handwritten math images and structured math syntax.MathWriting dataset. Contains over 500,000 digital inks of handwritten mathematical expressions obtained through either manual labelling or programmatic generation.1from transformers import TrOCRProcessor, VisionEncoderDecoderModel
2from PIL import Image
3
4# Helper funtion (path to either JPEG or PNG)
5def open_PIL_image(image_path: str) -> Image.Image:
6 image = Image.open(image_path)
7 if image_path.split('.')[-1].lower() == 'png':
8 image = Image.composite(image, PIL.Image.new('RGB', image.size, 'white'), image)
9 return image
10
11
12# Load model and processor from Hugging Face
13processor = TrOCRProcessor.from_pretrained('tjoab/latex_finetuned')
14model = VisionEncoderDecoderModel.from_pretrained('tjoab/latex_finetuned')
15
16
17# Load all images as a batch
18images = [open_PIL_image(path) for path in paths]
19
20# Preprocess the images
21preproc_image = processor.image_processor(images=images, return_tensors="pt").pixel_values
22
23# Generate and decode the tokens
24# NOTE: max_length default value is very small, which often results in truncated inference if not set
25pred_ids = model.generate(preproc_image, max_length=128)
26latex_preds = processor.batch_decode(pred_ids, skip_special_tokens=True)fp16 mixed precision
torch.cuda.amp for reduced memory usage.CER = (Substitutions + Insertions + Deletions) / Total Characters in Ground Truthx^2 vs. x_2\frac{a}{b} vs. \frac{b}{a}1@misc{li2021trocr,
2 title={TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models},
3 author={Minghao Li and Tengchao Lv and Lei Cui and Yijuan Lu and Dinei Florencio and Cha Zhang and Zhoujun Li and Furu Wei},
4 year={2021},
5 eprint={2109.10282},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL}
8}