C
M
a
k
e
创
建
L
i
b
t
o
r
c
h
项
目
CMake创建Libtorch项目
CMake创建Libtorch项目
项目结构图
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>intmain(){using torch::jit::script::Module;Module module =
torch::jit::load("D:\\workplace\\python\\OOP\\unet.pt");
std::cout <<"ok\n";// Create a vector of inputs.
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::ones({1,3,512,512}));// Execute the model and turn its output into a tensor.
at::Tensor output = module.forward(inputs).toTensor();
std::cout << output <<'\n';}