已经查看https://github.com/pytorch/pytorch上的pytorch各个版本,从pytorch1.0.0之后都是用vs2017编译的,所以下载编译好的libtorch使用时需要注意自己的vs版本。
At least Visual Studio 2017 version 15.6 with the toolset 14.13 and NVTX are needed.
CUDA version | Newest supported VS version |
---|---|
9.2 | Visual Studio 2017 Update 5 (15.5) (_MSC_VER <= 1912) |
10.0 | Visual Studio 2017 (15.X) (_MSC_VER < 1920) |
10.1 | Visual Studio 2019 (16.X) (_MSC_VER < 1930) |
libtorch源码编译:
cmd
:: [Optional] If you want to build with VS 2019 generator, please change the value in the next line to `Visual Studio 16 2019`.
:: Note: This value is useless if Ninja is detected. However, you can force that by using `set USE_NINJA=OFF`.
set CMAKE_GENERATOR=Visual Studio 15 2017
:: Read the content in the previous section carefully before you proceed.
:: [Optional] If you want to override the underlying toolset used by Ninja and Visual Studio with CUDA, please run the following script block.
:: "Visual Studio 2017 Developer Command Prompt" will be run automatically.
:: Make sure you have CMake >= 3.12 before you do this when you use the Visual Studio generator.
set CMAKE_GENERATOR_TOOLSET_VERSION=14.11
set DISTUTILS_USE_SDK=1
for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -version [15^,16^) -products * -latest -property installationPath`) do call "%i\VC\Auxiliary\Build\vcvarsall.bat" x64 -vcvars_ver=%CMAKE_GENERATOR_TOOLSET_VERSION%
:: [Optional] If you want to override the cuda host compiler
set CUDAHOSTCXX=C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\bin\HostX64\x64\cl.exe
python setup.py install
因为官方提供编译好debug与release,CPU与GPU版本的libtorch,所以一般情况下不需要我们自己编译库。
我直接下载的libtorch1.5.1-cu10.2的库,然后用VS2017编译了一个测试demo,demo是从网上找的,有部分错误已经解决。
(如果使用vs2015可能会报错:You need C++14 to compile PyTorch test_torch ,解决办法:换vs2017 )
生成一个libtorch需要的.pt文件。
import torch
import torchvision
# An instance of your model.
model = torchvision.models.resnet18()#这里保存的模型是自己训练好的网络模型
# An example input you would normally provide to your model's forward() method.
example = torch.rand(1, 3, 224, 224)
# Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("model.pt")
代码如下:
#include // One-stop header.
#include
#include
int main() {
// Deserialize the ScriptModule from a file using torch::jit::load().
torch::jit::script::Module module = torch::jit::load("./model.pt");
std::cout << "ok\n";
// Create a vector of inputs.
std::vector inputs;
inputs.push_back(torch::ones({ 1, 3, 224, 224 }));
// Execute the model and turn its output into a tensor.
at::Tensor output = module.forward(inputs).toTensor();
std::cout << output.slice(1, 0, 5) << '\n';
system("pause");
return 0;
}
然后生成Pytorch_test.exe,查看了一下依赖库:
PS D:\Pytorch_test\x64\Release> dumpbin /dependents Pytorch_test.exe
Microsoft (R) COFF/PE Dumper Version 14.00.24215.1
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file Pytorch_test.exe
File Type: EXECUTABLE IMAGE
Image has the following dependencies:
c10.dll
torch_cpu.dll
opencv_world410.dll
MSVCP140.dll
VCRUNTIME140.dll
api-ms-win-crt-heap-l1-1-0.dll
api-ms-win-crt-stdio-l1-1-0.dll
api-ms-win-crt-runtime-l1-1-0.dll
api-ms-win-crt-math-l1-1-0.dll
api-ms-win-crt-locale-l1-1-0.dll
KERNEL32.dll
其中依赖libtorch中的库只有c10.dll和torch_cpu.dll两个。其他依赖可以使用 下面的命令继续查找:
dumpbin /dependents xxx.dll 或者 xxx.exe
输出结果:
ok
0.0560 -0.6454 -0.5457 1.3441 0.3639
[ CPUFloatType{1,5} ]
请按任意键继续. . .
可能会遇到的问题参考下面的引用。
参考博客:libtorch error C2440: “初始化”: 无法从“torch::jit::script::Module”转换...的问题
参考博客:C++调用pytorch模型(vs2015+libtorch+pytorch)