### Install Protobuf via Package Manager (Linux/Mac) Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/docs/en/compile_local.md Provides alternative methods for installing protobuf using system package managers. For Linux, it uses `apt` to install `protobuf-compiler`. For macOS, it uses `brew` to install the protobuf package. These are simpler installation methods if a specific version is not strictly required. ```bash # Install Protobuf via apt (Linux): sudo apt install protobuf-compiler ``` ```bash # Install Protobuf via homebrew (Mac): brew install protobuf ``` -------------------------------- ### Install PaddlePaddle Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/docs/en/compile_local.md Installs the PaddlePaddle deep learning framework using pip. It specifically installs a pre-release version from a nightly build repository for CPU. This is a prerequisite for installing Paddle2ONNX. ```bash python -m pip install --pre paddlepaddle -i https://www.paddlepaddle.org.cn/packages/nightly/cpu/ ``` -------------------------------- ### Install Paddle2ONNX (Windows) Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/docs/en/compile_local.md Clones the Paddle2ONNX repository, updates submodules, sets the `PIP_EXTRA_INDEX_URL` environment variable for nightly builds, installs build tools like `setuptools`, `wheel`, and `auditwheel`, builds the package, and then installs the resulting wheel file. This process installs Paddle2ONNX from source on Windows. ```bash 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 ``` -------------------------------- ### Install Protobuf from Source (Linux/Mac) Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/docs/en/compile_local.md Clones the protobuf repository, checks out a specific version (v21.12), configures the build with cmake, compiles, and installs protobuf. It also sets the PATH environment variable to include the protobuf binaries. This method ensures a specific version is used for compatibility. ```bash 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 # set the library to environment export PATH=${PWD}/installed_protobuf_lib/bin:${PATH} ``` -------------------------------- ### Install Protobuf from Source (Windows) Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/docs/en/compile_local.md Clones the protobuf repository, checks out a specific version (v21.12), updates submodules, configures the build using cmake with Visual Studio 2019 generator, and then builds and installs protobuf. It also sets the PATH environment variable for the installed binaries. ```bash 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 the library to environment set PATH=%CD%\protobuf_install\bin;%PATH% ``` -------------------------------- ### Paddle2ONNX Installation (CMake) Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/CMakeLists.txt This CMake script handles the installation of Paddle2ONNX. It installs the shared or static library to the 'lib' directory and header files to 'include/paddle2onnx'. The installation logic is platform-dependent, with specific handling for Windows. ```cmake if(WIN32) install( TARGETS paddle2onnx LIBRARY DESTINATION lib ARCHIVE DESTINATION lib RUNTIME DESTINATION lib) else() if(WITH_STATIC) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpaddle2onnx_static.a DESTINATION lib) else() install(TARGETS paddle2onnx LIBRARY DESTINATION lib) endif() endif() install(FILES ${PROJECT_SOURCE_DIR}/paddle2onnx/converter.h ${PROJECT_SOURCE_DIR}/paddle2onnx/mappers_registry.h DESTINATION include/paddle2onnx) ``` -------------------------------- ### Install Paddle2ONNX (Linux/Mac) Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/docs/en/compile_local.md Clones the Paddle2ONNX repository, updates submodules, sets an environment variable for nightly builds, builds the package using `python -m build`, and then installs the built wheel file. This process installs Paddle2ONNX from source. ```bash 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 ``` -------------------------------- ### Install Paddle2ONNX Locally (Editable Mode) Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/docs/en/compile_local.md Installs Paddle2ONNX in editable mode using `pip install -e .`. This is useful for developers who are actively working on the Paddle2ONNX project and want changes to be reflected immediately without reinstalling. ```bash pip install -e . ``` -------------------------------- ### Optimize ONNX models with onnxslim Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/README_en.md Installs and utilizes the onnxslim tool to optimize exported ONNX models for better inference performance. This process takes an existing ONNX file and produces a slimmed version. ```bash pip install onnxslim onnxslim model.onnx slim.onnx ``` -------------------------------- ### Install Paddle2ONNX via pip Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/README_en.md Installs the Paddle2ONNX package using the Python package manager. This is the standard method for users who do not require source code modifications. ```bash pip install paddle2onnx ``` -------------------------------- ### Shared Library Build and Dependency Setup (CMake) Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/CMakeLists.txt This CMake code configures the build for a shared Paddle2ONNX library. It adds subdirectories for pybind11 and glog, finds the Python site-packages directory to locate Paddle libraries, and sets up include directories. It also includes platform-specific compile and link flags. ```cmake # add pybind11 add_subdirectory(third_party/pybind11) # add glog if(MSVC) set(WITH_GFLAGS OFF CACHE BOOL "Disable gflags support in glog") endif() add_subdirectory(third_party/glog) # find Python site-packages dir set(PYTHON_SITE_PACKAGES "") execute_process( COMMAND ${PYTHON_EXECUTABLE} -c "import site; print(';'.join(site.getsitepackages()))" OUTPUT_VARIABLE SITE_PACKAGES_LIST OUTPUT_STRIP_TRAILING_WHITESPACE) string(REPLACE ";" " " SITE_PACKAGES_LIST "${SITE_PACKAGES_LIST}") separate_arguments(SITE_PACKAGES_LIST) foreach(SITE_PACKAGES IN LISTS SITE_PACKAGES_LIST) if(EXISTS "${SITE_PACKAGES}" AND SITE_PACKAGES MATCHES "site-packages") string(REPLACE "\\" "/" PYTHON_SITE_PACKAGES "${SITE_PACKAGES}") message(STATUS "Found valid site-packages path: ${PYTHON_SITE_PACKAGES}.") break() endif() endforeach() if(NOT PYTHON_SITE_PACKAGES) message(FATAL_ERROR "No valid site-packages path found.") endif() if(MSVC) set(PADDLE_LIB "${PYTHON_SITE_PACKAGES}/paddle/base/libpaddle.lib") set(COMMON_LIB "${PYTHON_SITE_PACKAGES}/paddle/libs/common.lib") else() set(PADDLE_LIB "${PYTHON_SITE_PACKAGES}/paddle/base/libpaddle.so") endif() if(EXISTS ${PADDLE_LIB}) message(STATUS "Found libpaddle: ${PADDLE_LIB}.") else() message( FATAL_ERROR "Can not find libpaddle at ${PYTHON_SITE_PACKAGES}/paddle/base!") endif() # add paddle include dir set(PADDLE_INCLUDE_DIR ${PYTHON_SITE_PACKAGES}/paddle/include/) include_directories(${PADDLE_INCLUDE_DIR}) if(MSVC) # PIR patch add_custom_target( pir_patch ALL COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/patch/paddle/interface_support.h ${PADDLE_INCLUDE_DIR}/paddle/pir/include/core/interface_support.h COMMENT "Replace file ${PADDLE_INCLUDE_DIR}/paddle/pir/include/core/interface_support.h with ${CMAKE_SOURCE_DIR}/patch/paddle/interface_support.h" ) endif() if(APPLE) set_target_properties(paddle2onnx PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") elseif(MSVC) message("------ BUILD WITH MSVC --------") # remove `interface` macro defined in MSVC # target_compile_options(paddle2onnx PRIVATE /Uinterface) else() set_target_properties(paddle2onnx PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") set_target_properties(paddle2onnx PROPERTIES LINK_FLAGS "-Wl,--exclude-libs,ALL") set_target_properties(paddle2onnx PROPERTIES LINK_FLAGS_RELEASE -s) endif() set_target_properties(paddle2onnx PROPERTIES VERSION ${PADDLE2ONNX_VERSION}) target_link_libraries( paddle2onnx ${PADDLE_LIB} ${COMMON_LIB} p2o_paddle_proto onnx pybind11::embed glog::glog) endif() ``` -------------------------------- ### CMake Project Setup and Compiler Flags Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/CMakeLists.txt Configures the CMake project, sets the C++ standard to C++17, defines module paths, and enables position-independent code. It also includes conditional linking for libstdc++fs with specific GCC versions. ```cmake cmake_minimum_required(VERSION 3.16) project(paddle2onnx C CXX) # ONNX 1.16 requires C++ 17 set(CMAKE_CXX_STANDARD 17) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") # Build the libraries with - fPIC set(CMAKE_POSITION_INDEPENDENT_CODE ON) # Always link with libstdc++ fs.a when using GCC 8. link_libraries( "$<$,$,9.0>>:-lstdc++fs>" ) ``` -------------------------------- ### Configure and Generate Protobuf C++ Files in CMake Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/paddle2onnx/proto/CMakeLists.txt This script checks for an existing Protobuf target, finds the package if missing, and sets up the necessary include directories. It then uses the protobuf_generate_cpp macro to create source files from p2o_paddle.proto and adds them to a static library. ```cmake if(NOT TARGET protobuf) include(FindProtobuf) find_package(Protobuf REQUIRED) set(Protobuf_USE_STATIC_LIBS ON) include_directories(${PROTOBUF_INCLUDE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) set(PROTOBUF_LIBRARIES ${PROTOBUF_LIBRARIES} CACHE INTERNAL "" FORCE) endif() protobuf_generate_cpp(PROTO_SRC PROTO_HEADER p2o_paddle.proto) add_library(p2o_paddle_proto ${PROTO_HEADER} ${PROTO_SRC}) ``` -------------------------------- ### Convert PaddlePaddle model to ONNX via CLI Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/README_en.md Uses the paddle2onnx command-line interface to convert a PaddlePaddle model directory into an ONNX file. It requires specifying the model structure and parameter files. ```bash paddle2onnx --model_dir model_dir \ --model_filename model.json \ --params_filename model.pdiparams \ --save_file model.onnx ``` -------------------------------- ### Paddle2ONNX Build Configuration (CMake) Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/CMakeLists.txt This CMake script configures the build for Paddle2ONNX. It handles adding subdirectories for proto files, setting include directories, and recursively finding source files. It also manages versioning and conditional compilation based on build flags like WITH_STATIC. ```cmake add_subdirectory(${PROJECT_SOURCE_DIR}/paddle2onnx/proto) include_directories(${PROJECT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${PROJECT_SOURCE_DIR}/third_party/optimizer) file(GLOB_RECURSE ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/*.cc ${PROJECT_SOURCE_DIR}/third_party/optimizer/onnxoptimizer/*.cc) list(REMOVE_ITEM ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/cpp2py_export.cc) list(REMOVE_ITEM ALL_SRCS ${PROJECT_SOURCE_DIR}/third_party/optimizer/onnxoptimizer/cpp2py_export.cc) file(READ "${PROJECT_SOURCE_DIR}/VERSION_NUMBER" PADDLE2ONNX_VERSION) string(STRIP "${PADDLE2ONNX_VERSION}" PADDLE2ONNX_VERSION) ``` -------------------------------- ### Paddle2ONNX Build Options and Definitions Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/CMakeLists.txt Defines build options such as static compilation, debug logging, and MSVC specific settings. It also sets preprocessor definitions for ONNX opset version and internal flags. ```cmake option(WITH_STATIC "Compile Paddle2ONNX with STATIC" OFF) option(PADDLE2ONNX_DEBUG "If open the debug log while converting model" OFF) option(MSVC_STATIC_CRT "Compile Paddle2ONNX with MSVC STATIC CRT" OFF) if(MSVC) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") add_definitions(-DPADDLE_WITH_TESTING) endif() if(PADDLE2ONNX_DEBUG) add_definitions(-DPADDLE2ONNX_DEBUG) endif() # Set max opset version for onnx if you build from other version of onnx this # should be modified. add_definitions(-DMAX_ONNX_OPSET_VERSION=23) add_definitions(-DPADDLE2ONNX_LIB) # Internal flags for convert.h.in set(WITH_PADDLE2ONNX_STATIC_INTERNAL OFF) if(WITH_STATIC) set(WITH_PADDLE2ONNX_STATIC_INTERNAL ON CACHE BOOL "" FORCE) add_definitions(-DWITH_PADDLE2ONNX_STATIC_INTERNAL_AT_COMPILING) endif() ``` -------------------------------- ### Static Library Build for Paddle2ONNX (CMake) Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/CMakeLists.txt This section of the CMake script configures the build for a static Paddle2ONNX library. It creates a dummy target to manage dependencies and sets platform-specific compile flags for visibility. Finally, it bundles the static library. ```cmake if(WITH_STATIC) # Here, we use a dummy target (paddle2onnx_dummy) to form a build dependency # tree for paddle2onnx_static lib. add_library(paddle2onnx_dummy STATIC ${ALL_SRCS}) if(APPLE) set_target_properties(paddle2onnx_dummy PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") elseif(MSVC) message("------ BUILD WITH MSVC --------") else() set_target_properties(paddle2onnx_dummy PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") endif() target_link_libraries(paddle2onnx_dummy p2o_paddle_proto onnx) # Bundle paddle2onnx static lib here bundle_static_library(paddle2onnx_dummy paddle2onnx_static bundle_paddle2onnx) else() add_library(paddle2onnx SHARED ${ALL_SRCS}) ``` -------------------------------- ### Configuration File Inclusion Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/CMakeLists.txt Includes a utility script and configures a header file from a template. This is used to generate the mappers_registry.h file based on project settings. ```cmake include(utils) configure_file(${PROJECT_SOURCE_DIR}/paddle2onnx/mappers_registry.h.in ${PROJECT_SOURCE_DIR}/paddle2onnx/mappers_registry.h) ``` -------------------------------- ### ONNX Dependency Integration Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/CMakeLists.txt Includes the ONNX library as a third-party dependency. It checks if the 'onnx_proto' target already exists and, if not, sets the ONNX namespace and adds the ONNX subdirectory. ```cmake # Third dependency : onnx if(NOT TARGET onnx_proto) if(NOT ONNX_NAMESPACE) set(ONNX_NAMESPACE "onnx") endif() add_definitions("-DONNX_NAMESPACE=${ONNX_NAMESPACE}") add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/onnx) endif() ``` -------------------------------- ### Fixing Input Shape for Manually Built Networks Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/docs/en/FAQ.md This snippet illustrates how to fix the input shape for models with manually built networks in PaddlePaddle when converting to ONNX. It involves setting the `shape` parameter within `fluid.data()`. ```python fluid.data(shape=[]) ``` -------------------------------- ### Fixing Input Shape for PaddleX Models Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/docs/en/FAQ.md This snippet shows how to specify a fixed input shape for models converted from PaddleX to ONNX. The `--fixed_input_shape` argument is used to set the desired Height and Width. ```bash --fixed_input_shape=[Height,Width] ``` -------------------------------- ### Fixing Input Shape for Paddle Detection Models Source: https://github.com/paddlepaddle/paddle2onnx/blob/develop/docs/en/FAQ.md This snippet demonstrates how to fix the input shape for models originating from PaddleDetection when converting to ONNX. It involves modifying the `TestReader.inputs_def.image_shape` parameter. ```python TestReader.inputs_def.image_shape=[Channel,Height,Width] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.