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

时间:2019-11-23 10:16:28

标签: visual-studio cmake libtorch torchscript

在正式的PyTorch tutorial之后,我用Python创建了模型,并通过跟踪将其转换为Torch Script,并将脚本模块保存到.pt文件中。加载模型和CMakeList的C ++代码与本教程中的代码相同。

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

│
├───artifact
│       traced_resnet_model.pt
│       
├───cmakeapp
│   │   CMakeLists.txt
│   │   example-app.cpp
│   │   
├───libtorch
│   │   build-hash   
│   ├───bin
│   ├───cmake
│   ├───include
│   ├───lib
│   ├───share
│   └───test

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

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

mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=..\libtorch ..
make

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

CMake Warning (dev) at D:/dox/projects/AI/torchscript/libtorch/share/cmake/Caffe
2/public/utils.cmake:57 (if):
  Policy CMP0054 is not set: Only interpret if() arguments as variables or
  keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  Quoted variables like "MSVC" will no longer be dereferenced when the policy
  is set to NEW.  Since the policy is not set the OLD behavior will be used.
Call Stack (most recent call first):
  D:/dox/projects/AI/torchscript/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:
121 (caffe2_interface_library)
  D:/dox/projects/AI/torchscript/libtorch/share/cmake/Torch/TorchConfig.cmake:40
 (find_package)
  CMakeLists.txt:4 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at D:/dox/projects/AI/torchscript/libtorch/share/cmake/Torch
/TorchConfig.cmake:90 (if):
  Policy CMP0054 is not set: Only interpret if() arguments as variables or
  keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  Quoted variables like "MSVC" will no longer be dereferenced when the policy
  is set to NEW.  Since the policy is not set the OLD behavior will be used.
Call Stack (most recent call first):
  CMakeLists.txt:4 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

现在第一期makenmake都不起作用:

'make' is not recognized as an internal or external command, operable program or batch file.

D:\dox\projects\AI\torchscript\cmakeapp\build>nmake

Microsoft (R) Program Maintenance Utility Version 14.23.28107.0 Copyright (C) Microsoft Corporation.  All rights reserved.

NMAKE : fatal error U1064: MAKEFILE not found and no target specified Stop.

我想念什么吗?

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

LINK : fatal error LNK1181: cannot open input file 'torch-NOTFOUND.obj'
2>Done building project "example-app.vcxproj" -- FAILED.

由于指定了libtorch路径,并且CMake成功找到它,我对此错误感到非常惊讶。

第三,构建发行成功,但是它跳过了ALL_BUILD项目:

3>------ Skipped Build: Project: ALL_BUILD, Configuration: Release x64 ------
3>Project not selected to build for this solution configuration 
========== Build: 2 succeeded, 0 failed, 0 up-to-date, 1 skipped ==========

不确定应选择哪种解决方案配置来构建所有解决方案。

对于这些令人困惑的观点,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

linked site上的说明以Linux为中心,并且似乎假定用户在Linux环境中进行操作。在Linux上,最后一个命令make可以正常工作,但是您可能是使用Visual Studio构建的,而不是make。相反,您应该采用跨平台方法,并告诉CMake使用在配置过程中找到的任何构建工具进行构建。尝试使用cmake build .代替最后一个命令,就像您在this other tutorial中看到的那样:

mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..
cmake --build .

但是,该教程中提到的内容如下:

  

在Windows上,调试和发行版不兼容ABI。如果您打算以调试模式构建项目,建议从源代码构建PyTorch。

这表示Release配置应该可以工作,而您需要下载source code from Github才能以Debug模式进行构建。当MSVC默认情况下构建Debug时,您应该修改最后一个命令以指示Release配置:

cmake --build . --config Release

此外,在使用MSVC的Windows上进行构建时,其安装指南建议在您的CMake文件中添加以下行,以避免在此issue thread中讨论的错误,这也可能有助于解决您遇到的问题:

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)

答案 1 :(得分:-1)

我无法直接回答之前的答案。 我不确定我是否完全了解正在发生的事情,但是我找到了一种方法来避免错误torch-NOTFOUND.obj并使我的项目在RelWithDebugInfo中进行编译。该错误似乎是IMPORTED_LOCATION错误问题IMPORTED_LOCATION and -NOTFOUND。 如果在结尾处进入libtorch \ share \ cmake \ Caffe2 \ Caffe2Targets-release.cmake(从1.5.0版开始的第51-53行),则会发现:

set_target_properties(torch PROPERTIES
  IMPORTED_IMPLIB_RELEASE "${_IMPORT_PREFIX}/lib/torch.lib"
  IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/torch.dll"
  )

我替换为:

set_target_properties(torch PROPERTIES
  IMPORTED_IMPLIB "${_IMPORT_PREFIX}/lib/torch.lib"
  IMPORTED_LOCATION "${_IMPORT_PREFIX}/lib/torch.dll"
  )

我可以成功编译。