Paddle2ONNX Documentation

repository·develop·Indexed 21 days ago

https://github.com/paddlepaddle/paddle2onnx

A tool to convert PaddlePaddle models into the ONNX format for deployment across inference engines such as TensorRT, OpenVINO, MNN, TNN, and NCNN. It includes a CLI for conversion and a suite of utilities for model manipulation, including shape inference, pruning, and node renaming for both Paddle and ONNX models. Supports various model zoos including PaddleClas, PaddleOCR, and PaddleSeg.

Tokens
7.5K
Snippets
21
Records
40
Agent score
75%

What's inside Paddle2ONNX

  1. Compile and Install Paddle2ONNX on Linux/Mac

    develop

    To compile Paddle2ONNX from source on Linux or macOS, ensure you have cmake >= 3.16.0 and protobuf == 21.12 installed. Follow these steps:

    1. Install Protobuf: Clone the protobuf repository, checkout version v21.12, build it with CMAKE_INSTALL_PREFIX pointing to a local directory, and add the resulting bin directory to your PATH.
    2. Install PaddlePaddle: Install the nightly CPU version using pip.
    3. Install Paddle2ONNX: Clone the repository, initialize submodules, set the PIP_EXTRA_INDEX_URL to the Paddle nightly index, and build the wheel using python -m build.

    If you are developing Paddle2ONNX, you can install it in editable mode using pip install -e ..

    # 1. Install Protobuf
    git clone https://github.com/protocolbuffers/protobuf.git
    cd protobuf
    git checkout v21.12
    git submodule update --init
    mkdir build_source && cd build_source
    cmake ../cmake -DCMAKE_INSTALL_PREFIX=`pwd`/installed_protobuf_lib -Dprotobuf_BUILD_SHARED_LIBS=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=14
    make -j
    make install
    export PATH=${PWD}/installed_protobuf_lib/bin:${PATH}
    
    # 2. Install PaddlePaddle
    python -m pip install --pre paddlepaddle -i https://www.paddlepaddle.org.cn/packages/nightly/cpu/
    
    # 3. Install Paddle2ONNX
    git clone https://github.com/PaddlePaddle/Paddle2ONNX.git
    cd Paddle2ONNX
    git submodule update --init
    export PIP_EXTRA_INDEX_URL="https://www.paddlepaddle.org.cn/packages/nightly/cpu/"
    python -m build
    pip install dist/*.whl
  2. Prune a Paddle model by specifying outputs

    develop

    If you only need a subset of a Paddle model, you can perform model pruning by specifying the required input and output names. Note that if an input is not required by any of the specified outputs, that input will also be pruned from the model.

    Use the prune_paddle_model.py script with the following arguments:

    • --model_dir: Directory containing the original model.
    • --model_filename: The .pdmodel file.
    • --params_filename: The .pdiparams file.
    • --input_names: Space-separated list of input node names.
    • --output_names: Space-separated list of output node names (supports multiple outputs).
    • --save_dir: Directory where the pruned model will be saved.
    python prune_paddle_model.py --model_dir original_paddle_model  \-
                                 --model_filename model.pdmodel \-
                                 --params_filename model.pdiparams \-
                                 --input_names input0 input1 \-
                                 --output_names output0 output1 \-
                                 --save_dir new_paddle
  3. Convert PaddlePaddle models to ONNX via CLI

    develop

    You can convert a PaddlePaddle model to ONNX format using the paddle2onnx command line interface.

    To perform a conversion, you must provide a deployment model consisting of two files:

    1. A model structure file (e.g., model_name.json)
    2. A model parameters file (e.g., model_name.pdiparams)

    Basic usage example:

    paddle2onnx --model_dir model_dir \
                --model_filename model.json \
                --params_filename model.pdiparams \
                --save_file model.onnx
  4. Prune an ONNX model to specific outputs

    develop

    To extract only a subset of a model (e.g., specific outputs and their preceding nodes), use the prune_onnx_model.py script. You can specify one or multiple output tensors using the --output_names flag.

    python prune_onnx_model.py --model model.onnx --output_names x y --save_file new_model.onnx
  5. Compile and Install Paddle2ONNX on Windows

    develop

    To compile Paddle2ONNX on Windows, you must have Visual Studio 2019 installed with the Desktop development with C++ workload.

    1. Prepare Environment: Open the x64 Native Tools Command Prompt for VS 2019.
    2. Install Protobuf: Clone protobuf (v21.12), build using the Visual Studio 16 2019 generator, and add the bin directory to your PATH.
    3. Install Paddle2ONNX: Clone the repository, initialize submodules, set PIP_EXTRA_INDEX_URL, install build dependencies (setuptools, wheel, auditwheel, etc.), and build the wheel.

    Note: When running the cmake command for Protobuf, ensure -DCMAKE_INSTALL_PREFIX points to your desired installation path.

    # 1. Install Protobuf
    git clone https://github.com/protocolbuffers/protobuf.git
    cd protobuf
    git checkout v21.12
    git submodule update --init --recursive
    mkdir build
    cd build
    cmake -G "Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX=%CD%\protobuf_install -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_BUILD_SHARED_LIBS=OFF -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_EXAMPLES=OFF ..
    cmake --build . --config Release --target install
    set PATH=%CD%\protobuf_install\bin;%PATH%
    
    # 2. Install Paddle2ONNX
    git clone https://github.com/PaddlePaddle/Paddle2ONNX.git
    cd Paddle2ONNX
    git submodule update --init
    set PIP_EXTRA_INDEX_URL=https://www.paddlepaddle.org.cn/packages/nightly/cpu/
    pip install setuptools wheel auditwheel auditwheel-symbols build
    python -m build
    pip install dist/*.whl
  6. Perform ONNX model shape inference

    develop

    If your ONNX model is missing shape information for intermediate nodes, you can use the onnx_infer_shape.py script to perform shape inference. This script is based on the onnxruntime symbolic shape inference tool.

    python onnx_infer_shape.py --input model.onnx --output new_model.onnx
  7. Modify Paddle model input shapes

    develop

    To support dynamic input shapes (e.g., replacing fixed dimensions with -1), use the infer_paddle_model_shape.py script. This allows you to re-export a model with updated input/output shapes, which can be verified using tools like Netron.

    Use the following command structure:

    • --model_path: Path to the existing inference model directory.
    • --save_path: Path where the updated model will be saved.
    • --input_shape_dict: A JSON-formatted string mapping input names to their new shapes (e.g., "{'x':[-1,3,-1,-1]}").
    python infer_paddle_model_shape.py --model_path ch_PP-OCRv2_det_infer/inference \-
                                        --save_path ch_PP-OCRv2_det_infer/new_inference  \-
                                        --input_shape_dict="{'x':[-1,3,-1,-1]}"
  8. Rename ONNX model nodes (including inputs and outputs)

    develop

    You can rename intermediate nodes, inputs, or outputs using the rename_onnx_model.py script.

    • --origin_names: A list of the original node names to be replaced.
    • --new_names: A list of the new names to assign.

    Note: The number of names provided in --origin_names must match the number of names provided in --new_names.

    python rename_onnx_model.py --model model.onnx --origin_names x y z --new_names x1 y1 z1 --save_file new_model.onnx