在 Windows 上的 C++ 应用程序中使用 PyTorch 模型

men*_*oom 3 cmake visual-studio torchscript libtorch

按照官方 PyTorch教程,我用 Python 创建了模型,通过跟踪将其转换为 Torch 脚本,并将脚本模块保存到文件中.pt。加载模型和 CMakeLists 的 C++ 代码与教程中的代码相同。

\n\n

我下载了LibTorch 1.3(稳定版,Windows,无CUDA,发布版)并解压,所以我的目录结构是:

\n\n
\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80artifact\n\xe2\x94\x82 Traced_resnet_model.pt\n\xe2\ x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80cmakeapp\n\xe2\x94\x82\xe2\x94\x82 CMakeLists.txt\n\xe2 \x94\x82 \xe2\x94\x82 示例-app.cpp\n\xe2\x94\x82 \xe2\x94\x82 \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\ xe2\x94\x80libtorch\n\xe2\x94\x82 \xe2\x94\x82 构建哈希 \n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2 \x94\x80bin\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80cmake\n\xe2\x94\x82 \xe2\x94\x9c \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80include\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80lib \n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80share\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94 \x80\xe2\x94\x80\xe2\x94\x80test\n\n
\n\n

我安装了 Visual Studio 2019,并将 CMake 作为组件安装,因此我运行了 VS2019 的开发人员命令提示符并cd运行到项目目录 (cmakeapp)。

\n\n

根据指南,我运行了以下命令来构建应用程序:

\n\n
mkdir build\ncd build\ncmake -DCMAKE_PREFIX_PATH=..\\libtorch ..\nmake\n
Run Code Online (Sandbox Code Playgroud)\n\n

CMake 似乎成功了,除了一些警告:

\n\n
CMake Warning (dev) at D:/dox/projects/AI/torchscript/libtorch/share/cmake/Caffe\n2/public/utils.cmake:57 (if):\n  Policy CMP0054 is not set: Only interpret if() arguments as variables or\n  keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy\n  details.  Use the cmake_policy command to set the policy and suppress this\n  warning.\n\n  Quoted variables like "MSVC" will no longer be dereferenced when the policy\n  is set to NEW.  Since the policy is not set the OLD behavior will be used.\nCall Stack (most recent call first):\n  D:/dox/projects/AI/torchscript/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:\n121 (caffe2_interface_library)\n  D:/dox/projects/AI/torchscript/libtorch/share/cmake/Torch/TorchConfig.cmake:40\n (find_package)\n  CMakeLists.txt:4 (find_package)\nThis warning is for project developers.  Use -Wno-dev to suppress it.\n\nCMake Warning (dev) at D:/dox/projects/AI/torchscript/libtorch/share/cmake/Torch\n/TorchConfig.cmake:90 (if):\n  Policy CMP0054 is not set: Only interpret if() arguments as variables or\n  keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy\n  details.  Use the cmake_policy command to set the policy and suppress this\n  warning.\n\n  Quoted variables like "MSVC" will no longer be dereferenced when the policy\n  is set to NEW.  Since the policy is not set the OLD behavior will be used.\nCall Stack (most recent call first):\n  CMakeLists.txt:4 (find_package)\nThis warning is for project developers.  Use -Wno-dev to suppress it.\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在是第一个问题,既不工作make也不nmake工作:

\n\n
\'make\' is not recognized as an internal or external command, operable program or batch file.\n\nD:\\dox\\projects\\AI\\torchscript\\cmakeapp\\build>nmake\n\nMicrosoft (R) Program Maintenance Utility Version 14.23.28107.0 Copyright (C) Microsoft Corporation.  All rights reserved.\n\nNMAKE : fatal error U1064: MAKEFILE not found and no target specified Stop.\n
Run Code Online (Sandbox Code Playgroud)\n\n

我错过了什么吗?

\n\n

其次,我找到了生成的custom_ops.sln文件,因此在 Visual Studio 中打开它。该项目提供 4 种不同的配置:Debug、MinSizeRel、Release 和 RelWithDebInfo。构建除发布之外的任何内容都会失败:

\n\n
LINK : fatal error LNK1181: cannot open input file \'torch-NOTFOUND.obj\'\n2>Done building project "example-app.vcxproj" -- FAILED.\n
Run Code Online (Sandbox Code Playgroud)\n\n

我对这个错误感到非常惊讶,因为指定了 libtorch 路径并且 CMake 成功找到了它。

\n\n

第三,构建 Release 成功,但它跳过 ALL_BUILD 项目:

\n\n
3>------ Skipped Build: Project: ALL_BUILD, Configuration: Release x64 ------\n3>Project not selected to build for this solution configuration \n========== Build: 2 succeeded, 0 failed, 0 up-to-date, 1 skipped ==========\n
Run Code Online (Sandbox Code Playgroud)\n\n

不确定应该选择什么解决方案配置来构建所有这些。

\n\n

对于这些令人困惑的问题的澄清,我将不胜感激。

\n

squ*_*les 6

链接站点上的说明以 Linux 为中心,并且似乎假设用户在 Linux 环境中操作。在 Linux 上,最后一个命令make可以正常工作,但您可能使用 Visual Studio 进行构建,而不是make. 相反,您应该采用跨平台方法,并告诉 CMake 使用它在配置过程中找到的任何构建工具进行构建;尝试使用cmake build .最后一个命令,正如您在其他教程中看到的那样

mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..
cmake --build .
Run Code Online (Sandbox Code Playgroud)

但是,该教程中提到了以下内容:

在 Windows 上,调试和发布版本不兼容 ABI。如果您计划在调试模式下构建项目,我们建议从源代码构建 PyTorch。

这表明发布配置应该可以工作,而您需要从 Github 下载源代码才能在调试模式下构建。由于 MSVC 默认构建 Debug,因此您应该修改最后一个命令以指示配置Release

cmake --build . --config Release
Run Code Online (Sandbox Code Playgroud)

此外,当使用 MSVC 在 Windows 上构建时,他们的安装教程建议将以下行附加到您的 CMake 文件中,以避免此问题线程中讨论的错误,这也可能有助于解决您遇到的问题:

if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET example-app
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:example-app>)
endif (MSVC)
Run Code Online (Sandbox Code Playgroud)