Donut (Document Understanding Transformer)

repository·master·Indexed 26 days ago

https://github.com/clovaai/donut

An OCR-free end-to-end Transformer model for visual document understanding tasks, including classification, information extraction, and Document Visual Question Answering (DocVQA). The repository includes SynthDoG, a synthetic document generator for pre-training in English, Chinese, Japanese, and Korean. It provides pre-trained models such as donut-base and donut-proto, along with tools for training, testing, and running Gradio web demos.

Tokens
2.4K
Snippets
10
Records
20
Agent score
92%

What's inside Donut

  1. Install Donut from source

    master

    To install the official implementation from the repository, clone the repo and use conda to create a dedicated environment. This is useful if you want to modify the source or ensure specific dependency versions.

    git clone https://github.com/clovaai/donut.git
    cd donut/
    conda create -n donut_official python=3.7
    conda activate donut_official
    pip install .
  2. Test the trained Donut model

    master

    Use test.py to evaluate a trained model on a dataset to obtain inference results and accuracy scores (TED and F1).

    Arguments:

    • --dataset_name_or_path: Target dataset name (Hugging Face) or local path.
    • --pretrained_model_name_or_path: Path to the trained model weights.
    • --save_path: File path where predictions and scores will be saved.
    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
  3. Prepare dataset structure for Donut

    master

    Donut requires a specific directory structure for datasets. Each dataset must contain train, test, and validation directories, each containing a metadata.jsonl file and the corresponding image files.

    metadata.jsonl format: Each line in the .jsonl file must be a JSON object with:

    • file_name: The relative path to the image file.
    • ground_truth: A JSON-dumped string containing either gt_parse or gt_parses.

    Example Directory Structure:

    dataset_name
    ├── test
    │   ├── metadata.jsonl
    │   ├── image0.jpg
    │   └── image1.jpg
    ├── train
    │   ├── metadata.jsonl
    │   └── ...
    └── validation
        ├── metadata.jsonl
        └── ...
    >
    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}
                  .
                  .
  4. Train the Donut model

    master

    Run the train.py script to start training. You can specify a configuration file, a pretrained model (from Hugging Face or local), and the dataset path(s).

    Arguments:

    • --config: Path to the YAML config file for training.
    • --pretrained_model_name_or_path: Hugging Face model name or local path to a pretrained model.
    • --dataset_name_or_paths: A JSON-dumped list of dataset names (Hugging Face) or local paths.
    • --result_path: Directory where model outputs and artifacts will be saved.
    • --exp_version: Version string for the experiment; outputs are stored in {result_path}/{exp_version}/*.
    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"
  5. Generate synthetic documents with SynthDoG

    master

    Use the synthtiger command to invoke the SynthDoG generator. You need to provide a template script, the generator name (SynthDoG), and a configuration YAML file.

    synthtiger -o ./outputs/SynthDoG_en -c 50 -w 4 -v template.py SynthDoG config_en.yaml
  6. Generate ECJK language datasets with SynthDoG

    master

    SynthDoG supports English (en), Chinese (zh), Japanese (ja), and Korean (ko) through specific configuration files. Replace the placeholders with your desired paths and counts.

    # 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
  7. Run the Donut Gradio demo

    master

    You can launch a Gradio web interface to interact with the Donut model for tasks like Document Visual Question Answering (DocVQA) or other document processing tasks. The demo supports specifying the task type, the pretrained model path, and the network port/URL.

    CLI Arguments:

    • --task: The task name (e.g., docvqa, rvlcdip, cord). Defaults to docvqa.
    • --pretrained_path: The path to the pretrained model. Defaults to naver-clova-ix/donut-base-finetuned-docvqa.
    • --port: The port to host the Gradio interface on.
    • --url: The server name/URL to host the Gradio interface on.
    • --sample_img_path: An optional path to an image to use as an example in the UI.
  8. Format ground truth for Document Visual Question Answering (DocVQA)

    master

    For DocVQA, use the gt_parses field (a list of dictionaries) to support multiple answers. Each dictionary must contain a question and an answer key.

    Example: [{"question" : "what is the model name?", "answer" : "donut"}, {"question" : "what is the model name?", "answer" : "document understanding transformer"}]