- libtorch1.0
- vs2017
- CMake3.14
- windows10
- 无cuda
用c++调用pytorch模型官网上面有详细教程,也有很多博客,可以参考以下链接:
以及:
在操作的过程中,主要遇到三个比较大的问题:
问题一:cmake编译时以及找不到torch和opencv相关的cmake文件(以下代码是复制的stackoverflow上别人的报错,我当时没有把自己的报错截图下来,但问题是一样的)
-- The C compiler identification is GNU 7.3.0-- The CXX compiler identification is GNU 7.3.0-- Could not determine Eclipse version, assuming at least 3.6 (Helios). Adjust CMAKE_ECLIPSE_VERSION if this is wrong.-- Check for working C compiler: /usr/bin/cc-- Check for working C compiler: /usr/bin/cc -- works-- Detecting C compiler ABI info-- Detecting C compiler ABI info - done-- Detecting C compile features-- Detecting C compile features - done-- Check for working CXX compiler: /usr/bin/c++-- Check for working CXX compiler: /usr/bin/c++ -- works-- Detecting CXX compiler ABI info-- Detecting CXX compiler ABI info - done-- Detecting CXX compile features-- Detecting CXX compile features - doneCMake Error at CMakeLists.txt:10 (find_package): By not providing "FindTorch.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Torch", but CMake did not find one. Could not find a package configuration file provided by "Torch" with any of the following names: TorchConfig.cmake torch-config.cmake Add the installation prefix of "Torch" to CMAKE_PREFIX_PATH or set "Torch_DIR" to a directory containing one of the above files. If "Torch" provides a separate development package or SDK, be sure it has been installed.-- Configuring incomplete, errors occurred!
原因:它无法找到TorchConfig.cmake
和torch-config.cmake
文件
解决方法:通过编辑CMakeLists.txt解决,将libtorch文件中的path/libtorch/share/cmake/Torch 加到路径中去,如图所示
以及将opencv安装路径的build添加到系统环境变量中去:(变量名为如图所示,地址根据个人地址修改)
问题二:编译成功后,运行.cpp显示程序无法启动ALL_BUILD 拒绝访问
解决方法:CMake编译成功后会生成三个工程文件,分别是ALL_BUILD,工程本身文件(我的叫lenet),以及ZERO_CHECK,将ALL_BUILD和ZERO_CHECK在工程文件中移除即可以解决.
问题三:找不到dll文件,如图:
解决方法:将路径为\path\libtorch\lib中对应的torch.dll等对应缺失的文件复制到路径为\build\Debug和\build\Release中去即可解决.
注:用vs运行.cpp时要讲解决方案配置改为Release x64的方式,不然会报错.