GOT-OCR2.0 Documentation

repository·main·Indexed 27 days ago

https://github.com/ucas-haoranwei/got-ocr2.0

A unified end-to-end model for advanced OCR tasks, supporting plain text, formatted text, and fine-grained OCR including bounding boxes and color. The repository provides tools for running OCR demos, post-training using DeepSpeed, fine-tuning via the ms-swift library, and evaluation using benchmarks like Fox and OneChart.

Tokens
1.5K
Snippets
4
Records
5
Agent score
43%

What's inside GOT-OCR2.0

  1. Train GOT-OCR2.0

    main

    This codebase supports post-training (stage-2/stage-3) using DeepSpeed.

    Important Requirements:

    • The <image> token in the conversations -> human -> value field is mandatory in your training samples.
    • You must update data information in GOT/utils/constant.py and the data_name in GOT/data/conversation_dataset_qwen.py (line 37) to match your dataset.

    Note: For stage-1 training, use the Vary-tiny-600k repository instead.

    deepspeed   /GOT-OCR-2.0-master/GOT/train/train_GOT.py \
     --deepspeed /GOT-OCR-2.0-master/zero_config/zero2.json    --model_name_or_path /GOT_weights/ \
     --use_im_start_end True   \
     --bf16 True   \
     --gradient_accumulation_steps 2    \
     --evaluation_strategy "no"   \
     --save_strategy "steps"  \
     --save_steps 200   \
     --save_total_limit 1   \
     --weight_decay 0.    \
     --warmup_ratio 0.001     \
     --lr_scheduler_type "cosine"    \
     --logging_steps 1    \
     --tf32 True     \
     --model_max_length 8192    \
     --gradient_checkpointing True   \
     --dataloader_num_workers 8    \
     --report_to none  \
     --per_device_train_batch_size 2  \
     --num_train_epochs 1  \
     --learning_rate 2e-5   \
     --datasets pdf-ocr+scence \
     --output_dir /your/output/path
  2. Run OCR Demos

    main

    Use GOT/demo/run_ocr_2.0.py to perform various OCR tasks.

    Available --type options:

    • ocr: Plain text OCR.
    • format: Formatted text OCR.
    • format/ocr: Fine-grained OCR with bounding boxes or color coding.

    Additional features:

    • --render: Renders formatted OCR results to an HTML file (found in /results/demo.html).
    • --box [x1,y1,x2,y2]: Specify bounding boxes for fine-grained OCR.
    • --color red/green/blue: Specify color for fine-grained OCR.
    • --multi-page: Use with run_ocr_2.0_crop.py to process multiple images in a directory.
  3. Install GOT-OCR2.0

    main

    Follow these steps to set up the GOT-OCR2.0 environment. The required environment is cuda11.8+torch2.0.1.

    1. Clone the repository and enter the GOT folder.
    2. Create a Conda environment with Python 3.10 and install the package in editable mode.
    3. Install flash-attn using ninja to ensure proper build isolation handling.
    # 1. Clone and navigate
    git clone https://github.com/Ucas-HaoranWei/GOT-OCR2.0.git
    cd 'the GOT folder'
    
    # 2. Install Package
    conda create -n got python=3.10 -y
    conda activate got
    pip install -e .
    
    # 3. Install Flash-Attention
    pip install ninja
    pip install flash-attn --no-build-isolation
  4. Fine-tune GOT-OCR2.0 with ms-swift

    main

    You can perform quick fine-tuning using the ms-swift library. By default, this performs SFT on the LLM and projector while freezing the vision encoder.

    Custom Dataset Format: Your .jsonl files should follow one of these formats:

    • Single image: {"query": "<image>55555", "response": "66666", "images": ["image_path"]}
    • Multi-image: {"query": "<image><image>eeeee", "response": "fffff", "history": [], "images": ["image_path1", "image_path2"]}
    • Text-only/History: {"query": "EEEEE", "response": "FFFFF", "history": [["query1", "response1"], ... ]}

    Commands:

    • Use --sft_type lora for LoRA fine-tuning.
    • Use --deepspeed default-zero2 for distributed training with DeepSpeed ZeRO2.
    # Setup ms-swift
    git clone https://github.com/modelscope/ms-swift.git
    cd ms-swift
    pip install -e .[llm]
    
    # Default LoRA fine-tune
    CUDA_VISIBLE_DEVICES=0 swift sft\
    --model_type got-ocr2 \
    --model_id_or_path stepfun-ai/GOT-OCR2_0 \
    --sft_type lora \
    --dataset latex-ocr-print#5000
    
    # LoRA fine-tune with DeepSpeed ZeRO2
    NPROC_PER_NODE=4 \
    CUDA_VISIBLE_DEVICES=0,1,2,3 swift sft \
    --model_type got-ocr2 \
    --model_id_or_path stepfun-ai/GOT-OCR2_0 \
    --sft_type lora \
    --dataset latex-ocr-print#5000 \
    --deepspeed default-zero2
    
    # Fine-tune with your own data
    # --dataset train.jsonl --val_dataset val.jsonl
  5. Evaluate GOT-OCR2.0

    main

    Run evaluations using the evaluate_GOT.py script located in GOT/eval/. The project uses benchmarks like Fox and OneChart.

    Usage: Pass the model path, ground truth file, image directory, and output path. If using 8 GPUs, set --num-chunks 8 to parallelize.

    python3 GOT/eval/evaluate_GOT.py --model-name /GOT_weights/ --gtfile_path xxxx.json --image_path  /image/path/ --out_path /data/eval_results/GOT_mathpix_test/ --num-chunks 8 --datatype OCR