opencv+libtorch c++工程问题及解决方案

问题1:添加opencv库后,报错:

 by not providing "findqt5core.cmake" in cmake_module_path this project has  asked cmake to find a ...
  • 原因:未安装qt5
  • 解决方案:安装qt5
sudo apt-get install qt5-default

安装后编译时,仍然报错如上,解决方案:安装QT5后重启电脑

问题2:opencv报错

没有规则可制作目标“opencv/build/lib/libopencv_gapi.so.4.5.2libopencv_gapi.so.4.5.2”
  • 原因:只采用cmake-gui对opencv进行configure和Generate,尚未进行编译和安装
  • 解决方案:在build中打开终端:
make
make install

问题3:CMakeLists设置

cmake_minimum_required(VERSION 3.17)

project(enhance)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu")
find_package(Qt5Core)

add_executable(${PROJECT_NAME} "main.cpp" xx.h xx.cpp xx.cpp)

target_link_libraries(${PROJECT_NAME} Qt5::Core)


## torch
#set(CMAKE_PREFIX_PATH /media/zyy/software/libtorch/share/cmake/Torch)
set(CMAKE_PREFIX_PATH /media/zyy/software/anaconda3/envs/pytorch160/lib/python3.7/site-packages/torch)
find_package(Torch REQUIRED NO_CMAKE_FIND_ROOT_PATH)
target_link_libraries(${PROJECT_NAME} "${TORCH_LIBRARIES}")

# OpenCV
find_package(OpenCV REQUIRED)
# 添加头文件
include_directories(${OpenCV_INCLUDE_DIRS})
# Link your application with OpenCV libraries
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

问题4:undefined reference to “cv::imread”

现象:opencv和torch分别使用不会报错,但一起使用报上述找不到cv中的函数

原因: opencv4.x编译后与libtorch有冲突
解决方案: 编译安装opencv3.4.14版本(opencv3.x应该都可行)

  • 卸载安装的opencv:
sudo make uninstall
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
sudo apt-get --purge remove opencv-doc opencv-data python-opencv
  • 检查
locate opencv
  • 重新编译安装opencv3.4.14(网上有很多教程,请自行编译安装)

问题5:Mat转Tensor后放入GPU报错

Process finished with exit code 11

源代码如下:

torch::Tensor input_tensor = torch::from_blob(img.data, {1, img_float.rows, img_float.cols, img.channels()});
input_tensor = input_tensor.toType(torch::kFloat).to(torch::kCUDA);

猜测原因:tensor和Mat类型应对应,Mat为CV_8U,解决方案有二;
解决方案1(缺点耗时):

cv::Mat img_float;
img.convertTo(img_float, CV_32FC3);
auto img_tensor = torch::from_blob(img_float.data, { 1, img_float.rows, img_float.cols, img.channels() }).to(torch::kCUDA);
img_tensor = img_tensor.permute({0, 3, 1, 2});

解决方案2:

auto img_tensor = torch::from_blob(img.data, { 1, img.rows, img.cols, img.channels()}, torch::kByte).to(torch::kCUDA);
img_tensor = img_tensor.permute({0, 3, 1, 2}).toType(torch::kFloat);

问题6:libtorch GPU占用大,导致GPU溢出

在python环境中,模型推理之前加入 with torch.no_grad(), 可以做计算图,减少GPU占用

  • 解决方案
torch::NoGradGuard no_grad;
torch::Tensor output = model.forward(inputs).toTensor();

你可能感兴趣的:(C++,Pytorch,opencv,cmake)