简介
Donut 🍩,Document understanding transformer,是一种新的文档理解方法,利用无 OCR 的端到端 Transformer 模型。Donut 不需要现成的 OCR 引擎/API,但在各种视觉文档理解任务上均展现出最先进的性能,例如视觉文档分类或信息提取(又称文档解析)。 此外,我们提出了 SynthDoG 🐶,Synthetic Document Generator,它有助于模型预训练在各种语言和领域上保持灵活性。
我们详细描述该方法并提供完整实验结果和分析的学术论文,可在此处找到:
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.
预训练模型与网络演示
| Gradio 网络演示现已可用! |
|---|
![]() |
- 你可以使用
./app.py文件运行演示。 - 示例图像可在
./misc处获取,更多收据图像可在 CORD 数据集链接 处获取。 - 网络演示可通过下表中的链接访问。
- 注意:我们已更新 Google Colab 演示(截至 2023 年 6 月 15 日),以确保其正常运行。
| Task | Sec/Img | Score | Trained Model | Demo |
|---|---|---|---|---|
| CORD (Document Parsing) | 0.7 / 0.7 / 1.2 | 91.3 / 91.1 / 90.9 | donut-base-finetuned-cord-v2 (1280) / donut-base-finetuned-cord-v1 (1280) / donut-base-finetuned-cord-v1-2560 | gradio space web demo, google colab demo (updated at 23.06.15) |
| Train Ticket (Document Parsing) | 0.6 | 98.7 | donut-base-finetuned-zhtrainticket | google colab demo (updated at 23.06.15) |
| RVL-CDIP (Document Classification) | 0.75 | 95.3 | donut-base-finetuned-rvlcdip | gradio space web demo, google colab demo (updated at 23.06.15) |
| DocVQA Task1 (Document VQA) | 0.78 | 67.5 | donut-base-finetuned-docvqa | gradio space web demo, google colab demo (updated at 23.06.15) |
预训练骨干网络的链接在此处:
donut-base: 使用 64 块 A100 GPU 训练(约 2.5 天),层数(编码器:{2,2,14,2},解码器:4),输入尺寸 2560x1920,swin 窗口大小 10,IIT-CDIP(11M)和 SynthDoG(英语、中文、日语、韩语,0.5M x 4)。donut-proto: (初步模型)使用 8 块 V100 GPU 训练(约 5 天),层数(编码器:{2,2,18,2},解码器:4),输入尺寸 2048x1536,swin 窗口大小 8,以及 SynthDoG(英语、日语、韩语,0.4M x 3)。
请参阅我们的论文以获取更多细节。
SynthDoG 数据集

