如何在 Windows 10(GPU) 環境中安裝 Caffe(Convolutional Architecture for Fast Feature Embedding) framework--VS2015+CUDA8.0+Python2.7篇

動機:想在 Windows10(GPU) 平台安裝 Caffe,要如何實作呢?!

準備環境
1.OS: Windows 10 Home (x64)
2.GPU: GeForce RTX 2070 with Max-Q Design
3.CUDA: V8.0.60
4.Python 2.7.16 (Miniconda 2.7 for Windows 64-bit)
5.Conda: 4.7.12
6.Git: 2.25.0.windows.1
7.Microsoft Visual Studio Community 2015: 14.0.25431.01 Update 3

操作步驟
1.安裝 Git 工具: 至 https://git-scm.com/ 下載安裝...
2.下載 caffe-windows 源碼: 根據官網(參攷1.)的安裝指引,是引導到 https://github.com/BVLC/caffe/tree/windows 中,但筆者依步驟安裝時會在 scripts\build_win.cmd 中一直鬼打牆...so,換個角度到 參攷2. 找到另一個 https://github.com/happynear/caffe-windows/tree/ms 再打指令下載,如下:
C:\Users\user> git clone https://github.com/happynear/caffe-windows.git
3.安裝 Visual Studio 2015 with Update 3: 再依照上述的網頁(https://github.com/happynear/caffe-windows)步驟,陸續安裝 Visual Studio Community 2015 with Update 3 (x64, 繁中版)(https://my.visualstudio.com/Downloads?q=visual%20studio%202015&wt.mc_id=o~msft~vscom~older-downloads),請記住是 2015,不要自作聰明的下載 2017 或 2019...否則後果自負!!
4.安裝 CUDA Toolkit 8.0:下載(https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_win10-exe) ,請連帶 Patch 2 (Released Jun 26, 2017) 也一併下載及安裝(乖乖的裝補丁包你執行 caffe程式時不會出問題)
5.安裝 cuDNN v5.1 (Jan 20, 2017): for CUDA 8.0 (https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v5.1/prod_20161129/8.0/cudnn-8.0-windows10-x64-v5.1-zip)
6.安裝 Miniconda 2.7: for Windows 64-bit (https://repo.anaconda.com/miniconda/Miniconda2-latest-Windows-x86_64.exe),別懷疑真的只能安裝 Python 2.7
7.下載 third party libraries: 至網址(https://drive.google.com/file/d/13dbvXmMosxozWbSgJdDdxH62YWqXLjoJ/view?usp=sharing)並設定 環境變數 如下圖:
8.調整 VS2015參數: 複製 copy .\windows\CommonSettings.props.example 到 .\windows\CommonSettings.props,檔案內容太多筆者分享在此 (https://gist.github.com/dvsseed/64c644006496eb6a7314d66108094759)
9.安裝 Intel® Math Kernel Library (Intel® MKL) 2018 Update 2 for Windows: 請至 https://software.intel.com/en-us/mkl 下載安裝
10.執行 VS2015編譯程式: 請至目錄 C:\Users\user\caffe-windows\windows 的 Caffe.sln 檔案點擊兩下mouse,再按 建置方案 (方案組態: Release方案平台: x64),如下圖
  本項目的建置順序,詳述如下:
  • 先將 libcaffe [設定為啟始專案],再執行 [建置]  (需要確認 => 建置: 1 成功)
  • 再到 pycaffe,再執行 [建置]  (需要確認 => 建置: 3 成功),請注意設定 Miniconda2的目錄位置
  • 最後到上圖 方案'Caffe'(17專案),再執行 [建置方案]  (才會 建置: 15 成功)
11.最後,會在目錄 C:\Users\user\caffe-windows\Build\x64\Release 中看到一堆執行檔.exe (筆者分享於此... https://drive.google.com/file/d/1MoZWtoPmEkxgASB92CsmnxYOz1ynWeHN/view?usp=sharing )
12.複製 pycaffe 檔案: 將 C:\Users\dvsse\caffe-windows\Build\x64\Release\pycaffe\caffe 目錄檔案複製至 C:\ProgramData\Miniconda2\Lib\site-packages 目錄內即可:-),此項為 Python2 能執行 Caffe程式 的關鍵!!!



後記:很妙的是,雖然 VS2015 是設定 CUDA 8.0 來編譯,但執行.exe程式時卻會詢問 CUDA 9.0 cudart64_90.dll 在那裡,如下圖
還好筆者的筆電內都有安裝CUDA的各式版本,故直接到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin\cudart64_90.dll 複製到 C:\Users\user\caffe-windows\Build\x64\Release 目錄即可解決!!


心得
1.若在 Ubuntu 環境中安裝 Caffe 是相對(Windows10)容易的,上述步驟夠玩你半天都不會厭倦,但使用者偏好 Windows操作環境,因此,學會安裝是基本功呀!!
2.另外一個心得是安裝最新的不見得是好事,因為 CUDA就不會跟進更新,所以,在此時安裝舊版的反而更能對應 Caffe 不會出錯!!!
3.BTW其實還有一堆雜事設定及bug除錯沒記錄到!!!


參攷
1.Caffe Installation, http://caffe.berkeleyvision.org/installation.html
2.Caffe在windows及ubuntu下多種形式的編譯, https://www.twblogs.net/a/5c658f88bd9eee06ef379011
3.Miniconda installer archive, https://repo.continuum.io/miniconda/
4.如何快糙好猛地在Windows下编译CAFFE并使用其matlab和python接口, https://blog.csdn.net/happynear/article/details/45372231

留言