Package: donut-python
Donut 🍩 : Document Understanding TransformerOfficial Implementation of Donut and SynthDoG |Paper|Slide|PosterIntroductionDonut🍩,Documentunderstandingtransformer, is a new method of document understanding that utilizes an OCR-free end-to-end Transformer model. Donut does not require off-the-shelf OCR engines/APIs, yet it shows state-of-the-art performances on various visual document understanding tasks, such as visual document classification or information extraction (a.k.a. document parsing).
In addition, we presentSynthDoG🐶,SyntheticDocumentGenerator, that helps the model pre-training to be flexible on vairous languages and domains.Our academic paper, which describes our method in detail and provides full experimental results and analyses, can be found here:OCR-free Document Understanding Transformer.Geewook Kim,Teakgyu Hong,Moonbin Yim,JeongYeon Nam,Jinyoung Park,Jinyeong Yim,Wonseok Hwang,Sangdoo Yun,Dongyoon Han,Seunghyun Park. In ECCV 2022.Pre-trained Models and Web DemosGradio web demos are available!You can run the demo with./app.pyfile.Sample images are available at./miscand more receipt images are available atCORD dataset link.Web demos are available from the links in the following table.TaskSec/ImgScoreTrained ModelDemoCORD(Document Parsing)0.7 /0.7 /1.291.3 /91.1 /90.9donut-base-finetuned-cord-v2(1280) /donut-base-finetuned-cord-v1(1280) /donut-base-finetuned-cord-v1-2560gradio space web demo,google colab demoTrain Ticket(Document Parsing)0.698.7donut-base-finetuned-zhtrainticketgoogle colab demoRVL-CDIP(Document Classification)0.7595.3donut-base-finetuned-rvlcdipgradio space web demo,google colab demoDocVQA Task1(Document VQA)0.7867.5donut-base-finetuned-docvqagradio space web demo,google colab demoThe links to the pre-trained backbones are here:donut-base: trained with 64 A100 GPUs (~2.5 days), number of layers (encoder: {2,2,14,2}, decoder: 4), input size 2560x1920, swin window size 10, IIT-CDIP (11M) and SynthDoG (English, Chinese, Japanese, Korean, 0.5M x 4).donut-proto: (preliminary model) trained with 8 V100 GPUs (~5 days), number of layers (encoder: {2,2,18,2}, decoder: 4), input size 2048x1536, swin window size 8, and SynthDoG (English, Japanese, Korean, 0.4M x 3).Please seeour paperfor more details.SynthDoG datasetsThe links to the SynthDoG-generated datasets are here:synthdog-en: English, 0.5M.synthdog-zh: Chinese, 0.5M.synthdog-ja: Japanese, 0.5M.synthdog-ko: Korean, 0.5M.To generate synthetic datasets with our SynthDoG, please see./synthdog/README.mdandour paperfor details.Updates2022-11-14New version 1.0.9 is released (pip install donut-python --upgrade). See1.0.9 Release Notes.2022-08-12Donut 🍩 is also available athuggingface/transformers 🤗(contributed by@NielsRogge).donut-pythonloads the pre-trained weights from theofficialbranch of the model repositories. See1.0.5 Release Notes.2022-08-05A well-executed hands-on tutorial on donut 🍩 is published atTowards Data Science(written by@estaudere).2022-07-20First Commit, We release our code, model weights, synthetic data and generator.Software installationpipinstalldonut-pythonor clone this repository and install the dependencies:gitclonehttps://github.com/clovaai/donut.gitcddonut/
condacreate-ndonut_officialpython=3.7
condaactivatedonut_official
pipinstall.We testeddonutwith:torch== 1.11.0+cu113torchvision== 0.12.0+cu113pytorch-lightning== 1.6.4transformers== 4.11.3timm== 0.5.4Getting StartedDataThis repository assumes the following structure of dataset:>treedataset_name
dataset_name
├──test│├──metadata.jsonl
│├──{image_path0}│├──{image_path1}│.
│.
├──train
│├──metadata.jsonl
│├──{image_path0}│├──{image_path1}│.
│.
└──validation├──metadata.jsonl├──{image_path0}├──{image_path1}..

