Bootstrap

caffe学习(1)------windows下基于GPU配置

最近准备用caffe做图片的分类,可配置caffe就让我折腾了大半个月,一直配置不成功,最后还是参考官网的tutorial才配置成功,于是决定把配置的过程写下来,如果后面有朋友配置caffe遇到什么问题,还可以参考参考。

首先贴出官方的配置caffe的工程:https://github.com/BVLC/caffe/tree/windows,注意,这可是基于Windows系统的,因为我的电脑是win7的,打开这个网页下面有个readme文件,下面写的就是配置的要求和配置的过程,是全英文的,对于编程人员来说,看点英文是必须的,所以,我就硬着头皮一步一步配置了,黄天不负有心人,终于配置成功。下面,我准备讲一讲我的具体配置步骤。(ps:之前我是一直按照happynear的版本配置的:http://blog.csdn.net/happynear/article/details/45372231,但是一直配置不成功,编译生成的caffe.exe一打开就报错,也不知道是什么原因,不过happynear的版本也讲的很详细,大赞,关键是中文啊,还是有很多人配置成功了的。)

具体配置过程:

1、打开官方链接,下载Windows-master并解压保存在你的电脑中,如:E:\windows_caffe

2、按照官方的要求,必须要装Visual Studio 2013,

进入你刚才保存windows_caffe的文件夹下,复制 .\windows\CommonSettings.props.example,在原地黏贴,并重命名 CommonSettings.props,说白了,这一步骤就是修改工程的属性文件,里面CUDA和cuDNN库是默认被要求用到的,cuDNN和CPU_ONLY两个只能二选一,Python和matlab是被默认false的,也就是不用,你可以通过修改这个属性文件来个性化设置。

3、安装CUDA7.5:https://developer.nvidia.com/cuda-toolkit,如果不想安装,或者不能安装(没有显卡,或是显卡太旧),也可以用CUP_ONLY,只需在工程的属性文件里面修改一下即可,设置 CpuOnlyBuild为 true 并设置 UseCuDNNfalse.

4、安装cuDNN,https://developer.nvidia.com/cudnn,这个网站需要注册才能下载,我安装的是cuDNN v4,之前我在配置的时候安装是cuDNN v5,因为看到cuDNN的官网说v5和CUDA7.5对应,我想,这是不是更好呢,结果在编译caffe的时候出错了,说cudnn.h少了参数,没办法,还是选择了v4,其实caffe官网也说了,用v3或v4。

5、安装Python,(不想用Python可以不用安装)官网也给出了链接,直接下载:http://conda.pydata.org/miniconda.html,按照官网要求,我下载的是 Miniconda 2.7 64-bit Windows,通过安装Python就可以把Python的库路径加入到系统环境变量中去,在命令提示符窗口运行以下命令:

