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后的东西,如下图:

winsows10下用ninja编译配置caffe_第1张图片

编译过程十分钟左右,编译完成后会在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.
这个意思就是使用上述参数的话射程shared library可能会是一些测试失败。。。所以修改build_win.cmd文件使得DBUIL_SHARED_LIBS = 0,如下划线部分:

 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.


>> 

测试成功!


你可能感兴趣的:(深度学习)