通过朋友指导,终于成功在windows7上成功编译了caffe,这里将编译过程记录python
若是不须要cuda版本的caffe,本步跳过git
具体安装方法见: 安装visual studio 2013github
若是不须要cuda版本的caffe,本部分能够跳过windows
安装方法与theano安装(二)windows安装visual studio 2010及cuda中的cuda安装方法一致,只不过是版本号不一样而已网络
若是你的本机没有安装Python,而且你须要配置pycaffe,那么,请按照win7系统安装python,并按照Pycharm集成开发环境安装python工具
安装完python后,再安装一下protobuf,由于后面编译pycaffe时有须要:visual-studio
pip install protobuf
若是你的本机没有安装matlab ,而且你须要配置matcaffe,那么,请安装matlab,具体安装方法请自行查阅测试
在github上下载的window caffe把呢你自带一个配置文件的例子,在进行编译以前,须要先更改该文件内容ui
这里假设caffe-windows被解压到了以下的文件夹中
spa
将该文件夹(即.\windows)下的CommonSettings.props.example文件复制一份儿,并将复制的文件命名为CommonSettings.props
-
caffe-windows自带的配置文件CommonSettings.props.example中与咱们后面要进行的编译密切相关的就是以下部分
例子中全部配置参数都是默认值
CpuOnlyBuild
:是否使用CPU
UseCuDNN
:是否使用CuDNN
<CuDnnPath></CuDnnPath>
赋值,即下载的CuDNN的路径CudaVersion
:CUDA的版本
PythonSupport
:是否支持python
<PythonDir>C:\Miniconda2\</PythonDir>
赋值
D:\program file\Anaconda2
,那么,应该令 <PythonDir>D:\program file\Anaconda2</PythonDir>
MatlabSupport
:是否支持matlab
<MatlabDir>C:\Program Files\MATLAB\R2014b</MatlabDir>
进行赋值:
D:\Program Files\MATLAB\R2014b
,那么,<MatlabDir>D:\Program Files\MATLAB\R2014b</MatlabDir>
配置文件部分设置以下:
<PropertyGroup Label="UserMacros"> <BuildDir>$(SolutionDir)..\Build</BuildDir> <!--NOTE: CpuOnlyBuild and UseCuDNN flags can't be set at the same time.--> <CpuOnlyBuild>true</CpuOnlyBuild> <UseCuDNN>false</UseCuDNN> <CudaVersion>7.5</CudaVersion> <!-- NOTE: If Python support is enabled, PythonDir (below) needs to be set to the root of your Python installation. If your Python installation does not contain debug libraries, debug build will not work. --> <PythonSupport>false</PythonSupport> <!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be set to the root of your Matlab installation. --> <MatlabSupport>false</MatlabSupport> <CudaDependencies></CudaDependencies> <!-- Set CUDA architecture suitable for your GPU. Setting proper architecture is important to mimize your run and compile time. --> <CudaArchitecture>compute_35,sm_35;compute_52,sm_52</CudaArchitecture> <!-- CuDNN 3 and 4 are supported --> <CuDnnPath></CuDnnPath> <ScriptsDir>$(SolutionDir)\scripts</ScriptsDir> </PropertyGroup> <PropertyGroup Condition="'$(CpuOnlyBuild)'=='false'"> <CudaDependencies>cublas.lib;cuda.lib;curand.lib;cudart.lib</CudaDependencies> </PropertyGroup> <PropertyGroup Condition="'$(UseCuDNN)'=='true'"> <CudaDependencies>cudnn.lib;$(CudaDependencies)</CudaDependencies> </PropertyGroup> <PropertyGroup Condition="'$(UseCuDNN)'=='true' And $(CuDnnPath)!=''"> <LibraryPath>$(CuDnnPath)\cuda\lib\x64;$(LibraryPath)</LibraryPath> <IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup> <OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir> <IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir> </PropertyGroup> <PropertyGroup> <LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath> <IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup Condition="'$(PythonSupport)'=='true'"> <PythonDir>C:\Miniconda2\</PythonDir> <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath> <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup Condition="'$(MatlabSupport)'=='true'"> <MatlabDir>C:\Program Files\MATLAB\R2014b</MatlabDir> <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath> <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath> </PropertyGroup>
配置文件部分设置以下:
<PropertyGroup Label="UserMacros"> <BuildDir>$(SolutionDir)..\Build</BuildDir> <!--NOTE: CpuOnlyBuild and UseCuDNN flags can't be set at the same time.--> <CpuOnlyBuild>false</CpuOnlyBuild> <UseCuDNN>ture</UseCuDNN> <CudaVersion>7.5</CudaVersion> <!-- NOTE: If Python support is enabled, PythonDir (below) needs to be set to the root of your Python installation. If your Python installation does not contain debug libraries, debug build will not work. --> <PythonSupport>true</PythonSupport> <!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be set to the root of your Matlab installation. --> <MatlabSupport>true</MatlabSupport> <CudaDependencies></CudaDependencies> <!-- Set CUDA architecture suitable for your GPU. Setting proper architecture is important to mimize your run and compile time. --> <CudaArchitecture>compute_35,sm_35;compute_52,sm_52</CudaArchitecture> <!-- CuDNN 3 and 4 are supported --> <CuDnnPath>D:\software\caffe-vs\cuda-cuDNN</CuDnnPath> <ScriptsDir>$(SolutionDir)\scripts</ScriptsDir> </PropertyGroup> <PropertyGroup Condition="'$(CpuOnlyBuild)'=='false'"> <CudaDependencies>cublas.lib;cuda.lib;curand.lib;cudart.lib</CudaDependencies> </PropertyGroup> <PropertyGroup Condition="'$(UseCuDNN)'=='true'"> <CudaDependencies>cudnn.lib;$(CudaDependencies)</CudaDependencies> </PropertyGroup> <PropertyGroup Condition="'$(UseCuDNN)'=='true' And $(CuDnnPath)!=''"> <LibraryPath>$(CuDnnPath)\cuda\lib\x64;$(LibraryPath)</LibraryPath> <IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup> <OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir> <IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir> </PropertyGroup> <PropertyGroup> <LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath> <IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup Condition="'$(PythonSupport)'=='true'"> <PythonDir>D:\File Program\Anaconda\</PythonDir> <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath> <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup Condition="'$(MatlabSupport)'=='true'"> <MatlabDir>D:\File Program\Matlab\</MatlabDir> <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath> <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath> </PropertyGroup>
注:事实上,对cuDNN路径的配置有两种方案:
- 方案1:将cuDNN v4的压缩包内的bin、include、lib中的文件分别放置在%CUDA_PATH% (即cuda的安装路径)的相应目录中
- 方案2:将 .\windows\CommonSettings.props文件中的CuDnnPath设置为该解压缩路径,例如,将cuDNN解压后文件放置在D:\software\caffe-vs\cuda-cuDNN
中,那么,在CommonSettings.props中,令<CuDnnPath>D:\software\caffe-vs\cuda-cuDNN\</CuDnnPath>
,即上面提到的方法
利用VS2013打开名称为Caffe的解决方案
首先编译libcaffe项目(其余的项目依赖于libcaffe项目)
右键libcaffe,选择生成,开始编译libcaffe
…
…
还原NuGet包,即从网络下载编译须要的各类依赖包
通过一段时间等待后,编译成功
这里有两个须要注意的地方:
(1)编译过程当中,可能会出现错误
error C2220: 警告被视为错误 - 没有生成“object”文件
错误的缘由是编译过程当中出现了警告,而致使编译没法进行,此时,须要进行以下修改:
即:设置项目属性,不要将警告视为错误
右键->项目属性->C/C++->常规->将警告视为错误 设为否
另外,有一次在另一个机器上出现了一样的问题,但按照上述方法却未解决,是由于文件的代码页为英文,而个人系统中的代码页为中文,解决方案:利用VS打开出错的文件->从新保存->重启vs->从新编译libcaffe->问题解决。
(2)caffe编译过程当中,须要一些依赖包,在有网络的状况下,当编译第一个项目libcaffe时,程序会自动从网路下载这些依赖包,下载到与caffe-windows文件夹同级的NugetPackages文件夹中,下图所示即为程序自动下载的依赖包
但在没有网络状况下,须要事先将这些依赖包下载好,并将它们的路径在vs的管理NuGet程序包中进行设置:工具->选项->NuGet Package Manager->程序包源->利用右侧的加号添加本地的package
…
手动添加这些package地址后,还须要手动对这些NuGet Package进行安装
(3)另外,第二次编译过程出现以下错误
发现是下载的NegetPackages\glog.0.3.3.0下载缺乏两个文件:
glog.overlay-x64_v120_Release_dynamic.0.3.3.0
glog.overlay-x64_v120_Debug_dynamic.0.3.3.0
一样,在caffe项目处右键生成
直接在项目pycaffe
右键,点击生成便可
直接在项目matcaffe
右键,点击生成便可
第二次在编译matcaffe时,出现以下错误:
上面这个问题解决后,又出现了另一个错误
\extern\lib\win64\microsoft
添加到matcaffe项目->连接器->常规->附加目录中,而后再次编译,成功;这是由于编译程序没法找到matlab的相关静态连接库的缘由;后来通过仔细查询,发现,原来是由于配置文件CommonSettings中的matlabDir不当心填写错误了,才会致使上面两个问题!将此处改正,上面2个问题成功解决,matcaffe编译成功
<PropertyGroup Condition="'$(MatlabSupport)'=='true'"> <MatlabDir>D:\Program Files\MATLAB\R2014b</MatlabDir> <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath> <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath> </PropertyGroup>
接下来,再依次编译其余项目,一样的方法
classification
:用来分类compute_image_mean
:计算均值convert_cifar_dat
:对cifat数据集进行转换convert_mnist_data
:对mnist数据集进行转换为了测试编译好的caffe可否正常使用,须要进行测试,主要利用mnist数据集进行分类问题的测试
(1)下载测试数据集
(2)修改配置文件
进入文件夹D:\software\caffe-vs\caffe-windows\examples\mnist
,即examples路径下的mnist文件夹
① 打开lenet_solver.prototxt
设置网络配置文件路径,这里使用了绝对路径,相对路径尚未尝试
② 打开lenet_train_test.prototxt
(3)运行caffe程序,具体地
① 打开cmd命令行,cd到caffe-windows的.\Build\x64\Debug
路径下,在该路径下,能够直接执行caffe.exe
D:\software\caffe-vs\caffe-windows\Build\x64\Debug
或者,为了方便,能够将caffe的路径添加到环境变量Path中,这样,cmd就能够直接识别caffe命令了
② 运行caffe
caffe train -solver lenet_solver.prototxt
注意,由于上一步已经将D:\software\caffe-vs\caffe-windows\Build\x64\Debug
路径添加到环境变量PATH中了,因此,这里能够直接使用caffe.exe
下面两步主要配置cuDnn、python和matlab,具体配置时,应该在4.5以前进行
若是已经编译好pycaffe,那么,为了可以在python使用caffe,还须要在python中进行一些相关配置
若是已经编译好matcaffe,那么,为了可以在matlab使用caffe,还须要在matlab中进行一些相关配置
添加环境变量
<caffe_root>\Build\x64\Release\matcaffe
路径添加到matlab的搜索路径中<caffe_root>\Build\x64\Release
路径添加到环境变量PATH中After you have built solution with Matlab support, in order to use it you have to:
add the generated matcaffe folder to Matlab search path, and
add \Build\x64\Release to your system path.
最后来张图