CMake创建Libtorch项目
原创
©著作权归作者所有:来自51CTO博客作者茗君(Major_S)的原创作品,请联系作者获取转载授权,否则将追究法律责任
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>
int main() {
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';
}
创建项目
cmake -DCMAKE_PREFIX_PATH=E:\software\libtorch ..
编译结果
cmake --build .
运行结果