winsows10下用ninja编译配置caffe

本文详细介绍如何在Windows环境下配置Caffe深度学习框架,包括安装VS2015、Anaconda、CUDA、cuDNN等必备组件,以及CMake和Ninja的安装步骤。最后通过测试验证安装成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出于好奇自己想在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.youkuaiyun.com/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.
这个意思就是使用上述参数的话射程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.


>> 

测试成功!

在Windows系统上安装Caffe并确保其与CUDA、cuDNN和Python接口兼容的过程涉及多个复杂的步骤。为了帮助你顺利完成整个安装过程,建议参阅《Windows Caffe安装指南:从零开始到编译完成》。这份指南将提供详细的操作步骤和解决方案,帮助你应对安装过程中可能遇到的问题。 参考资源链接:[Windows Caffe安装指南:从零开始到编译完成](https://wenku.youkuaiyun.com/doc/6412b5abbe7fbd1778d43f8a?spm=1055.2569.3001.10343) 首先,你需要安装所有必要的依赖软件。确保Visual Studio版本符合Caffe的要求,CUDA和cuDNN也需要正确安装配置。Python环境同样需要被配置,以便Caffe能够使用Python接口进行深度学习模型的训练和部署。 在安装好所有依赖后,你可以开始编译Caffe。以下是编译的基本步骤: 1. 使用Git克隆Caffe的源代码仓库到本地目录。 2. 进入Caffe源代码目录,并切换到适合Windows系统的分支。 3. 根据你的系统环境修改构建脚本`build_win.cmd`,配置编译选项,包括选择正确的MSVC版本、是否使用NINJA构建系统、是否只使用CPU进行构建,以及是否需要构建Python接口。 4. 执行修改后的构建脚本开始编译过程。这可能需要一些时间,具体取决于你的系统性能。 5. 编译完成后,检查Caffe是否正确安装,并测试其功能以确保一切运行正常。 在编译过程中,你可能会遇到各种问题,例如依赖库版本不兼容、路径设置错误等。在这些情况下,可以参考Caffe官方文档或访问相关技术社区寻求帮助。如果你希望进一步了解Caffe的使用方法或者希望深入理解其内部工作原理,继续阅读《Windows Caffe安装指南:从零开始到编译完成》中的进阶部分将会非常有益。 总之,编译安装Caffe虽然复杂,但通过系统地学习和实践,你可以成功搭建自己的深度学习环境。此外,为了更好地理解和使用Caffe,建议熟悉深度学习的基础知识和Caffe的架构设计,这将有助于你更好地掌握这个强大的工具。 参考资源链接:[Windows Caffe安装指南:从零开始到编译完成](https://wenku.youkuaiyun.com/doc/6412b5abbe7fbd1778d43f8a?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值