winsows10下用ninja编译配置caffe
出于好奇自己想在windows上配置一下caffe環境,于是在網上找了一些教程。發現都和caffe官網上的說法有些不同,于是抱著作死的心態去按照官網上的做法走了一遍。自己也是新手,還有許多不明白的地方,隨手mark一下。
參考:https://github.com/BVLC/caffe/tree/windows
Requirements
- Visual Studio 2013 or 2015
- CMake?3.4 or higher (Visual Studio and?Ninja?generators are supported)
Optional Dependencies
- Python for the pycaffe interface. Anaconda Python 2.7 or 3.5 x64 (or Miniconda)
- Matlab for the matcaffe interface.
- CUDA 7.5 or 8.0 (use CUDA 8 if using Visual Studio 2015)
- cuDNN v5
We assume that?cmake.exe?and?python.exe?are on your?PATH.
這是官網上的說明。
1.首先就是安裝vs2015,這個就不必細說。
2.安裝anaconda和一些必要的包:
https://www.continuum.io/downloads/? ?這是下載地址,下載之后雙擊安裝程序逐步安裝,記得勾選添加anaconda至環境變量這個選項,因為anaconda自帶python,我下載的是python2.7版本的。安裝了anaconda之后無需再安裝python了。它的方便之處就是自己帶了很多python的庫還有spyder這個類似matlab的python集成開發環境。
然后就可打開cmd運行:
conda install --yes numpy scipy matplotlib scikit-image pip six conda install --yes --channel willyd protobuf==3.1.0 這主要是為了在python下使用caffe安裝的依賴包。
3.安裝cuda8.0以及cuDnn v5.1:
如果想使用GPU加速,則需要安裝CUDA及cuDNN,這里如果你是nvidia的顯卡且cuda的計算能力評分在3.0及以上可以考慮安裝cuda:
https://developer.nvidia.com/cuda-downloads
評分詳見:https://developer.nvidia.com/cuda-gpus
下載cuda驅動之后雙擊安裝,選擇自定義,安裝cuda套件,我并沒有選擇更新顯卡驅動和安裝experience。詳情參考了博文:
http://blog.csdn.net/tina_ttl/article/details/51388242
這里注意安裝的過程中有個添加path環境變量的選項要選擇,安裝完成后就不用配置自己的path環境變量了。
安裝完cuda后緊接著安裝cuDNN v5.1.
安裝cuDnn就是下載文件并拷貝文件到CUDA的相關目錄里面。
下載cuDNN:https://developer.nvidia.com/rdp/form/cudnn-download-survey? ? ? 這個要注冊之后才能下載,下載后解壓,會出現一個名為cuda的文件夾,下面有三個子文件夾:bin,include,lib ? ?分別把這三個子文件夾里面的內容拷貝至CUDA安裝目錄下對應的bin,include,lib ? 文件夾下面,例如我的是C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0
4.安裝CMAKE3.4以及ninja
安裝cmake3.4,直接去官網https://cmake.org/download/上下載對應的.msi文件安裝即可。注意將cmake添加到path環境變量。
安裝ninja,到https://github.com/ninja-build/ninja/releases上面去下載ninja-win.zip以及source code(zip),分別解壓得到ninja.exe,和一個ninja-1.7.2的文件夾,將ninja.exe復制到ninja-1.7.2文件夾中,雙擊ninja.exe文件,會生成一個build文件夾(沒看懂),這時可以將ninja-1.7.2這個文件夾路徑(也就是你的ninja路徑)添加到你的path環境變量中,例如我的是:D:\Programs\Projects-caffe\ninja-1.7.2
這樣ninja就可以用了。
至于官網上的安裝方法:
> conda config --add channels conda-forge > conda install ninja --yes 我沒有試過,不過應該是可以的。
5.編譯
至此,所有準備工作完成,進行編譯。我是編譯python環境下的caffe,有需要的話可以編譯matlab的caffe,可以自行修改caffe\scripts\build_win.cmd文件進行編譯,當然修改此文件可以使用vs2015編譯,官網的意思時使用ninja更快更強。
The fastest method to get started with caffe on Windows is by executing the following commands in a?cmd?prompt (we use?C:\Projects?as a root folder for the remainder of the instructions):
C:\Projects> git clone https://github.com/BVLC/caffe.git C:\Projects> cd caffe C:\Projects\caffe> git checkout windows :: Edit any of the options inside build_win.cmd to suit your needs C:\Projects\caffe> scripts\build_win.cmd如需修改配置,使用記事本編輯build_win.cmd文件,修改的地方為else后的東西,如下圖:
編譯過程十分鐘左右,編譯完成后會在caffe目錄里生成build文件夾。
然后將caffe\python\caffe文件夾復制到path\to\Anaconda2\Lib\site-packages下(例如D:\Programs\Anaconda2\Lib\site-packages)。
?官網說法:In order to use the python interface you need to either add the?C:\Projects\caffe\python?folder to your python path of copy the?C:\Projects\caffe\python\caffe?folder to your?site_packages?folder.
也可以新建一個環境變量PYTHONPATH
變量值為path\to\caffe\python(例如:?C:\Projects\caffe\python)
6.測試
run cmd
C:\Users\ThomasZhao>python
Python 2.7.13 |Anaconda custom (64-bit)| (default, Dec 19 2016, 13:29:36) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import caffe
>>>
如果沒有報錯,說明caffe安裝成功。
補充:試著編譯了一下matlab的caffe
發現運行caffe.run_tests()時:
Using the MATLAB interface
Follow the above procedure and use?-DBUILD_matlab=ON. Change your current directory in MATLAB to?C:\Projects\caffe\matlab?and run the following command to run the tests:
>> caffe.run_tests()出現了錯誤:
Invalid MEX-file..........................................找不到指定模塊
解決方案,設置path環境變量:D:\Programs\Projects-caffe\caffe\build\install\bin
然后重啟matlab,再次運行:
caffe.run_tests()
Cleared 0 solvers and 0 stand-alone nets
Running caffe.test.test_net
.....
Done caffe.test.test_net
__________
Unexpected Standard exception from MEX file.
What() is:CHECK failed: file != NULL:
..
Error in caffe.get_solver (line 7)
pSolver = caffe_('get_solver', solver_file);
Error in caffe.Solver (line 21)
? ? ? ? self = caffe.get_solver(varargin{:});
Error in caffe.test.test_solver (line 22)
? ? ? self.solver = caffe.Solver(solver_file);
Error in caffe.run_tests (line 12)
results = [...
今天又找了一下原因,發現應該時編譯的時候選擇了參數:
DBUILD_SHARED_LIBS=ON
而官網教程上的說法是
CMake can be used to build a shared library instead of the default static library. To do so follow the above procedure and use?-DBUILD_SHARED_LIBS=ON. Please note however, that some tests (more specifically the solver related tests) will fail since both the test exectuable and caffe library do not share static objects contained in the protobuf library.
- Shared library (DLL) build will have failing tests.
?else (
? ? :: Change the settings here to match your setup
? ? :: Change MSVC_VERSION to 12 to use VS 2013
? ? if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14
? ? :: Change to 1 to use Ninja generator (builds much faster)
? ? if NOT DEFINED WITH_NINJA set WITH_NINJA=1
? ? :: Change to 1 to build caffe without CUDA support
? ? if NOT DEFINED CPU_ONLY set CPU_ONLY=0
? ? :: Change to Debug to build Debug. This is only relevant for the Ninja generator the Visual Studio generator will generate both Debug and Release configs
? ? if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
? ? :: Set to 1 to use NCCL
? ? if NOT DEFINED USE_NCCL set USE_NCCL=0
? ? :: Change to 1 to build a caffe.dll
? ? if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=0
然后再編譯,將caffe目錄下\build\install\bin復制到\matlab\+caffe\下(也可以將path\to\build\install\bin添加到PATH環境變量)
再打開matlab切換當前路徑到caffe目錄下\matlab,
命令行輸入:caffe.run_tests():
>> caffe.run_tests()
Cleared 0 solvers and 0 stand-alone nets
Running caffe.test.test_net
.....
Done caffe.test.test_net
__________
Running caffe.test.test_solver
.
Done caffe.test.test_solver
__________
Running caffe.test.test_io
.
Done caffe.test.test_io
__________
Cleared 0 solvers and 0 stand-alone nets
ans =?
? 1x7 TestResult array with properties:
? ? Name
? ? Passed
? ? Failed
? ? Incomplete
? ? Duration
Totals:
? ?7 Passed, 0 Failed, 0 Incomplete.
? ?1.0383 seconds testing time.
>>?
測試成功!
總結
以上是生活随笔為你收集整理的winsows10下用ninja编译配置caffe的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows 10 Visual S
- 下一篇: 训练FCN时遇到的问题总结