>catdataset_name/test/metadata.jsonl{"file_name":{image_path0},"ground_truth":"{\"gt_parse\": {ground_truth_parse}, ... {other_metadata_not_used} ... }"}{"file_name":{image_path1},"ground_truth":"{\"gt_parse\": {ground_truth_parse}, ... {other_metadata_not_used} ... }"}..The structure ofmetadata.jsonlfile is inJSON Lines text format, i.e.,.jsonl. Each line consists offile_name: relative path to the image file.ground_truth: string format (json dumped), the dictionary contains eithergt_parseorgt_parses. Other fields (metadata) can be added to the dictionary but will not be used.donutinterprets all tasks as a JSON prediction problem. As a result, alldonutmodel training share a same pipeline. For training and inference, the only thing to do is preparinggt_parseorgt_parsesfor the task in format described below.For Document ClassificationThegt_parsefollows the format of{"class" : {class_name}}, for example,{"class" : "scientific_report"}or{"class" : "presentation"}.Google colab demo is availablehere.Gradio web demo is availablehere.For Document Information ExtractionThegt_parseis a JSON object that contains full information of the document image, for example, the JSON object for a receipt may look like{"menu" : [{"nm": "ICE BLACKCOFFEE", "cnt": "2", ...}, ...], ...}.More examples are available atCORD dataset.Google colab demo is availablehere.Gradio web demo is availablehere.For Document Visual Question AnsweringThegt_parsesfollows the format of[{"question" : {question_sentence}, "answer" : {answer_candidate_1}}, {"question" : {question_sentence}, "answer" : {answer_candidate_2}}, ...], for example,[{"question" : "what is the model name?", "answer" : "donut"}, {"question" : "what is the model name?", "answer" : "document understanding transformer"}].DocVQA Task1 has multiple answers, hencegt_parsesshould be a list of dictionary that contains a pair of question and answer.Google colab demo is availablehere.Gradio web demo is availablehere.For (Psuedo) Text Reading TaskThegt_parselooks like{"text_sequence" : "word1 word2 word3 ... "}This task is also a pre-training task of Donut model.You can use ourSynthDoG🐶 to generate synthetic images for the text reading task with propergt_parse. See./synthdog/README.mdfor details.TrainingThis is the configuration of Donut model training onCORDdataset used in our experiment.
We ran this with a single NVIDIA A100 GPU.pythontrain.py--configconfig/train_cord.yaml\--pretrained_model_name_or_path"naver-clova-ix/donut-base"\--dataset_name_or_paths'["naver-clova-ix/cord-v2"]'\--exp_version"test_experiment"..Prediction:<s_menu><s_nm>LemonTea(L)</s_nm><s_cnt>1</s_cnt><s_price>25.000</s_price></s_menu><s_total><s_total_price>25.000</s_total_price><s_cashprice>30.000</s_cashprice><s_changeprice>5.000</s_changeprice></s_total>
Answer:<s_menu><s_nm>LemonTea(L)</s_nm><s_cnt>1</s_cnt><s_price>25.000</s_price></s_menu><s_total><s_total_price>25.000</s_total_price><s_cashprice>30.000</s_cashprice><s_changeprice>5.000</s_changeprice></s_total>
NormedED:0.0
Prediction:<s_menu><s_nm>HulkTopperPackage</s_nm><s_cnt>1</s_cnt><s_price>100.000</s_price></s_menu><s_total><s_total_price>100.000</s_total_price><s_cashprice>100.000</s_cashprice><s_changeprice>0</s_changeprice></s_total>
Answer:<s_menu><s_nm>HulkTopperPackage</s_nm><s_cnt>1</s_cnt><s_price>100.000</s_price></s_menu><s_total><s_total_price>100.000</s_total_price><s_cashprice>100.000</s_cashprice><s_changeprice>0</s_changeprice></s_total>
NormedED:0.0
Prediction:<s_menu><s_nm>GiantSquid</s_nm><s_cnt>x1</s_cnt><s_price>Rp.39.000</s_price><s_sub><s_nm>C.Finishing-Cut</s_nm><s_price>Rp.0</s_price><sep/><s_nm>B.SpicyLevel-ExtremeHotRp.0</s_price></s_sub><sep/><s_nm>A.Flavour-Salt&Pepper</s_nm><s_price>Rp.0</s_price></s_sub></s_menu><s_sub_total><s_subtotal_price>Rp.39.000</s_subtotal_price></s_sub_total><s_total><s_total_price>Rp.39.000</s_total_price><s_cashprice>Rp.50.000</s_cashprice><s_changeprice>Rp.11.000</s_changeprice></s_total>
Answer:<s_menu><s_nm>GiantSquid</s_nm><s_cnt>x1</s_cnt><s_price>Rp.39.000</s_price><s_sub><s_nm>C.Finishing-Cut</s_nm><s_price>Rp.0</s_price><sep/><s_nm>B.SpicyLevel-ExtremeHot</s_nm><s_price>Rp.0</s_price><sep/><s_nm>A.Flavour-Salt&Pepper</s_nm><s_price>Rp.0</s_price></s_sub></s_menu><s_sub_total><s_subtotal_price>Rp.39.000</s_subtotal_price></s_sub_total><s_total><s_total_price>Rp.39.000</s_total_price><s_cashprice>Rp.50.000</s_cashprice><s_changeprice>Rp.11.000</s_changeprice></s_total>
NormedED:0.039603960396039604Epoch29:100%|█████████████|200/200[01:49<00:00,1.82it/s,loss=0.00327,exp_name=train_cord,exp_version=test_experiment]Some important arguments:--config: config file path for model training.--pretrained_model_name_or_path: string format, model name in huggingface modelhub or local path.--dataset_name_or_paths: string format (json dumped), list of dataset names in huggingface datasets or local paths.--result_path: file path to save model outputs/artifacts.--exp_version: used for experiment versioning. The output files are saved at{result_path}/{exp_version}/*TestWith the trained model, test images and ground truth parses, you can get inference results and accuracy scores.pythontest.py--dataset_name_or_pathnaver-clova-ix/cord-v2--pretrained_model_name_or_path./result/train_cord/test_experiment--save_path./result/output.json100%|█████████████|100/100[00:35<00:00,2.80it/s]Totalnumberofsamples:100,TreeEditDistance(TED)basedaccuracyscore:0.9129639764131697,F1accuracyscore:0.8406020841373987Some important arguments:--dataset_name_or_path: string format, the target dataset name in huggingface datasets or local path.--pretrained_model_name_or_path: string format, the model name in huggingface modelhub or local path.--save_path: file path to save predictions and scores.How to CiteIf you find this work useful to you, please cite:@inproceedings{kim2022donut,title={OCR-Free Document Understanding Transformer},author={Kim, Geewook and Hong, Teakgyu and Yim, Moonbin and Nam, JeongYeon and Park, Jinyoung and Yim, Jinyeong and Hwang, Wonseok and Yun, Sangdoo and Han, Dongyoon and Park, Seunghyun},booktitle={European Conference on Computer Vision (ECCV)},year={2022}}LicenseMIT license

Copyright (c) 2022-present NAVER Corp.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.