您的位置:首页 > Web前端

Windows下VS2013编译Caffe完全手册

2017-09-14 11:04 543 查看
本人的操作系统是Win7 64位的,VS2013旗舰版,Matlab2015b,Python是Miniconda2 64-bit (exe installer),当然Python anaconda3应该也可以,我没试过,这个是按照Microsoft/caffe主页上的建议安装的,然后是下载Microsoft/caffe,网址如下:https://github.com/Microsoft/caffe

步骤01仅CPU下编译Caffe:

首先是按照要求配置CommonSettings.props文件,具体方法可参考Microsoft/caffe,即把解压的caffe-master\caffe-master\windows下的CommonSettings.props.example文件去掉最后一个扩展名example变成CommonSettings.props,用vs2013或写字板等打开进行相关配置,如下:



<!--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>

由上面的配置可知,本人仅在利用CPU进行编译,并且不支持Python和Matlab的情况下进行编译。首先打开caffe-master\caffe-master\windows文件夹下的Caffe.sln,第一步先生成libcaffe,如下图所示:



右击libcaffe,然后点击Build即可。我在编译的时候出现如下错误:



在VS2013 底部的Output栏双击错误时,直接打开了 alt_sstream_impl.hpp文件,不用管它,点击保存alt_sstream_impl.hpp,然后重新生成libcaffe即可,编译成功。然后再生成Solution'Caffe'(16 projects),如下图所示:



这一步编译成功后,Windows下VS2013配置Caffe才算完成,按照上述步骤,可以编译Debug下的Caffe。
以上步骤是仅使用CPU,并且在不支持Matlab与Python的情况下进行编译的,几乎没什么编译错误,很简单,下面进行GPU下,且在Matlab与Python支持下进行Caffe编译的,会出现一些编译错误,下面进行详细的配置过程如步骤02 GPU下编译Caffe。

步骤02 GPU下编译Caffe:

如步骤01需要配置CommonSettings.props文件,我进行了如下配置:



<CpuOnlyBuild>false</CpuOnlyBuild>
<UseCuDNN>true</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>




<!-- CuDNN 4 and 5 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>D:\MicroCaffe\Miniconda2</PythonDir>
<LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>
<IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(MatlabSupport)'=='true'">
<MatlabDir>E:\CommonSoftware\Matlab2015b</MatlabDir>
<LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>
<IncludePath>$(MatlabDir)\extern\include;$(MatlabDir)\toolbox\distcomp\gpu\extern\include;$(IncludePath)</IncludePath>
注意:
<CuDnnPath></CuDnnPath>
本人并没有添加任何路径,网上的很多教程此处均添加了路径,本人当初也添加了cudnn的路径,但是在编译libcaffe时还是出现了找不到头文件cudnn.h的错误,无奈在项目libcaffe->properties->VC++ Directories下加入cudnn的解压路径下的include路径,libcaffe编译通过,但是在编译Solution 'Caffe'(16
projects)时出现



Error	9483	error MSB3073: The command ""D:\MicroCaffe\GPUCaffe\caffe\windows\\scripts\BinplaceCudaDependencies.cmd" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin" "D:\MicroCaffe\cuda;D:\MicroCaffe\cuda\include" false true "D:\MicroCaffe\GPUCaffe\caffe\windows\..\Build\x64\Debug\"
:VCEnd" exited with code 1.	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppCommon.targets	132	5	libcaffe
的错误,通过把
<CuDnnPath></CuDnnPath>
内的路径删除,并做如下操作:

本人安装的是CUDA7.5,默认的安装路径(推荐),cudnn为cudnn-7.5-windows7-x64-v5.0-ga.zip,解压后,cuda路径下的文件如下:



将这三个文件拷贝到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\路径下(CUDA7.5默认安装路径),再次编译,又出现了新的错误:
Error	275	error C1083: Cannot open include file: 'gpu/mxGPUArray.h': No such file or directory	D:\MicroCaffe\GPUCaffe\caffe\matlab\+caffe\private\caffe_.cpp	16	1	matcaffe
,即找不到头文件'gpu/mxGPUArray.h',这个是因为该文件换地方了,放在了Matlab的这个路径toolbox\distcomp\gpu\extern\include\gpu下,本人上面的CommonSettings.props文件已作配置:
<IncludePath>$(MatlabDir)\extern\include;$(MatlabDir)\toolbox\distcomp\gpu\extern\include;$(IncludePath)</IncludePath>
应不会出现上述编译错了。再次编译,重新生成,又出现了新的错误:
Error	10022	error LNK1104: cannot open file 'python27_d.lib'	D:\MicroCaffe\GPUCaffe\caffe\windows\pycaffe\LINK	pycaffe
这个网上普遍的解决方法是在Pyton.h头文件添加宏定义,本人试过,均无法解决问题,至少在针对编译Caffe问题上,此路不通,最终得解决方法是:

1. 修改 pyconfig.h

1.1 将 

#ifdef _DEBUG
# define Py_DEBUG
#endif


改为

#ifdef _DEBUG
//# define Py_DEBUG
#endif


1.2 将

# ifdef _DEBUG
# pragma comment(lib,"python27_d.lib")
# else
# pragma comment(lib,"python27.lib")
# endif /* _DEBUG */


改为

# ifdef _DEBUG
# pragma comment(lib,"python27.lib")
# else
# pragma comment(lib,"python27.lib")
# endif /* _DEBUG */


2. 修改object.h



#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
#define Py_TRACE_REFS
#endif


改为

#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
// #define Py_TRACE_REFS
#endif


按照上述方法修改后,编译通过,如下图所示:
Debug下编译libcaffe及Solution'Caffe':





Release下编译libcaffe及Solution'Caffe':





为了验证是否编译成功,可在Python下验证一下,如下:



至此,Windows下利用VS2013编译Caffe完成。

参考:http://blog.csdn.net/u011501388/article/details/77968399 http://www.cnblogs.com/ziyu-trip/p/7353704.html http://blog.csdn.net/hereiskxm/article/details/53517516 http://www.jianshu.com/p/3cd99857d460 http://bbs.csdn.net/topics/392019355?locationNum=6 http://blog.csdn.net/longji/article/details/65449803
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Windows Caffe