官方下载Libtorch
cmake编译
cmake_minimum_required (VERSION 3.12)
project (reid_demo_libtorch)
set (Torch_DIR "D:\\cpp\\libtorch\\share\\cmake\\Torch")
set (OpenCV_DIR "E:\\opencvbuilder")
find_package (Torch REQUIRED)
find_package (OpenCV REQUIRED)
if (NOT Torch_FOUND)
message (FATAL_ERROR "Libtorch Not Found!")
endif (NOT Torch_FOUND)
if (NOT OpenCV_FOUND)
message (FATAL_ERROR "OPenCV Not Found!")
endif (NOT OpenCV_FOUND)
message (STATUS "Libtorch status: ")
message (STATUS "libraries: ${TORCH_LIBRARIES}")
message (STATUS "OpenCV libraries status: ")
message (STATUS "OpenCV version: ${OpenCV_VERSION}")
message (STATUS "OpenCV libraries: ${OpenCV_LIBS}")
message (STATUS "OpenCV include path: ${OpenCV_INCLUDE_DIRS}")
aux_source_directory (src SRC_LIST)
include_directories (include)
add_executable (main ${SRC_LIST})
target_link_libraries (
main
${OpenCV_LIBS}
${TORCH_LIBRARIES}
)
检查cpu和cuda
#include <iostream>
#include <torch/torch.h>
#include<torch/script.h>
using namespace std;
int main()
{
cout << "cuda is" << torch::cuda::is_available() << endl;
cout << "cudnn is" << torch::cuda::cudnn_is_available() << endl;
cout << torch::cuda::device_count() << endl;
torch::Tensor tr = torch::arange(0, 9, torch::kFloat32).reshape({ 3,3 });
cout << tr << endl;
return 0;
}
yolo导出libtorch的pt模型
c++推理