win下clion配置pytorch和OpenCV
- 一、clion配置vs编译器以及测试
- 二、clion配置pytorch
- 2.1、下载libtorch
- 2. 2、环境变量配置
- 2.3、cmakelist.txt编写
- 2.4、main函数测试运行
- 三、clion配置opencv
- 3.1、源码下载
- 3.2、编译
- 3.3、环境变量配置
- 3.4、cmakelist.txt编写
- 3.5 main函数测试运行
博主是安装了vs后通过clion编辑器配置pytorch和opencv。
一、clion配置vs编译器以及测试
博主clion平时用的mingw编译,这里改为visual studio在安装过程中发现mingw有些不适配的问题,所以改为了vs编译。
首先点击【File】→【Settings】
【Toolchains】按图中所示进行设置后【Apply】
【Cmake】也需要进行如下设置:
最后注意这里也需要改为配置好的vs。
二、clion配置pytorch
2.1、下载libtorch
下载地址:pytorch官网
图里的CUDA版本向下兼容,比如图里的cuda11.8可适配11.8及其以下cuda版本。
红色框和绿色框分别是release和debug版本这里注意和clion配置vs匹配上即可。后续会详细说明。
2. 2、环境变量配置
下载后解压到任意指定安装目录。
进入到目录***\libtorch\share\cmake\Torch
,比如博主是D:\pyTorch\libtorch-win-shared-with-deps-2.0.0+cu117\libtorch\share\cmake\Torch
,复制此目录地址添加到环境变量。
配置完成后重启电脑。
2.3、cmakelist.txt编写
cmake_minimum_required(VERSION 3.19)
project(ModelDeploy)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_PREFIX_PATH D:/pyTorch/libtorch-win-shared-with-deps-2.0.0+cu117/libtorch)
#set(Torch_DIR "E:/libtorch/share/cmake/Torch")
#include_directories("E:/libtorch/include")
#include_directories("E:/libtorch/include/torch/csrc/api/include")
find_package(Torch REQUIRED )
add_executable(modelDeploy main.cpp)
target_link_libraries(modelDeploy ${TORCH_LIBRARIES} ${OpenCV_LIBS})
set_property(TARGET modelDeploy PROPERTY CXX_STANDARD 14)
其中下图的目录地址为3中libtorch的安装目录。
2.4、main函数测试运行
#include <iostream>
#include <torch/script.h>
#include <memory>
#include <torch/torch.h>
int main() {
std::cout << "Hello world." << std::endl;
torch::Tensor a = torch::rand({2, 3});
std::cout << a << std::endl;
/*std::string path = "D:/aniya.jpg";
Mat im = imread(path);
imshow("image", im);
waitKey(0);*/
return 0;
}
成功运行:
三、clion配置opencv
与libtorch不同,OpenCV需要编译后安装。
3.1、源码下载
OpenCV源码
下载后解压用clion打开
3.2、编译
同二中clion配置vs编译器,这里同样需要将clion的编译器设置为vs。
设置完成后点击【build】开始编译,编译结束后【install】。注意编译过程中可能会有很多warning,无需在意。
3.3、环境变量配置
同libtorch的环境变量配置,这里需要添加的环境变量为:**\opencv-4.5.5\cmake-build-release-visual-studio\install\x64\vc16\bin
博主为D:\opencv\opencv-4.5.5\cmake-build-release-visual-studio\install\x64\vc16\bin
;如果这里是用mingw编译的,目录则为**\cmake-build-debug\install\x64\mingw\bin
。
目录地址如下图所示:
环境变量配置后重启电脑生效。
3.4、cmakelist.txt编写
在1.3中cmakelist.txt中添加
set(OpenCV_DIR D:/opencv/opencv-4.5.5/cmake-build-release-visual-studio/install)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
图中目录即为OpenCV编译后的安装目录。
完整的cmakelist.txt:
cmake_minimum_required(VERSION 3.19)
project(ModelDeploy)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_PREFIX_PATH D:/pyTorch/libtorch-win-shared-with-deps-2.0.0+cu117/libtorch)
#set(Torch_DIR "E:/libtorch/share/cmake/Torch")
#include_directories("E:/libtorch/include")
#include_directories("E:/libtorch/include/torch/csrc/api/include")
find_package(Torch REQUIRED )
set(OpenCV_DIR D:/opencv/opencv-4.5.5/cmake-build-release-visual-studio/install)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(modelDeploy main.cpp)
target_link_libraries(modelDeploy ${TORCH_LIBRARIES} ${OpenCV_LIBS})
set_property(TARGET modelDeploy PROPERTY CXX_STANDARD 14)
3.5 main函数测试运行
#include <iostream>
#include <torch/script.h>
#include <memory>
#include <torch/torch.h>
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int main() {
std::cout << "Hello world." << std::endl;
torch::Tensor a = torch::rand({2, 3});
std::cout << a << std::endl;
std::string path = "D:/aniya.jpg";
Mat im = imread(path);
imshow("image", im);
waitKey(0);
return 0;
}
成功运行: