在 Ubuntu 20.04 上设置 ONNX 运行时(C++ API)

2024-02-09

我目前正在尝试让我的图像处理程序在 Ubuntu(来自 Windows)上运行。

我已经成功构建并链接了 OpenCV 和 Boost 库以与我的 cpp 程序一起使用,但是我还没有找到任何关于在 Ubuntu 20.04 上设置 Onnx Runtime C++ 的说明除了对特定 Visual Studio 项目使用以下带有 NuGet 包管理器的命令之外:

Install-Package Microsoft.ML.OnnxRuntime -Version 1.4.0

在 Windows 上,我只需要使用 NuGet 包管理器来下载给定 Visual Studio 项目的库。似乎可以使用 NuGet 在 Ubuntu 上执行此操作,但我想知道是否可以更“手动”执行此操作,例如 boost 和 OpenCV 构建和安装。谢谢!


在 Linux 上安装 NuGet Onnxruntime 版本

在 Ubuntu 20.04 上测试

对于可通过以下方式获得的 onnxruntime 的较新版本NuGet https://www.nuget.org/packages/Microsoft.ML.OnnxRuntime/我采用了以下工作流程:下载版本(此处为 1.7.0,但您可以相应更新链接),并将其安装到~/.local/。对于全局(系统范围)安装,您可以将文件放在相应的文件夹中/usr/local/.

mkdir /tmp/onnxInstall
cd /tmp/onnxInstall
wget -O onnx_archive.nupkg https://www.nuget.org/api/v2/package/Microsoft.ML.OnnxRuntime/1.7.0
unzip onnx_archive.nupkg
cp runtimes/linux-x64/native/libonnxruntime.so ~/.local/lib/
cp -r build/native/include/ ~/.local/include/onnxruntime/

Cmake

现在如果你想能够find_package(onnxruntime)从你的 Cmake 包中,我建议你将我自己创建的 onnx cmake 文件放在~/.local/share/cmake/onnxruntime。这些文件是:

cat ~/.local/share/cmake/onnxruntime/onnxruntimeVersion.cmake:

# Custom cmake version file by jcarius

set(PACKAGE_VERSION "1.7.0")

# Check whether the requested PACKAGE_FIND_VERSION is compatible
if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
  set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
  set(PACKAGE_VERSION_COMPATIBLE TRUE)
  if("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
    set(PACKAGE_VERSION_EXACT TRUE)
  endif()
endif()

cat ~/.local/share/cmake/onnxruntime/onnxruntimeConfig.cmake

# Custom cmake config file by jcarius to enable find_package(onnxruntime) without modifying LIBRARY_PATH and LD_LIBRARY_PATH
#
# This will define the following variables:
#   onnxruntime_FOUND        -- True if the system has the onnxruntime library
#   onnxruntime_INCLUDE_DIRS -- The include directories for onnxruntime
#   onnxruntime_LIBRARIES    -- Libraries to link against
#   onnxruntime_CXX_FLAGS    -- Additional (required) compiler flags

include(FindPackageHandleStandardArgs)

# Assume we are in <install-prefix>/share/cmake/onnxruntime/onnxruntimeConfig.cmake
get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(onnxruntime_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

set(onnxruntime_INCLUDE_DIRS ${onnxruntime_INSTALL_PREFIX}/include)
set(onnxruntime_LIBRARIES onnxruntime)
set(onnxruntime_CXX_FLAGS "") # no flags needed


find_library(onnxruntime_LIBRARY onnxruntime
    PATHS "${onnxruntime_INSTALL_PREFIX}/lib"
)

add_library(onnxruntime SHARED IMPORTED)
set_property(TARGET onnxruntime PROPERTY IMPORTED_LOCATION "${onnxruntime_LIBRARY}")
set_property(TARGET onnxruntime PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_INCLUDE_DIRS}")
set_property(TARGET onnxruntime PROPERTY INTERFACE_COMPILE_OPTIONS "${onnxruntime_CXX_FLAGS}")

find_package_handle_standard_args(onnxruntime DEFAULT_MSG onnxruntime_LIBRARY onnxruntime_INCLUDE_DIRS)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Ubuntu 20.04 上设置 ONNX 运行时(C++ API) 的相关文章

随机推荐