0 下载准备
具备vs2015,cmake libtorch
libtorch
https://pytorch.org/get-started/locally/
先用的release版本测试了一下,
把lib中的dll放在bin中,然后加到环境变量的path中
保存使用的模型
定义自己model后,使用torch.jit.trace
example=torch.rand(1,3,234,345)#尺度随意
traced_script_module = torch.jit.trace(net, example)
traced_script_module.save("model.pt")
#output = traced_script_module(data)#可以用这个验证是否结果一致
pytorch部分到此结束
1 cmake
CMakeLists.txt
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(my_test)
find_package(Torch REQUIRED)
add_executable(example example.cpp)
target_link_libraries(example "${TORCH_LIBRARIES}")
set_property(TARGET example PROPERTY CXX_STANDARD 11)
example.cpp
#include <torch/script.h> // One-stop header.
#include <iostream>
#include <memory>
int main(int argc, const char* argv[]) {
if (argc != 2) {
std::cerr << "usage: example-app <path-to-exported-script-module>\n";
return -1;
}
// Deserialize the ScriptModule from a file using torch::jit::load().
std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(argv[1]);
assert(module != nullptr);
std::cout << "ok";
}
进入到build 打开cmd
执行:
cmake -DCMAKE_PREFIX_PATH=I:\libtorch -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 14 Win64" ..
-- Selecting Windows SDK version to target Windows 10.0.17134.
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
CMake Warning (dev) at I:/libtorch/share/cmake/Caffe2/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):
I:/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:121 (caffe2_interface_library)
I:/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 I:/libtorch/share/cmake/Torch/TorchConfig.cmake:88 (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.
-- Found torch: I:/libtorch/lib/torch.lib
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_BUILD_TYPE
-- Build files have been written to: H:/code/c++_code/pytorch+C/example/build
打开,运行,就可生成exe
ref
https://oldpan.me/archives/pytorch-windows-libtorch
https://oldpan.me/archives/pytorch-c-libtorch-inference
官网:https://pytorch.org/cppdocs/installing.html
测试
在python中
arr=np.arange(1,19).reshape((3,2,3)).astype(np.float32)
arr_torch = torch.from_numpy(arr).float().unsqueeze(0)
output = traced_script_module(arr_torch )
输出
output
Out[40]:
tensor([[[[ 1.0000, 2.7597, 3.3423],
[ 5.1762, 8.7566, 7.2332]],
[[11.7321, 19.8502, 15.8473],
[17.1406, 27.9997, 21.4676]],
[[22.5052, 36.0626, 27.0142],
[27.9509, 44.2620, 32.6647]]]], grad_fn=<ThresholdBackward1>)
arr
Out[41]:
array([[[ 1., 2., 3.],
[ 4., 5., 6.]],
[[ 7., 8., 9.],
[10., 11., 12.]],
[[13., 14., 15.],
[16., 17., 18.]]], dtype=float32)
C++中
float data[] = { 1, 2, 3,4, 5, 6 ,
7,8,9,10,11,12,13,
14,15,16,17,18};
torch::TensorOptions option(torch::kFloat32);
torch::Tensor f = torch::from_blob(data, { 1, 3, 2,3 }, option);
cout << f;
at::Tensor result = module->forward({ f }).toTensor();
cout << result;
输出
(1,1,.,.) =
1 2 3
4 5 6
(1,2,.,.) =
7 8 9
10 11 12
(1,3,.,.) =
13 14 15
16 17 18
[ Variable[CPUType]{1,3,2,3} ]
(1,1,.,.) =
1.0000 2.7597 3.3423
5.1762 8.7566 7.2332
(1,2,.,.) =
11.7321 19.8502 15.8473
17.1406 27.9997 21.4676
(1,3,.,.) =
22.5052 36.0626 27.0142
27.9509 44.2620 32.6647
[ Variable[CPUType]{1,3,2,3} ]
一致!
https://pytorch.org/tutorials/advanced/cpp_export.html
译文:https://blog.csdn.net/dou3516/article/details/82912480
torchscript:https://pytorch.org/docs/master/jit.html