Windows 7+Visual Studio 2015下Caffe安裝(CPU/非GPU)
由於專案組安排跟進深度學習、目標檢測,準備先嚐試一下MTCNN,其網路執行需要Caffe,暫時又只有Windows 7系統,因此先準備安裝caffe-windows。其次,考慮用筆記本GPU跑可能會炸,因此準備安裝CPU版的。
按照README.md進行安裝,首先看到要求環境是:
Requirements: Visual Studio 2015, CUDA 9.0, third party libraries: Baidu Yun or Google Drive. Please extract the archive into ./windows/thirdparty/
. Then, add the folder ./windows/thirdparty/bins
PATH
.
Visual Studio要求2015版,我電腦只有2013,得重新安裝,也正合我意,因為我一般不喜歡用軟體最新的版本,傾向選擇最新的前一版。安裝vs2015:下載正版,參考使用專業版金鑰(HMGNV-WCYXV-X7G9W-YCX63-B98R2),安裝過程中及後續安裝過程需較好的網路環境,會下載很多東西。
繼續跟著README.md走,
Pre-Build Steps:
Copy `.\windows\CommonSettings.props.example` to `.\windows\CommonSettings.props`
By defaults Windows build requires `CUDA` and `cuDNN` libraries.
Both can be disabled by adjusting build variables in `.\windows\CommonSettings.props`.
Python support is disabled by default, but can be enabled via `.\windows\CommonSettings.props` as well.
將`.\windows\CommonSettings.props.example`複製一份,名稱改為CommonSettings.props,用於vs專案配置。
Cuda、cudnn支援?
由於沒打算用GPU跑,因此不裝cuda及cudnn,所以將CommonSettings.props中相關項都改為false,將cpuonly設為true。
Python支援?
已有python2.7,安裝numpy、scipy、matplotlib、scikit-image、protobuf等python庫,numpy、scipy、matplotlib、scikit-image在Anaconda中已有,在cmd命令列輸入“pip install protobuf”可安裝protobuf。
在CommonSettings.props中設定支援python,將關於python路徑的項進行設定:
我的python所在目錄為“D:\Program Files (x86)\Anaconda\”,因此如下設定:
Matlab支援?
在CommonSettings.props中設定支援Matlab,將關於Matlab路徑的項進行設定:
Build:
關鍵步驟問題較多。
開啟工程後即提示VS2015缺python相關支援,聯網自動修復下載安裝了好一會。
開始嘗試單獨生成(build)caffe.exe,成功了。
嘗試批生成,失敗,原因很多,一個個來:
0、首先,之前已經在配置檔案中已設定不使用cuda及cudnn,結果生成時一直報錯:“無法開啟cufft.lib檔案”。很煩,弄了很久,最後直接將CommonSettings.props中的<CudaDependencies>cufft.lib</CudaDependencies>註釋掉(改為<!-- <CudaDependencies>cufft.lib</CudaDependencies> -->),就沒問題了,真是無語。(後續發現這篇部落格中有提到)
1、編譯caffe.binding出現問題:“error C2220 。。。。沒有生成“object”檔案”
根據一篇部落格提示是編碼方式的問題。因為我們的系統大部分使用的是中文,而被編譯.cpp檔案是英文的。按此法打開出此錯的類的主檔案(好像是CaffeBinding.cpp?),儲存,再編譯,無錯。(很奇怪的是caffe.cpp編譯沒出問題,後面其他庫生成也無此問題,望大神留言指點)
2、前面的問題可能出在批生成(可能是批生成中包括Debug模式生成的問題,沒弄明白,望大神留言指點),後續沒有再用批生成,選擇一個一個類(release)生成,其中Matcaffe及test_all出現問題。
Matcaffe用於支援matlab,錯誤為:windows sdk 10 was not found。
我係統為win7,不清楚為什麼一定要win10sdk,不是很懂,嘗試在vs2015中類對應屬性中的常規(general)項中修改,發現改不動。
後發現直接用文字編輯器(Notepad++)開啟對應vs工程配置檔案matcaffe.vcxproj,修改<WindowsTargetPlatformVersion>10.0.10586.0</WindowsTargetPlatformVersion>為<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>,再生成就沒什麼問題了。
這裡額外提醒一下,在復現他人程式碼時,凡是各類系統、軟體、庫版本(如cuda、windows、vs、opencv等)不適配的,都可以嘗試在VS工程配置檔案xxx.vcxproj中用notepad等文字編輯器開啟編輯,將對應版本值設定為已有的,因為使用差不多的版本不一定會出錯的,免得所有環境全都得重灌一模一樣的。
3、test_all的問題:
又出現了:error C2220: 警告被視為錯誤 - 沒有生成“object”檔案
回頭再看下網上資料,警告被視為錯誤不一定會有大問題,可如下圖設定系統放鬆檢測。
最後,所有庫再次生成均無問題。
配置python支援
After you have built solution with Python support, in order to use it you have to either:
* set `PythonPath` environment variable to point to `<caffe_root>\Build\x64\Release\pycaffe`, or
* copy folder `<caffe_root>\Build\x64\Release\pycaffe\caffe` under `<python_root>\lib\site-packages`.
配置 PythonPath的方法。
嘗試執行caffe.exe,出現缺失opencv_world310.dll的問題,everything搜尋發現此檔案包含在thirdparty中。回看發現是自己漏了看了README.md中的內容:“Then, add the folder `./windows/thirdparty/bins` to the environment variable `PATH`.”
所以安裝要心細注意。
設定好Path後,重啟vs,將所有工程重新build,執行caffe.exe,
成功!
設定matlab支援。參考這篇部落格新增matlab search path,matlab中輸入pathtool選擇新增幷包含子資料夾。
至此caffe-windows安裝完成。
內容、表述如有不當之處,望批評指正。
參考: