赞
踩
CONFIG += c++14 QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0 CONFIG += no_keywords INCLUDEPATH += \ <path-to-libtorch-folder>/libtorch/include \ <path-to-libtorch-folder>/libtorch/include/torch/csrc/api/include LIBS += \ -L<path-to-libtorch-folder>/libtorch/lib \ -ltorch \ -ltorch_cpu \ -lc10 \ -lc10_cuda \ -lcaffe2_nvrtc \ -lcpuinfo \ -lprotobuf \ -lprotobuf-lite \ -lprotoc \ -lcaffe2_module_test_dynamic \ -lcaffe2_detectron_ops_gpu
#undef slots
#include <torch/torch.h>
#define slots Q_SLOTS
若没有
#include <QObject>
可以不作修改
Non-Qt Project
CMake
cmake_minimum_required(VERSION 3.5)
project(<project_name> LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(Torch_DIR <path-to-libtorch-folder>/libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)
FILE(GLOB SC_FILES "*.cpp" "*.h")
add_executable(${PROJECT_NAME} ${SC_FILES})
target_link_libraries(${PROJECT_NAME} "${TORCH_LIBRARIES}")
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
#include <torch/torch.h>
#include <iostream>
using namespace std;
int main()
{
torch::Tensor tensor = torch::eye(3);
cout << tensor << endl;
}
1 0 0
0 1 0
0 0 1
[ CPUFloatType{3,3} ]
按 <RETURN> 来关闭窗口...
若仅需要推理,则只需要这一个头文件
#include <torch/script.h>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。