C M a k e 创 建 L i b t o r c h 项 目 CMake创建Libtorch项目 CMakeLibtorch

项目结构图

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创建Libtorch项目_创建项目_02

编译结果

cmake --build .

CMake创建Libtorch项目_#include_03

运行结果

CMake创建Libtorch项目_计算机视觉_04

CMake创建Libtorch项目_opencv_05