- libtorch1.0
- vs2017
- CMake3.14
- windows10
- 無cuda
用c++調用pytorch模型官網上面有詳細教程,也有很多博客,可以參考以下鏈接:https://blog.csdn.net/dou3516/article/details/82912480
以及:https://zhuanlan.zhihu.com/p/55292836
在操作的過程中,主要遇到三個比較大的問題:
問題一: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 - done CMake 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的方式,不然會報錯.