conda install --yes numpy scipy matplotlib scikit-image pip
pip install protobuf
这条命令应该是对Python提高一些性能的,安装了很多东西,时间有点久,耐心等待。。。。
6、安装matlab,(不想用matlab可以不用安装)因为这个我原本电脑就有安装,就不讲了。
7、把这些都安装好了,就可以修改工程的属性文件了。下面是我修改的属性文件,给出来做参考。
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ImportGroup Label="PropertySheets" />
    <PropertyGroup Label="UserMacros">
        <BuildDir>$(SolutionDir)..\Build</BuildDir>
        <!--NOTE: CpuOnlyBuild and UseCuDNN flags can't be set at the same time.-->
        <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>
        <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>E:\cudnnlib\</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>E:\Miniconda2\</PythonDir>
        <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>
        <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>
    </PropertyGroup>
    <PropertyGroup Condition="'$(MatlabSupport)'=='true'">
        <MatlabDir>E:\Program Files\MATLAB\R2014a</MatlabDir>
        <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>
        <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath>
    </PropertyGroup>
    <ItemDefinitionGroup Condition="'$(CpuOnlyBuild)'=='true'">
        <ClCompile>
            <PreprocessorDefinitions>CPU_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        </ClCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(UseCuDNN)'=='true'">
        <ClCompile>
            <PreprocessorDefinitions>USE_CUDNN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        </ClCompile>
        <CudaCompile>
            <Defines>USE_CUDNN</Defines>
        </CudaCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(PythonSupport)'=='true'">
        <ClCompile>
            <PreprocessorDefinitions>WITH_PYTHON_LAYER;BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        </ClCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(MatlabSupport)'=='true'">
        <ClCompile>
            <PreprocessorDefinitions>MATLAB_MEX_FILE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        </ClCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup>
        <ClCompile>
            <MinimalRebuild>false</MinimalRebuild>
            <MultiProcessorCompilation>true</MultiProcessorCompilation>
            <PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;USE_OPENCV;USE_LEVELDB;USE_LMDB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
            <TreatWarningAsError>false</TreatWarningAsError>
        </ClCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
        <ClCompile>
            <Optimization>Full</Optimization>
            <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
            <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
            <FunctionLevelLinking>true</FunctionLevelLinking>
        </ClCompile>
        <Link>
            <EnableCOMDATFolding>true</EnableCOMDATFolding>
            <GenerateDebugInformation>true</GenerateDebugInformation>
            <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
            <OptimizeReferences>true</OptimizeReferences>
        </Link>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
        <ClCompile>
            <Optimization>Disabled</Optimization>
            <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
            <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
        </ClCompile>
        <Link>
            <GenerateDebugInformation>true</GenerateDebugInformation>
        </Link>
    </ItemDefinitionGroup>
</Project>


另外特别注意:

(1)<CudaArchitecture>compute_35,sm_35;compute_52,sm_52</CudaArchitecture>
这一行,是CUDA的计算能力,必须和你的电脑显卡想匹配,不然会报错。查看显卡GPU CUDA Capability的版本,方法: 
step1: 右键“计算机”—>“属性”—>“设备管理器”—>“显示适配器”
step2: 根据显卡型号,在https://developer.nvidia.com/cuda-gpus查看CUDA Capability的版本。要查看显卡CUDA Capability版本的原因是因为:buildVS2013项目默认是开启cudnn的,而CUDNN要求GPU CUDA Capability 不低于3.0,如果CUDA Capability 版本低于3.0,在编译过程中,不关闭cudnn,则会出现类似问题:
caffe make runtest error(core dumped)Check failed: status == CUDNN_STATUS_SUCCESS (6 vs. 0)
(2)<TreatWarningAsError>false</TreatWarningAsError>
这一行,如果不改,就会在编译的时候报错,error C2220: 警告被视为错误 - 没有生成“object”文件 (..\..\src\caffe\util\math_functions.cpp)
8、改完工程属性配置文件之后,就可以编译caffe了,就在这个属性配置文件同级目录下有个Caffe.sln,用Visual Studio 2013打开即可,它是默认只对libcaffe进行编译的,你想全部编译需要在调试-->设置启动项目-->通用属性-->启动项目,把单启动项目改为多启动项目,然后再勾选需要启动的项目。改解决方案配置为Release,平台为x64,在点击编译之后,程序会通过NuGet自动加载第三方库(3rdparty),时间会很长,然后就是大约半个小时的编译,最后会在E:\caffe_windows\Build\x64\Release\目录下生成caffe.exe和其他工程的.exe文件。
9、在生成各种.exe文件之后,需要使用Python和matlab的,别忘了把<caffe_root>\Build\x64\Release\pycaffe\caffe文件夹拷贝到<python_root>\lib\site-packages文件夹下。对于matlab,就把生成的matcaffe文件夹添加到matlab的搜索路径里,并把<caffe_root>\Build\x64\Release文件夹添加到你的系统环境路径。
好了,到这里就把caffe配置完成了,之后需了解caffe的框架等等,官网有很多教程,可以多看看。
参考文献:https://github.com/BVLC/caffe/tree/windows

;