Views
No views yet
1from transformers import AutoProcessor, UdopForConditionalGeneration
2from datasets import load_dataset
3
4# load model and processor
5# in this case, we already have performed OCR ourselves
6# so we initialize the processor with `apply_ocr=False`
7processor = AutoProcessor.from_pretrained("microsoft/udop-large", apply_ocr=False)
8model = UdopForConditionalGeneration.from_pretrained("microsoft/udop-large")
9
10# load an example image, along with the words and coordinates
11# which were extracted using an OCR engine
12dataset = load_dataset("nielsr/funsd-layoutlmv3", split="train")
13example = dataset[0]
14image = example["image"]
15words = example["tokens"]
16boxes = example["bboxes"]
17question = "Question answering. What is the date on the form?"
18
19# prepare everything for the model
20encoding = processor(image, question, words, boxes=boxes, return_tensors="pt")
21
22# autoregressive generation
23predicted_ids = model.generate(**encoding)
24print(processor.batch_decode(predicted_ids, skip_special_tokens=True)[0])
259/30/921@misc{tang2023unifying,
2 title={Unifying Vision, Text, and Layout for Universal Document Processing},
3 author={Zineng Tang and Ziyi Yang and Guoxin Wang and Yuwei Fang and Yang Liu and Chenguang Zhu and Michael Zeng and Cha Zhang and Mohit Bansal},
4 year={2023},
5 eprint={2212.02623},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV}
8}