SynthDoG 生成的数据集链接如下:
synthdog-en: 英语,0.5M。synthdog-zh: 中文,0.5M。synthdog-ja: 日语,0.5M。synthdog-ko: 韩语,0.5M。
要使用我们的 SynthDoG 生成合成数据集,请参阅 ./synthdog/README.md 和 我们的论文 以获取详细信息。
更新
2023-06-15 我们已更新所有 Google Colab 演示,以确保其正常运行。
2022-11-14 新版本 1.0.9 已发布(pip install donut-python --upgrade)。请参阅 1.0.9 发布说明。
2022-08-12 Donut 🍩 也可在 huggingface/transformers 🤗 中使用(由 @NielsRogge 贡献)。 donut-python 从模型仓库的 official 分支加载预训练权重。请参阅 1.0.5 发布说明。
2022-08-05 一篇关于 donut 🍩 的精心制作的实践教程已发布在 Towards Data Science(由 @estaudere 撰写)。
2022-07-20 首次提交,我们发布了我们的代码、模型权重、合成数据和生成器。
软件安装
pip install donut-python
或克隆此仓库并安装依赖项:
git clone https://github.com/clovaai/donut.git
cd donut/
conda create -n donut_official python=3.7
conda activate donut_official
pip install .
我们在以下环境中测试了 donut-python == 1.0.1:
- torch == 1.11.0+cu113
- torchvision == 0.12.0+cu113
- pytorch-lightning == 1.6.4
- transformers == 4.11.3
- timm == 0.5.4
注意:根据多起报告的问题,我们注意到由于关键依赖库的近期更新,配置 donut-python 的测试环境面临更大的挑战。虽然我们正积极寻求解决方案,但已更新了 Google Colab 演示(截至 2023 年 6 月 15 日)以确保其正常运行。如需协助,建议您参考以下演示链接:CORD Colab Demo, Train Ticket Colab Demo, RVL-CDIP Colab Demo, DocVQA Colab Demo。
入门指南
数据
本仓库假设数据集具有以下结构:
> tree dataset_name
dataset_name
├── test
│ ├── metadata.jsonl
│ ├── {image_path0}
│ ├── {image_path1}
│ .
│ .
├── train
│ ├── metadata.jsonl
│ ├── {image_path0}
│ ├── {image_path1}
│ .
│ .
└── validation
├── metadata.jsonl
├── {image_path0}
├── {image_path1}
.
.
> cat dataset_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} ... }"}
.
.
metadata.jsonl文件的结构采用 JSON Lines 文本格式,即.jsonl。每一行包含file_name:图像文件的相对路径。ground_truth:字符串格式(json dumped),该字典包含gt_parse或gt_parses。可以向字典中添加其他字段(元数据),但不会被使用。
donut将所有任务解释为 JSON 预测问题。因此,所有donut模型训练共享相同的流程。对于训练和推理,唯一需要做的就是按照以下格式为任务准备gt_parse或gt_parses。
用于文档分类
gt_parse 遵循 {"class" : {class_name}} 的格式,例如,{"class" : "scientific_report"} 或 {"class" : "presentation"}。
用于文档信息提取
gt_parse 是一个 JSON 对象,包含文档图像的完整信息,例如,收据的 JSON 对象可能类似于 {"menu" : [{"nm": "ICE BLACKCOFFEE", "cnt": "2", ...}, ...], ...}。
用于文档视觉问答
gt_parses 遵循 [{"question" : {question_sentence}, "answer" : {answer_candidate_1}}, {"question" : {question_sentence}, "answer" : {answer_candidate_2}}, ...] 的格式,例如,[{"question" : "what is the model name?", "answer" : "donut"}, {"question" : "what is the model name?", "answer" : "document understanding transformer"}]。
用于(伪)文本阅读任务
gt_parse 类似于 {"text_sequence" : "word1 word2 word3 ... "}
- 该任务也是 Donut 模型的预训练任务。
- 您可以使用我们的 SynthDoG 🐶 为文本阅读任务生成带有适当
gt_parse的合成图像。详情请参阅./synthdog/README.md。
训练
这是我们在实验中使用的 Donut 模型在 CORD 数据集上的训练配置。 我们在单块 NVIDIA A100 GPU 上运行了此配置。
python train.py --config config/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>Lemon Tea (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>Lemon Tea (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>
Normed ED: 0.0
Prediction: <s_menu><s_nm>Hulk Topper Package</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>Hulk Topper Package</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>
Normed ED: 0.0
Prediction: <s_menu><s_nm>Giant Squid</s_nm><s_cnt>x 1</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.Spicy Level - Extreme Hot Rp. 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>Giant Squid</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.Spicy Level - Extreme Hot</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>
Normed ED: 0.039603960396039604
Epoch 29: 100%|█████████████| 200/200 [01:49<00:00, 1.82it/s, loss=0.00327, exp_name=train_cord, exp_version=test_experiment]
一些重要的参数:
--config:模型训练的配置文件路径。--pretrained_model_name_or_path:字符串格式,Hugging Face 模型库中的模型名称或本地路径。--dataset_name_or_paths:字符串格式(json dumped),Hugging Face 数据集或本地路径的数据集名称列表。--result_path:保存模型输出/产物的文件路径。--exp_version:用于实验版本控制。输出文件保存在{result_path}/{exp_version}/*
测试
使用训练好的模型、测试图像和真实值解析,你可以获得推理结果和准确率分数。
python test.py --dataset_name_or_path naver-clova-ix/cord-v2 --pretrained_model_name_or_path ./result/train_cord/test_experiment --save_path ./result/output.json
100%|█████████████| 100/100 [00:35<00:00, 2.80it/s]
Total number of samples: 100, Tree Edit Distance (TED) based accuracy score: 0.9129639764131697, F1 accuracy score: 0.8406020841373987
一些重要参数:
--dataset_name_or_path:字符串格式,Hugging Face 数据集或本地路径中的目标数据集名称。--pretrained_model_name_or_path:字符串格式,Hugging Face 模型库或本地路径中的模型名称。--save_path:用于保存预测结果和分数的文件路径。
如何引用
如果您发现本工作对您有用,请引用:
@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}
}
许可证
MIT 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.
