dots.mocr Multimodal Model

repository·main·Indexed 43 days ago

https://github.com/rednote-hilab/dots.mocr

A multimodal model for advanced document parsing, structured graphics recognition, and general vision tasks. It features SOTA multilingual document parsing and the ability to convert charts, UI layouts, and scientific figures into SVG code via a specialized dots.mocr-svg variant. Capabilities include grounding, semantic understanding, and interactive dialogue, with official integration for deployment via vLLM (version 0.11.0+) and Hugging Face Transformers.

Tokens
3K
Snippets
7
Records
11
Agent score
7%

What's inside dots.mocr

  1. Overview of dots.mocr capabilities

    main

    dots.mocr is a multimodal model designed for comprehensive parsing. It recognizes diverse human scripts and structured graphical content. Its core capabilities include:

    • Grounding: Locating elements within an image.
    • Recognition: Identifying text and graphical components.
    • Semantic Understanding: Interpreting the meaning of content.
    • Interactive Dialogue: Engaging in conversation based on visual input.

    Key features include state-of-the-art (SOTA) multilingual document parsing and the ability to convert structured graphics (such as charts, UI layouts, and scientific figures) directly into SVG code. A specialized variant, dots.mocr-svg, is also available for optimized image-to-SVG parsing tasks.

  2. Parse structured graphics into SVG

    main

    A core capability of dots.mocr is the interpretation of visual languages (charts, graphics, chemical formulas, logos) by parsing them directly into SVG code.

    This allows for high-fidelity reconstruction of dense human knowledge encapsulated in graphics. The project also offers dots.mocr-svg specifically optimized for these tasks to complement the base 3B-parameter model.

  3. Install dots.mocr via Conda

    main

    To install dots.mocr locally, create a new Conda environment with Python 3.12, clone the repository, and install the package in editable mode.

    Note: If you encounter installation issues, it is recommended to use the official vLLM Docker image for an easier setup.

    conda create -n dots_mocr python=3.12
    conda activate dots_mocr
    
    git clone https://github.com/rednote-hilab/dots.mocr.git
    cd dots.mocr
    
    pip install -e .
  4. Download dots.mocr model weights

    main

    Use the provided utility script to download the model weights.

    Important: Use a directory name without periods (e.g., DotsMOCR instead of dots.mocr) for the model save path to avoid issues with Transformers integration.

    To download via standard method:

    python3 tools/download_model.py

    To download via ModelScope:

    python3 tools/download_model.py --type modelscope
  5. Deploy dots.mocr using vLLM

    main

    Dots OCR is officially integrated into vLLM (version 0.11.0+). You can deploy a model server using the vllm serve command. This is the highly recommended method for deployment and inference.

    To serve the standard dots.mocr model:

    CUDA_VISIBLE_DEVICES=0 vllm serve rednote-hilab/dots.mocr --tensor-parallel-size 1 --gpu-memory-utilization 0.9 --chat-template-content-format string --trust-remote-code

    To serve the dots.mocr-svg model:

    CUDA_VISIBLE_DEVICES=0 vllm serve rednote-hilab/dots.mocr-svg --tensor-parallel-size 1 --gpu-memory-utilization 0.9 --chat-template-content-format string --trust-remote-code
  6. Install dots.mocr

    main

    To set up dots.mocr, follow these installation steps. Note that you must ensure your CUDA version is compatible with the PyTorch installation. Refer to the official PyTorch getting started guide for specific version matching.

    1. Install the dots.mocr package.
    2. Install the specific PyTorch stack (version 2.7.0) with CUDA 12.8 support.
    3. (Optional) Install flash-attn to enable faster inference.
    # Install dots.mocr
    # (Command for dots.mocr installation depends on the package manager/source)
    
    # Install pytorch stack
    pip install torch==2.7.0 torchvision==0.22.0 torchaudio==2.7.0 --index-url https://download.pytorch.org/whl/cu128
    
    # Install flash-attn for faster inference
    pip install flash-attn==2.8.0.post2
  7. Run vLLM inference demos

    main

    Once the vLLM server is running, you can run various specialized parsing demos using the --prompt_mode flag.

    Available prompt modes include:

    • prompt_layout_all_en: Document parsing (layout and recognition).
    • prompt_web_parsing: Web page parsing.
    • prompt_scene_spotting: Scene spotting.
    • prompt_image_to_svg: Image parsing with SVG code (requires dots.mocr-svg).
    • prompt_general_qa: General Question Answering.
    # Document parsing
    python3 ./demo/demo_vllm.py --prompt_mode prompt_layout_all_en 
    
    # Web parsing 
    python3 ./demo/demo_vllm.py --prompt_mode prompt_web_parsing --image_path ./assets/showcase/origin/webpage_1.png
    
    # Scene spotting
    python3 ./demo/demo_vllm.py --prompt_mode prompt_scene_spotting --image_path ./assets/showcase/origin/scene_1.jpg
    
    # Image parsing with svg code
    python3 ./demo/demo_vllm_svg.py --prompt_mode prompt_image_to_svg 
    
    # General QA
    python3 ./demo/demo_vllm_general.py
  8. Perform inference using Huggingface Transformers

    main

    You can use the transformers library for inference. This method is slower than vLLM but useful for local development or environments without vLLM support.

    Note: When using the DotsMOCRParser class, you can pass use_hf=True to switch to Transformers-based inference.

    import torch
    from transformers import AutoModelForCausalLM, AutoProcessor, AutoTokenizer
    from qwen_vl_utils import process_vision_info
    from dots_mocr.utils import dict_promptmode_to_prompt
    
    model_path = "./weights/DotsMOCR"
    model = AutoModelForCausalLM.from_pretrained(
        model_path,
        attn_implementation="flash_attention_2",
        torch_dtype=torch.bfloat16,
        device_map="auto",
        trust_remote_code=True
    )
    processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
    
    image_path = "demo/demo_image1.jpg"
    prompt = """Please output the layout information from the PDF image..."""
    
    messages = [
            {
                "role": "user",
                "content": [
                    {
                        "type": "image",
                        "image": image_path
                    },
                    {"type": "text", "text": prompt}
                ]
            }
        ]
    
    # Preparation for inference
    text = processor.apply_chat_template(
        messages, 
        tokenize=False, 
        add_generation_prompt=True
    )
    image_inputs, video_inputs = process_vision_info(messages)
    inputs = processor(
        text=[text],
        images=image_inputs,
        videos=video_inputs,
        padding=True,
        return_tensors="pt",
    )
    
    inputs = inputs.to("cuda")
    
    # Inference
    generated_ids = model.generate(**inputs, max_new_tokens=24000)
    generated_ids_trimmed = [
        out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
    ]
    output_text = processor.batch_decode(
        generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
    )
    print(output_text)
  9. Parse documents with dots_mocr/parser.py

    main

    The parser.py script allows you to parse images or PDFs into structured data. This works both with a running vLLM server and via Transformers (by adding --use_hf true).

    Available Tasks:

    • Full Parsing (Layout + Recognition): Default behavior.
    • Layout Detection Only: Use --prompt prompt_layout_only_en.
    • Text Extraction Only (OCR): Use --prompt prompt_ocr (excludes Page-header and Page-footer).

    Output Files Generated:

    1. [filename].json: Structured layout data (bounding boxes, categories, text).
    2. [filename].md: Processed Markdown file from extracted text.
    3. [filename]_nohf.md: Markdown version excluding page headers/footers.
    4. [filename].jpg: Visualization of detected layout boxes on the original image.
    # Parse all layout info (detection and recognition)
    # Single image
    python3 dots_mocr/parser.py demo/demo_image1.jpg
    
    # Single PDF (use --num_thread for large PDFs)
    python3 dots_mocr/parser.py demo/demo_pdf1.pdf --num_thread 64
    
    # Layout detection only
    python3 dots_mocr/parser.py demo/demo_image1.jpg --prompt prompt_layout_only_en
    
    # Text only (OCR)
    python3 dots_mocr/parser.py demo/demo_image1.jpg --prompt prompt_ocr
    
    # Using Transformers instead of vLLM
    python3 dots_mocr/parser.py demo/demo_image1.jpg --use_hf true
  10. General Vision Task capabilities

    main

    Beyond document parsing, dots.mocr is evaluated on a variety of general vision tasks, including:

    • Descriptive & Reasoning: CharXiv_descriptive, CharXiv_reasoning, and OCR_Reasoning.
    • Visual Question Answering (VQA): infovqa, docvqa, and ChartQA.
    • Benchmarks: OCRBench, AI2D, CountBenchQA, and refcoco.
  11. Evaluate Document Parsing performance

    main

    The project provides benchmarks for document parsing capabilities across several metrics.

    Elo Score Comparison

    dots.mocr is evaluated against models like Gemini 3 Pro, GLM-OCR, and PaddleOCR-VL-1.5 using Elo scores across benchmarks like olmOCR-Bench, OmniDocBench (v1.5), and XDocParse. The evaluation is conducted using Gemini 3 Flash.

    olmOCR-bench

    dots.mocr performance is measured on olmOCR-bench across specific document characteristics:

    • ArXiv
    • Old scans math
    • Tables
    • Old scans
    • Headers & footers
    • Multi column
    • Long tiny text
    • Base
    • Overall

    Note: Page-header and Page-footer cells are deleted from the result markdown during evaluation.

    Other Benchmarks

    Performance is also tracked via:

    • OmniDocBench (v1.5): Measured using TextEdit↓ and Read OrderEdit↓ (lower is better).
    • pdf-parse-bench: Reproduced results for comparison.