按照官方 PyTorch教程,我在 Python 中创建了模型,通过跟踪将其转换为 Torch 脚本,并将脚本模块保存到.pt
文件中。加载模型和 CMakeLists 的 C++ 代码与教程中的相同。
我下载了LibTorch 1.3(稳定,Windows,无CUDA,release)并解压,所以我的目录结构是:
│ ├────神器 │ traced_resnet_model.pt │ ├───cmakeapp │ │ CMakeLists.txt │ │ example-app.cpp │ │ ├────libtorch │ │ 构建哈希 │ ├───bin │ ├───cmake │ ├───包括 │ ├───lib │ ├───分享 │ └───测试
我将 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.
现在第一个问题,既不工作make
也不nmake
工作:
'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 成功找到了它。
第三,构建Release成功,但是跳过了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 ==========
不确定应该选择哪种解决方案配置来构建所有配置。
对于这些令人困惑的问题的澄清,我将不胜感激。