Commit 1fe1af27 authored by wanglch's avatar wanglch
Browse files

Initial commit

parent 4cfcf972
## Code of Conduct 🤝
Before diving in, please take a moment to review our Code of Conduct. It sets the tone for our community and emphasizes the importance of respect and inclusivity. [Read the Code of Conduct](LICENSE.md).
## Contribution Types 🦠🚀📚
### Bug Reports 🐞
If you encounter any bugs during your journey, don't fret! We have the Bug Busters ready to help. To report a bug, follow these steps:
1. Check if the bug has already been reported in [GitHub Issues](https://github.com/AI4Finance-Foundation/FinGPT/issues).
2. If it's a new bug, open a new issue with a concise description and provide detailed, step-by-step instructions to reproduce it.
### Feature Requests 💡
Do you have visionary ideas that could elevate FinGPT? Share them with us! When submitting a feature request, be sure to include:
1. A clear and vivid description of the feature you envision.
2. Discuss the impact and potential benefits.
### Documentation 📖
For those with a penchant for words and an eye for detail, consider contributing to our documentation. You can make the documentation more enlightening for everyone. 🧙📜
### Code Contributions 💻
Calling all AI heroes and wizards! You are the secret sauce behind the FinGPT project. To contribute code and save the financial world:
1. **Fork the Repository**: Click the "Fork" button on the top right of the repository's page. This creates your own copy of the project.
2. **Clone your Fork**: In your terminal, use the following command to clone your fork to your local machine:
```bash
git clone https://github.com/YourUsername/FinGPT.git
```
3. **Create a New Branch**: Make a new branch for your adventures. This helps keep the main codebase clean:
```bash
git checkout -b your-feature-branch
```
4. **Work Your Magic**: Implement your code or changes.
5. **Commit and Push**: Use these commands to commit your changes and push them to your fork:
```bash
git commit -m "Your commit message"
git push origin your-feature-branch
```
6. **Create a Pull Request**: Go to the original FinGPT repository and click "New Pull Request." Select your branch, write a description, and submit.
## Seeking Assistance ❓🙋‍♀️
If you find yourself stuck or have questions, remember that our support team is your sidekick. Don't hesitate to reach out. We are here to guide you through the process and provide any necessary assistance.
## Getting Started 🚀🚀
Are you ready to make a mark on the FinGPT project? Grab your cape and join us in our mission to make finance and AI even more incredible. Your contributions are the magic that fuels our journey.
🔗 [FinGPT GitHub Repository](https://github.com/AI4Finance-Foundation/FinGPT)
### May your contributions be as amazing as you are! 🌌🚀
\ No newline at end of file
This diff is collapsed.
<div align="center">
# Donut 🍩 : Document Understanding Transformer
[![Paper](https://img.shields.io/badge/Paper-arxiv.2111.15664-red)](https://arxiv.org/abs/2111.15664)
[![Conference](https://img.shields.io/badge/ECCV-2022-blue)](#how-to-cite)
[![Demo](https://img.shields.io/badge/Demo-Gradio-brightgreen)](#demo)
[![Demo](https://img.shields.io/badge/Demo-Colab-orange)](#demo)
[![PyPI](https://img.shields.io/pypi/v/donut-python?color=green&label=pip%20install%20donut-python)](https://pypi.org/project/donut-python)
[![Downloads](https://static.pepy.tech/personalized-badge/donut-python?period=total&units=international_system&left_color=grey&right_color=brightgreen&left_text=Downloads)](https://pepy.tech/project/donut-python)
Official Implementation of Donut and SynthDoG | [Paper](https://arxiv.org/abs/2111.15664) | [Slide](https://docs.google.com/presentation/d/1gv3A7t4xpwwNdpxV_yeHzEOMy-exJCAz6AlAI9O5fS8/edit?usp=sharing) | [Poster](https://docs.google.com/presentation/d/1m1f8BbAm5vxPcqynn_MbFfmQAlHQIR5G72-hQUFS2sk/edit?usp=sharing)
</div>
## Introduction
**Donut** 🍩, **Do**cume**n**t **u**nderstanding **t**ransformer, 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 present **SynthDoG** 🐶, **Synth**etic **Do**cument **G**enerator, that helps the model pre-training to be flexible on various languages and domains.
Our academic paper, which describes our method in detail and provides full experimental results and analyses, can be found here:<br>
> [**OCR-free Document Understanding Transformer**](https://arxiv.org/abs/2111.15664).<br>
> [Geewook Kim](https://geewook.kim), [Teakgyu Hong](https://dblp.org/pid/183/0952.html), [Moonbin Yim](https://github.com/moonbings), [JeongYeon Nam](https://github.com/long8v), [Jinyoung Park](https://github.com/jyp1111), [Jinyeong Yim](https://jinyeong.github.io), [Wonseok Hwang](https://scholar.google.com/citations?user=M13_WdcAAAAJ), [Sangdoo Yun](https://sangdooyun.github.io), [Dongyoon Han](https://dongyoonhan.github.io), [Seunghyun Park](https://scholar.google.com/citations?user=iowjmTwAAAAJ). In ECCV 2022.
<img width="946" alt="image" src="misc/overview.png">
## Pre-trained Models and Web Demos
Gradio web demos are available! [![Demo](https://img.shields.io/badge/Demo-Gradio-brightgreen)](#demo) [![Demo](https://img.shields.io/badge/Demo-Colab-orange)](#demo)
|:--:|
|![image](misc/screenshot_gradio_demos.png)|
- You can run the demo with `./app.py` file.
- Sample images are available at `./misc` and more receipt images are available at [CORD dataset link](https://huggingface.co/datasets/naver-clova-ix/cord-v2).
- Web demos are available from the links in the following table.
- Note: We have updated the Google Colab demo (as of June 15, 2023) to ensure its proper working.
|Task|Sec/Img|Score|Trained Model|<div id="demo">Demo</div>|
|---|---|---|---|---|
| [CORD](https://github.com/clovaai/cord) (Document Parsing) | 0.7 /<br> 0.7 /<br> 1.2 | 91.3 /<br> 91.1 /<br> 90.9 | [donut-base-finetuned-cord-v2](https://huggingface.co/naver-clova-ix/donut-base-finetuned-cord-v2/tree/official) (1280) /<br> [donut-base-finetuned-cord-v1](https://huggingface.co/naver-clova-ix/donut-base-finetuned-cord-v1/tree/official) (1280) /<br> [donut-base-finetuned-cord-v1-2560](https://huggingface.co/naver-clova-ix/donut-base-finetuned-cord-v1-2560/tree/official) | [gradio space web demo](https://huggingface.co/spaces/naver-clova-ix/donut-base-finetuned-cord-v2),<br>[google colab demo (updated at 23.06.15)](https://colab.research.google.com/drive/1NMSqoIZ_l39wyRD7yVjw2FIuU2aglzJi?usp=sharing) |
| [Train Ticket](https://github.com/beacandler/EATEN) (Document Parsing) | 0.6 | 98.7 | [donut-base-finetuned-zhtrainticket](https://huggingface.co/naver-clova-ix/donut-base-finetuned-zhtrainticket/tree/official) | [google colab demo (updated at 23.06.15)](https://colab.research.google.com/drive/1YJBjllahdqNktXaBlq5ugPh1BCm8OsxI?usp=sharing) |
| [RVL-CDIP](https://www.cs.cmu.edu/~aharley/rvl-cdip) (Document Classification) | 0.75 | 95.3 | [donut-base-finetuned-rvlcdip](https://huggingface.co/naver-clova-ix/donut-base-finetuned-rvlcdip/tree/official) | [gradio space web demo](https://huggingface.co/spaces/nielsr/donut-rvlcdip),<br>[google colab demo (updated at 23.06.15)](https://colab.research.google.com/drive/1iWOZHvao1W5xva53upcri5V6oaWT-P0O?usp=sharing) |
| [DocVQA Task1](https://rrc.cvc.uab.es/?ch=17) (Document VQA) | 0.78 | 67.5 | [donut-base-finetuned-docvqa](https://huggingface.co/naver-clova-ix/donut-base-finetuned-docvqa/tree/official) | [gradio space web demo](https://huggingface.co/spaces/nielsr/donut-docvqa),<br>[google colab demo (updated at 23.06.15)](https://colab.research.google.com/drive/1oKieslZCulFiquequ62eMGc-ZWgay4X3?usp=sharing) |
The links to the pre-trained backbones are here:
- [`donut-base`](https://huggingface.co/naver-clova-ix/donut-base/tree/official): 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`](https://huggingface.co/naver-clova-ix/donut-proto/tree/official): (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 see [our paper](#how-to-cite) for more details.
## SynthDoG datasets
![image](misc/sample_synthdog.png)
The links to the SynthDoG-generated datasets are here:
- [`synthdog-en`](https://huggingface.co/datasets/naver-clova-ix/synthdog-en): English, 0.5M.
- [`synthdog-zh`](https://huggingface.co/datasets/naver-clova-ix/synthdog-zh): Chinese, 0.5M.
- [`synthdog-ja`](https://huggingface.co/datasets/naver-clova-ix/synthdog-ja): Japanese, 0.5M.
- [`synthdog-ko`](https://huggingface.co/datasets/naver-clova-ix/synthdog-ko): Korean, 0.5M.
To generate synthetic datasets with our SynthDoG, please see `./synthdog/README.md` and [our paper](#how-to-cite) for details.
## Updates
**_2023-06-15_** We have updated all Google Colab demos to ensure its proper working.<br>
**_2022-11-14_** New version 1.0.9 is released (`pip install donut-python --upgrade`). See [1.0.9 Release Notes](https://github.com/clovaai/donut/releases/tag/1.0.9).<br>
**_2022-08-12_** Donut 🍩 is also available at [huggingface/transformers 🤗](https://huggingface.co/docs/transformers/main/en/model_doc/donut) (contributed by [@NielsRogge](https://github.com/NielsRogge)). `donut-python` loads the pre-trained weights from the `official` branch of the model repositories. See [1.0.5 Release Notes](https://github.com/clovaai/donut/releases/tag/1.0.5).<br>
**_2022-08-05_** A well-executed hands-on tutorial on donut 🍩 is published at [Towards Data Science](https://towardsdatascience.com/ocr-free-document-understanding-with-donut-1acfbdf099be) (written by [@estaudere](https://github.com/estaudere)).<br>
**_2022-07-20_** First Commit, We release our code, model weights, synthetic data and generator.
## Software installation
[![PyPI](https://img.shields.io/pypi/v/donut-python?color=green&label=pip%20install%20donut-python)](https://pypi.org/project/donut-python)
[![Downloads](https://static.pepy.tech/personalized-badge/donut-python?period=total&units=international_system&left_color=grey&right_color=brightgreen&left_text=Downloads)](https://pepy.tech/project/donut-python)
```bash
pip install donut-python
```
or clone this repository and install the dependencies:
```bash
git clone https://github.com/clovaai/donut.git
cd donut/
conda create -n donut_official python=3.7
conda activate donut_official
pip install .
```
We tested [donut-python](https://pypi.org/project/donut-python/1.0.1) == 1.0.1 with:
- [torch](https://github.com/pytorch/pytorch) == 1.11.0+cu113
- [torchvision](https://github.com/pytorch/vision) == 0.12.0+cu113
- [pytorch-lightning](https://github.com/Lightning-AI/lightning) == 1.6.4
- [transformers](https://github.com/huggingface/transformers) == 4.11.3
- [timm](https://github.com/rwightman/pytorch-image-models) == 0.5.4
**Note**: From several reported issues, we have noticed increased challenges in configuring the testing environment for `donut-python` due to recent updates in key dependency libraries. While we are actively working on a solution, we have updated the Google Colab demo (as of June 15, 2023) to ensure its proper working. For assistance, we encourage you to refer to the following demo links: [CORD Colab Demo](https://colab.research.google.com/drive/1NMSqoIZ_l39wyRD7yVjw2FIuU2aglzJi?usp=sharing), [Train Ticket Colab Demo](https://colab.research.google.com/drive/1YJBjllahdqNktXaBlq5ugPh1BCm8OsxI?usp=sharing), [RVL-CDIP Colab Demo](https://colab.research.google.com/drive/1iWOZHvao1W5xva53upcri5V6oaWT-P0O?usp=sharing), [DocVQA Colab Demo](https://colab.research.google.com/drive/1oKieslZCulFiquequ62eMGc-ZWgay4X3?usp=sharing).
## Getting Started
### Data
This repository assumes the following structure of dataset:
```bash
> 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} ... }"}
.
.
```
- The structure of `metadata.jsonl` file is in [JSON Lines text format](https://jsonlines.org), i.e., `.jsonl`. Each line consists of
- `file_name` : relative path to the image file.
- `ground_truth` : string format (json dumped), the dictionary contains either `gt_parse` or `gt_parses`. Other fields (metadata) can be added to the dictionary but will not be used.
- `donut` interprets all tasks as a JSON prediction problem. As a result, all `donut` model training share a same pipeline. For training and inference, the only thing to do is preparing `gt_parse` or `gt_parses` for the task in format described below.
#### For Document Classification
The `gt_parse` follows the format of `{"class" : {class_name}}`, for example, `{"class" : "scientific_report"}` or `{"class" : "presentation"}`.
- Google colab demo is available [here](https://colab.research.google.com/drive/1xUDmLqlthx8A8rWKLMSLThZ7oeRJkDuU?usp=sharing).
- Gradio web demo is available [here](https://huggingface.co/spaces/nielsr/donut-rvlcdip).
#### For Document Information Extraction
The `gt_parse` is 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 at [CORD dataset](https://huggingface.co/datasets/naver-clova-ix/cord-v2).
- Google colab demo is available [here](https://colab.research.google.com/drive/1o07hty-3OQTvGnc_7lgQFLvvKQuLjqiw?usp=sharing).
- Gradio web demo is available [here](https://huggingface.co/spaces/naver-clova-ix/donut-base-finetuned-cord-v2).
#### For Document Visual Question Answering
The `gt_parses` follows 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, hence `gt_parses` should be a list of dictionary that contains a pair of question and answer.
- Google colab demo is available [here](https://colab.research.google.com/drive/1Z4WG8Wunj3HE0CERjt608ALSgSzRC9ig?usp=sharing).
- Gradio web demo is available [here](https://huggingface.co/spaces/nielsr/donut-docvqa).
#### For (Pseudo) Text Reading Task
The `gt_parse` looks like `{"text_sequence" : "word1 word2 word3 ... "}`
- This task is also a pre-training task of Donut model.
- You can use our **SynthDoG** 🐶 to generate synthetic images for the text reading task with proper `gt_parse`. See `./synthdog/README.md` for details.
### Training
This is the configuration of Donut model training on [CORD](https://github.com/clovaai/cord) dataset used in our experiment.
We ran this with a single NVIDIA A100 GPU.
```bash
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]
```
Some important arguments:
- `--config` : config file path for model training.
- `--pretrained_model_name_or_path` : string format, model name in Hugging Face modelhub or local path.
- `--dataset_name_or_paths` : string format (json dumped), list of dataset names in Hugging Face 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}/*`
### Test
With the trained model, test images and ground truth parses, you can get inference results and accuracy scores.
```bash
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
```
Some important arguments:
- `--dataset_name_or_path` : string format, the target dataset name in Hugging Face datasets or local path.
- `--pretrained_model_name_or_path` : string format, the model name in Hugging Face modelhub or local path.
- `--save_path`: file path to save predictions and scores.
## How to Cite
If you find this work useful to you, please cite:
```bibtex
@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}
}
```
## License
```
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.
```
FROM image.sourcefind.cn:5000/gpu/admin/base/pytorch:pytorch1.13-py3.8-cuda11.8
ENV DEBIAN_FRONTEND=noninteractive
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pytorch-lightning==1.6.4
transformers==4.25.1
timm==0.5.4
gradio==3.50.0
donut-python
\ No newline at end of file
import argparse
import gradio as gr
import torch
from PIL import Image
from donut import DonutModel
def demo_process_vqa(input_img, question):
global pretrained_model, task_prompt, task_name
input_img = Image.fromarray(input_img)
user_prompt = task_prompt.replace("{user_input}", question)
output = pretrained_model.inference(input_img, prompt=user_prompt)["predictions"][0]
return output
def demo_process(input_img):
global pretrained_model, task_prompt, task_name
input_img = Image.fromarray(input_img)
output = pretrained_model.inference(image=input_img, prompt=task_prompt)["predictions"][0]
return output
parser = argparse.ArgumentParser()
parser.add_argument("--task", type=str, default="cord-v2")
parser.add_argument("--pretrained_path", type=str, default="/home/wanglch/projects/donut/naver-clova-ix/donut-base-finetuned-cord-v2")
args, left_argv = parser.parse_known_args()
task_name = args.task
if "docvqa" == task_name:
task_prompt = "<s_docvqa><s_question>{user_input}</s_question><s_answer>"
else: # rvlcdip, cord, ...
task_prompt = f"<s_{task_name}>"
pretrained_model = DonutModel.from_pretrained(args.pretrained_path, trust_remote_code=True)
if torch.cuda.is_available():
pretrained_model.half()
device = torch.device("cuda")
pretrained_model.to(device)
else:
pretrained_model.encoder.to(torch.bfloat16)
pretrained_model.eval()
demo = gr.Interface(
fn=demo_process_vqa if task_name == "docvqa" else demo_process,
inputs=["image", "text"] if task_name == "docvqa" else "image",
outputs="json",
title=f"Donut 🍩 demonstration for `{task_name}` task",
)
demo.launch(debug=True)
\ No newline at end of file
import argparse
import gradio as gr
import torch
from PIL import Image
from donut import DonutModel
def demo_process_vqa(input_img, question):
global pretrained_model, task_prompt, task_name
input_img = Image.fromarray(input_img)
user_prompt = task_prompt.replace("{user_input}", question)
output = pretrained_model.inference(input_img, prompt=user_prompt)["predictions"][0]
return output
def demo_process(input_img):
global pretrained_model, task_prompt, task_name
input_img = Image.fromarray(input_img)
output = pretrained_model.inference(image=input_img, prompt=task_prompt)["predictions"][0]
return output
parser = argparse.ArgumentParser()
parser.add_argument("--task", type=str, default="zhtrainticket")
parser.add_argument("--pretrained_path", type=str, default="/home/wanglch/projects/donut/naver-clova-ix/donut-base-finetuned-zhtrainticket")
args, left_argv = parser.parse_known_args()
task_name = args.task
if "docvqa" == task_name:
task_prompt = "<s_docvqa><s_question>{user_input}</s_question><s_answer>"
else: # rvlcdip, cord, ...
task_prompt = f"<s_{task_name}>"
pretrained_model = DonutModel.from_pretrained(args.pretrained_path)
if torch.cuda.is_available():
pretrained_model.half()
device = torch.device("cuda")
pretrained_model.to(device)
else:
pretrained_model.encoder.to(torch.bfloat16)
pretrained_model.eval()
demo = gr.Interface(
fn=demo_process_vqa if task_name == "docvqa" else demo_process,
inputs=["image", "text"] if task_name == "docvqa" else "image",
outputs="json",
title=f"Donut 🍩 demonstration for `{task_name}` task",
)
demo.launch()
\ No newline at end of file
# 模型唯一标识
modelCode = 646
# 模型名称
modelName=vary_pytorch
# 模型描述
modelDescription=多模态OCR大模型
# 应用场景
appScenario=推理,OCR,金融,教育,政府,科研,交通,广媒
# 框架类型
frameType=pytorch
CUDA_VISIBLE_DEVICES=5,6 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"]' --result_path "/home/wanglch/projects/saves/donut/cord/train/multi_dtk" --exp_version "test_experiment"
\ No newline at end of file
pytorch-lightning==1.6.4
transformers==4.25.1
timm==0.5.4
gradio==3.50.0
donut-python
\ No newline at end of file
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"]' --result_path "/home/wanglch/projects/saves/donut/cord/train/single_dtk" --exp_version "test_experiment"
\ No newline at end of file
# SynthDoG 🐶: Synthetic Document Generator
SynthDoG is synthetic document generator for visual document understanding (VDU).
![image](../misc/sample_synthdog.png)
## Prerequisites
- python>=3.6
- [synthtiger](https://github.com/clovaai/synthtiger) (`pip install synthtiger`)
## Usage
```bash
# Set environment variable (for macOS)
$ export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
synthtiger -o ./outputs/SynthDoG_en -c 50 -w 4 -v template.py SynthDoG config_en.yaml
{'config': 'config_en.yaml',
'count': 50,
'name': 'SynthDoG',
'output': './outputs/SynthDoG_en',
'script': 'template.py',
'verbose': True,
'worker': 4}
{'aspect_ratio': [1, 2],
.
.
'quality': [50, 95],
'short_size': [720, 1024]}
Generated 1 data (task 3)
Generated 2 data (task 0)
Generated 3 data (task 1)
.
.
Generated 49 data (task 48)
Generated 50 data (task 49)
46.32 seconds elapsed
```
Some important arguments:
- `-o` : directory path to save data.
- `-c` : number of data to generate.
- `-w` : number of workers.
- `-s` : random seed.
- `-v` : print error messages.
To generate ECJK samples:
```bash
# english
synthtiger -o {dataset_path} -c {num_of_data} -w {num_of_workers} -v template.py SynthDoG config_en.yaml
# chinese
synthtiger -o {dataset_path} -c {num_of_data} -w {num_of_workers} -v template.py SynthDoG config_zh.yaml
# japanese
synthtiger -o {dataset_path} -c {num_of_data} -w {num_of_workers} -v template.py SynthDoG config_ja.yaml
# korean
synthtiger -o {dataset_path} -c {num_of_data} -w {num_of_workers} -v template.py SynthDoG config_ko.yaml
```
quality: [50, 95]
landscape: 0.5
short_size: [720, 1024]
aspect_ratio: [1, 2]
background:
image:
paths: [resources/background]
weights: [1]
effect:
args:
# gaussian blur
- prob: 1
args:
sigma: [0, 10]
document:
fullscreen: 0.5
landscape: 0.5
short_size: [480, 1024]
aspect_ratio: [1, 2]
paper:
image:
paths: [resources/paper]
weights: [1]
alpha: [0, 0.2]
grayscale: 1
crop: 1
content:
margin: [0, 0.1]
text:
path: resources/corpus/enwiki.txt
font:
paths: [resources/font/en]
weights: [1]
bold: 0
layout:
text_scale: [0.0334, 0.1]
max_row: 10
max_col: 3
fill: [0.5, 1]
full: 0.1
align: [left, right, center]
stack_spacing: [0.0334, 0.0334]
stack_fill: [0.5, 1]
stack_full: 0.1
textbox:
fill: [0.5, 1]
textbox_color:
prob: 0.2
args:
gray: [0, 64]
colorize: 1
content_color:
prob: 0.2
args:
gray: [0, 64]
colorize: 1
effect:
args:
# elastic distortion
- prob: 1
args:
alpha: [0, 1]
sigma: [0, 0.5]
# gaussian noise
- prob: 1
args:
scale: [0, 8]
per_channel: 0
# perspective
- prob: 1
args:
weights: [750, 50, 50, 25, 25, 25, 25, 50]
args:
- percents: [[0.75, 1], [0.75, 1], [0.75, 1], [0.75, 1]]
- percents: [[0.75, 1], [1, 1], [0.75, 1], [1, 1]]
- percents: [[1, 1], [0.75, 1], [1, 1], [0.75, 1]]
- percents: [[0.75, 1], [1, 1], [1, 1], [1, 1]]
- percents: [[1, 1], [0.75, 1], [1, 1], [1, 1]]
- percents: [[1, 1], [1, 1], [0.75, 1], [1, 1]]
- percents: [[1, 1], [1, 1], [1, 1], [0.75, 1]]
- percents: [[1, 1], [1, 1], [1, 1], [1, 1]]
effect:
args:
# color
- prob: 0.2
args:
rgb: [[0, 255], [0, 255], [0, 255]]
alpha: [0, 0.2]
# shadow
- prob: 1
args:
intensity: [0, 160]
amount: [0, 1]
smoothing: [0.5, 1]
bidirectional: 0
# contrast
- prob: 1
args:
alpha: [1, 1.5]
# brightness
- prob: 1
args:
beta: [-48, 0]
# motion blur
- prob: 0.5
args:
k: [3, 5]
angle: [0, 360]
# gaussian blur
- prob: 1
args:
sigma: [0, 1.5]
quality: [50, 95]
landscape: 0.5
short_size: [720, 1024]
aspect_ratio: [1, 2]
background:
image:
paths: [resources/background]
weights: [1]
effect:
args:
# gaussian blur
- prob: 1
args:
sigma: [0, 10]
document:
fullscreen: 0.5
landscape: 0.5
short_size: [480, 1024]
aspect_ratio: [1, 2]
paper:
image:
paths: [resources/paper]
weights: [1]
alpha: [0, 0.2]
grayscale: 1
crop: 1
content:
margin: [0, 0.1]
text:
path: resources/corpus/jawiki.txt
font:
paths: [resources/font/ja]
weights: [1]
bold: 0
layout:
text_scale: [0.0334, 0.1]
max_row: 10
max_col: 3
fill: [0.5, 1]
full: 0.1
align: [left, right, center]
stack_spacing: [0.0334, 0.0334]
stack_fill: [0.5, 1]
stack_full: 0.1
textbox:
fill: [0.5, 1]
textbox_color:
prob: 0.2
args:
gray: [0, 64]
colorize: 1
content_color:
prob: 0.2
args:
gray: [0, 64]
colorize: 1
effect:
args:
# elastic distortion
- prob: 1
args:
alpha: [0, 1]
sigma: [0, 0.5]
# gaussian noise
- prob: 1
args:
scale: [0, 8]
per_channel: 0
# perspective
- prob: 1
args:
weights: [750, 50, 50, 25, 25, 25, 25, 50]
args:
- percents: [[0.75, 1], [0.75, 1], [0.75, 1], [0.75, 1]]
- percents: [[0.75, 1], [1, 1], [0.75, 1], [1, 1]]
- percents: [[1, 1], [0.75, 1], [1, 1], [0.75, 1]]
- percents: [[0.75, 1], [1, 1], [1, 1], [1, 1]]
- percents: [[1, 1], [0.75, 1], [1, 1], [1, 1]]
- percents: [[1, 1], [1, 1], [0.75, 1], [1, 1]]
- percents: [[1, 1], [1, 1], [1, 1], [0.75, 1]]
- percents: [[1, 1], [1, 1], [1, 1], [1, 1]]
effect:
args:
# color
- prob: 0.2
args:
rgb: [[0, 255], [0, 255], [0, 255]]
alpha: [0, 0.2]
# shadow
- prob: 1
args:
intensity: [0, 160]
amount: [0, 1]
smoothing: [0.5, 1]
bidirectional: 0
# contrast
- prob: 1
args:
alpha: [1, 1.5]
# brightness
- prob: 1
args:
beta: [-48, 0]
# motion blur
- prob: 0.5
args:
k: [3, 5]
angle: [0, 360]
# gaussian blur
- prob: 1
args:
sigma: [0, 1.5]
quality: [50, 95]
landscape: 0.5
short_size: [720, 1024]
aspect_ratio: [1, 2]
background:
image:
paths: [resources/background]
weights: [1]
effect:
args:
# gaussian blur
- prob: 1
args:
sigma: [0, 10]
document:
fullscreen: 0.5
landscape: 0.5
short_size: [480, 1024]
aspect_ratio: [1, 2]
paper:
image:
paths: [resources/paper]
weights: [1]
alpha: [0, 0.2]
grayscale: 1
crop: 1
content:
margin: [0, 0.1]
text:
path: resources/corpus/kowiki.txt
font:
paths: [resources/font/ko]
weights: [1]
bold: 0
layout:
text_scale: [0.0334, 0.1]
max_row: 10
max_col: 3
fill: [0.5, 1]
full: 0.1
align: [left, right, center]
stack_spacing: [0.0334, 0.0334]
stack_fill: [0.5, 1]
stack_full: 0.1
textbox:
fill: [0.5, 1]
textbox_color:
prob: 0.2
args:
gray: [0, 64]
colorize: 1
content_color:
prob: 0.2
args:
gray: [0, 64]
colorize: 1
effect:
args:
# elastic distortion
- prob: 1
args:
alpha: [0, 1]
sigma: [0, 0.5]
# gaussian noise
- prob: 1
args:
scale: [0, 8]
per_channel: 0
# perspective
- prob: 1
args:
weights: [750, 50, 50, 25, 25, 25, 25, 50]
args:
- percents: [[0.75, 1], [0.75, 1], [0.75, 1], [0.75, 1]]
- percents: [[0.75, 1], [1, 1], [0.75, 1], [1, 1]]
- percents: [[1, 1], [0.75, 1], [1, 1], [0.75, 1]]
- percents: [[0.75, 1], [1, 1], [1, 1], [1, 1]]
- percents: [[1, 1], [0.75, 1], [1, 1], [1, 1]]
- percents: [[1, 1], [1, 1], [0.75, 1], [1, 1]]
- percents: [[1, 1], [1, 1], [1, 1], [0.75, 1]]
- percents: [[1, 1], [1, 1], [1, 1], [1, 1]]
effect:
args:
# color
- prob: 0.2
args:
rgb: [[0, 255], [0, 255], [0, 255]]
alpha: [0, 0.2]
# shadow
- prob: 1
args:
intensity: [0, 160]
amount: [0, 1]
smoothing: [0.5, 1]
bidirectional: 0
# contrast
- prob: 1
args:
alpha: [1, 1.5]
# brightness
- prob: 1
args:
beta: [-48, 0]
# motion blur
- prob: 0.5
args:
k: [3, 5]
angle: [0, 360]
# gaussian blur
- prob: 1
args:
sigma: [0, 1.5]
quality: [50, 95]
landscape: 0.5
short_size: [720, 1024]
aspect_ratio: [1, 2]
background:
image:
paths: [resources/background]
weights: [1]
effect:
args:
# gaussian blur
- prob: 1
args:
sigma: [0, 10]
document:
fullscreen: 0.5
landscape: 0.5
short_size: [480, 1024]
aspect_ratio: [1, 2]
paper:
image:
paths: [resources/paper]
weights: [1]
alpha: [0, 0.2]
grayscale: 1
crop: 1
content:
margin: [0, 0.1]
text:
path: resources/corpus/zhwiki.txt
font:
paths: [resources/font/zh]
weights: [1]
bold: 0
layout:
text_scale: [0.0334, 0.1]
max_row: 10
max_col: 3
fill: [0.5, 1]
full: 0.1
align: [left, right, center]
stack_spacing: [0.0334, 0.0334]
stack_fill: [0.5, 1]
stack_full: 0.1
textbox:
fill: [0.5, 1]
textbox_color:
prob: 0.2
args:
gray: [0, 64]
colorize: 1
content_color:
prob: 0.2
args:
gray: [0, 64]
colorize: 1
effect:
args:
# elastic distortion
- prob: 1
args:
alpha: [0, 1]
sigma: [0, 0.5]
# gaussian noise
- prob: 1
args:
scale: [0, 8]
per_channel: 0
# perspective
- prob: 1
args:
weights: [750, 50, 50, 25, 25, 25, 25, 50]
args:
- percents: [[0.75, 1], [0.75, 1], [0.75, 1], [0.75, 1]]
- percents: [[0.75, 1], [1, 1], [0.75, 1], [1, 1]]
- percents: [[1, 1], [0.75, 1], [1, 1], [0.75, 1]]
- percents: [[0.75, 1], [1, 1], [1, 1], [1, 1]]
- percents: [[1, 1], [0.75, 1], [1, 1], [1, 1]]
- percents: [[1, 1], [1, 1], [0.75, 1], [1, 1]]
- percents: [[1, 1], [1, 1], [1, 1], [0.75, 1]]
- percents: [[1, 1], [1, 1], [1, 1], [1, 1]]
effect:
args:
# color
- prob: 0.2
args:
rgb: [[0, 255], [0, 255], [0, 255]]
alpha: [0, 0.2]
# shadow
- prob: 1
args:
intensity: [0, 160]
amount: [0, 1]
smoothing: [0.5, 1]
bidirectional: 0
# contrast
- prob: 1
args:
alpha: [1, 1.5]
# brightness
- prob: 1
args:
beta: [-48, 0]
# motion blur
- prob: 0.5
args:
k: [3, 5]
angle: [0, 360]
# gaussian blur
- prob: 1
args:
sigma: [0, 1.5]
"""
Donut
Copyright (c) 2022-present NAVER Corp.
MIT License
"""
from elements.background import Background
from elements.content import Content
from elements.document import Document
from elements.paper import Paper
from elements.textbox import TextBox
__all__ = ["Background", "Content", "Document", "Paper", "TextBox"]
"""
Donut
Copyright (c) 2022-present NAVER Corp.
MIT License
"""
from synthtiger import components, layers
class Background:
def __init__(self, config):
self.image = components.BaseTexture(**config.get("image", {}))
self.effect = components.Iterator(
[
components.Switch(components.GaussianBlur()),
],
**config.get("effect", {})
)
def generate(self, size):
bg_layer = layers.RectLayer(size, (255, 255, 255, 255))
self.image.apply([bg_layer])
self.effect.apply([bg_layer])
return bg_layer
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment