UniMERNet

repository·main·Indexed 19 days ago

https://github.com/opendatalab/unimernet

A universal network for real-world mathematical expression recognition (MER) that converts images of printed, screen-captured, and handwritten math formulas into LaTeX code. Version 0.2.3 includes support for various model sizes (base, small, tiny), a Streamlit GUI, and Character Detection Matching (CDM), a vision-based evaluation metric that renders LaTeX to images for precise character-level matching.

Tokens
7.4K
Snippets
37
Records
44
Agent score
67%

What's inside unimernet

  1. What is Character Detection Matching (CDM)?

    main

    Character Detection Matching (CDM) is an evaluation metric designed for formula recognition. Unlike traditional text-based metrics like BLEU or Edit Distance, which are sensitive to LaTeX representation variations and training data distribution, CDM uses a vision-based approach.

    It works by:

    1. Rendering both the predicted LaTeX and the ground truth (GT) LaTeX into images.
    2. Using visual feature extraction and localization to perform precise character-level matching.
    3. Incorporating spatial position information to ensure objective and accurate evaluation.

    This method avoids the pitfalls where a model might get a high BLEU score despite incorrect recognition, or a low score despite correct recognition due to different LaTeX formatting.

  2. Download the UniMER Dataset

    main

    The UniMER dataset consists of two main parts:

    • UniMER-1M: A training set with over 1 million LaTeX-image pairs.
    • UniMER-Test: A benchmark test set with 23,757 samples categorized into SPE (Simple Printed), CPE (Complex Printed), SCE (Screen Capture), and HWE (Handwritten).

    Datasets can be downloaded from OpenDataLab (recommended for users in China) or Hugging Face. After downloading, extract them into the following directory structure:

    • ./data/UniMER-1M
    • ./data/UniMER-Test
  3. Test UniMERNet models

    main

    To evaluate the model performance:

    1. Configure Test Paths: Open the files in the configs/val folder and set the paths to your test datasets.
    2. Execute Testing: Run the testing script using the provided shell script.

    Note: The test.py script performs the evaluation. Ensure you have updated the test set paths in test.py to match your actual data locations.

    # After configuring paths in configs/val
    bash script/test.sh
  4. Run UniMERNet via GUI, CLI, or Jupyter

    main

    UniMERNet can be executed in three different ways depending on your needs:

    1. Streamlit GUI: Provides an interactive, user-friendly interface for real-time formula recognition and rendering. Run via the unimernet_gui command.
    2. Command Line Demo: Predicts LaTeX code from images using a script. Run via python demo.py.
    3. Jupyter Notebook: Recognizes and renders formulas within a notebook environment. Run via jupyter-lab ./demo.ipynb.
    # 1. Streamlit GUI
    unimernet_gui
    
    # 2. Command line demo
    python demo.py
    
    # 3. Jupyter Notebook demo
    jupyter-lab ./demo.ipynb
  5. Download UniMERNet model files

    main

    To use UniMERNet, you must download the model weights and configuration files. The files can be obtained from the Hugging Face repository. Ensure you have the following files in your local model directory:

    • README.md
    • config.json
    • preprocessor_config.json
    • pytorch_model.bin
    • tokenizer.json
    • tokenizer_config.json
    https://huggingface.co/wanderkid/unimernet/tree/main
  6. Deploy CDM using Docker

    main

    If local environment setup is difficult, you can use Docker to deploy CDM.

    1. Build the image: Use the provided Dockerfile.
    2. Run the container: Start an interactive bash session. Use the -v flag to map host directories to the container so that evaluation results are persisted on your host machine.
    # Build the docker image
    docker build -f DockerFile -t cdm:latest .
    
    # Start a container
    docker run -it cdm bash
    
    # Example with volume mapping to save results
    docker run -it -v /path/on/host:/path/in/container cdm bash
  7. Install CDM locally on Linux

    main

    CDM has complex dependencies. It is recommended to use a Linux system. You must install Node.js, ImageMagick (compiled from source to ensure version 7+), pdflatex (via texlive-full), and Python requirements.

    Step 1: Install Node.js

    wget https://registry.npmmirror.com/-/binary/node/latest-v16.x/node-v16.13.1-linux-x64.tar.gz
    
    tar -xvf node-v16.13.1-linux-x64.tar.gz
    
    mv node-v16.13.1-linux-x64/* /usr/local/nodejs/
    
    ln -s /usr/local/nodejs/bin/node /usr/local/bin
    
    ln -s /usr/local/nodejs/bin/npm /usr/local/bin
    
    node -v

    Step 2: Install ImageMagick (from source)

    git clone https://github.com/ImageMagick/ImageMagick.git ImageMagick-7.1.1
    
    cd ImageMagick-7.1.1
    
    ./configure
    
    make
    
    sudo make install
    
    sudo ldconfig /usr/local/lib
    
    convert --version

    Step 3: Install LaTeX PDF support

    apt-get update
    
    sudo apt-get install texlive-full

    Step 4: Install Python requirements

    pip install -r requirements.txt
    # Summary of installation steps
    # 1. Install Node.js
    # 2. Install ImageMagick from source
    # 3. Install texlive-full
    # 4. pip install -r requirements.txt
  8. Install UniMERNet

    main

    You can install UniMERNet using pip for general use or via editable install for development. It is recommended to use a clean Conda environment with Python 3.10.

    Setup Environment

    conda create -n unimernet python=3.10
    conda activate unimernet

    Installation Options

    Option 1: Standard Installation (Recommended for most users)

    pip install --upgrade unimernet
    pip install "unimernet[full]"

    Option 2: Developer Installation (Local editable mode)

    pip install -e ."[full]"
    pip install --upgrade unimernet
    pip install "unimernet[full]"
  9. Test UniMERNet model

    main

    To evaluate the UniMERNet model on a test set:

    1. Configure Test Path: Open the files in the configs/val directory and set the path to your test dataset.
    2. Execute Testing: Run the test script.
    bash script/test.sh

    Note: The test.py script uses the path specified in the configs/val folder. Ensure you update test.py with your actual path if necessary.

    bash script/test.sh
  10. Run UniMERNet for inference

    main

    UniMERNet provides several ways to perform mathematical expression recognition (converting images to LaTeX):

    1. Streamlit GUI: For an interactive, user-friendly experience with real-time rendering.
      unimernet_gui
       *Note: Requires `pip install "unimernet[full]"`.*
    
    2. **Command Line Demo**: Predict LaTeX code from images via a script.
       ```bash
    python demo.py
    1. Jupyter Notebook: For interactive recognition and rendering in a notebook environment.

    Run this in your terminal

    jupyter-lab ./demo.ipynb

    unimernet_gui
  11. Install dependencies and download MFD model weights

    main

    To set up the Mathematical Formula Detection (MFD) component, create a dedicated Conda environment with Python 3.10 and install the ultralytics package. You can download the model weights (weights.pt) using either ModelScope or HuggingFace.

    conda create -n mfd python=3.10
    conda activate mfd
    pip install ultralytics
    
    # download with modelscope
    cd MFD/
    wget -c https://www.modelscope.cn/models/wanderkid/PDF-Extract-Kit/resolve/master/models/MFD/weights.pt