深度学习之Caffe环境搭建

来源:互联网 发布:深入浅出mysql豆瓣 编辑:程序博客网 时间:2024/06/13 13:28

window10(64位)+VS2013+CUDA+CUDNN+caffe

  1. 下载准备
    caffe : 下载地址:http://pan.baidu.com/s/1gfBgJtT
    CUDA8.0: 下载地址:http://pan.baidu.com/s/1b9EHRo
    CUDNN:下载地址:http://pan.baidu.com/s/1dFp6iDb

  2. 配置过程
    2.1 caff配置过程
    进入你刚才保存windows_caffe的文件夹下,进入 windows\CommonSettings.props.example,并重命名为 CommonSettings.props,说白了,这一步骤就是修改工程的属性文件。
    属性文件的修改

<?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>
<CudaVersion>8.0</CudaVersion>中根据cuda版本进行更改。

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)

3)里面CUDA和cuDNN库是默认被要求用到的,cuDNN和CPU_ONLY两个只能二选一,Python和matlab是被默认false的,也就是不用,你可以通过修改这个属性文件来个性化设置。

4)改完工程属性配置文件之后,就可以编译caffe了,就在这个属性配置文件同级目录下有个Caffe.sln,用Visual Studio 2013打开即可,它是默认只对libcaffe进行编译的,解决方案配置默认为Dubeg,如果你想全部编译需要在“解决方案—-右键(设置启动项目)—-通用属性—启动项目”,把单启动项目改为多启动项目,然后再勾选需要启动的项目。改解决方案配置为Release,平台为x64,在点击编译之后,程序会通过NuGet自动加载第三方库(3rdparty),时间会很长,然后就是大约半个小时的编译,最后会在\caffe_windows\Build\x64\Release\目录下生成caffe.exe和其他工程的.exe文件。
注意:编译Caffe.sln程序时,要把“包含目录”和“库目录”及“链接器(或CUDA Linker)—附加包含目录(Additional Dependencies)”按照下面配置cuda和cudnn的方式配置完成。
- 2.2 cuda配置过程
1.直接双击exe文件即可
这里写图片描述
2.弹出的对话框可以选择自己的路径,也可以直接OK,此文件是临时文件,安装完成会自动删除。
这里写图片描述

3.接下来就像正常的软件安装一样就可以了,安装的时候选择自定义,把所有选项都打上勾;只是最后的ToolKit等的安装路径的时候,一定要自己选择安装路径,可以放在其他盘,不要默认。我电脑上已安装就不在重复了,8.0和此图差不多,Samples(sdk存放地)一个文件夹如“Cuda\SamplesCuda”,另外两个Toolkit和documentation可以放在一个同一个文件夹下,如“Cuda\ToolkitCuda”
这里写图片描述
4.下一步安装就行了。至此,cuda的安装就搞定了。

5.CUDA环境的配置:
接下来配置cuda的环境变量,默认安装好后,他会自动帮你设置好2个环境变量(CUDA_PATH和CUDA_PATH_V8_0).自己添加还需要添加以下几个环境变量,方便日后配置vs使用:
CUDA_BIN_PATH  %CUDA_PATH%\bin
CUDA_LIB_PATH  %CUDA_PATH%\lib\x64
CUDA_SDK_BIN  %CUDA_SDK_PATH%\bin\x64
CUDA_SDK_LIB  %CUDA_SDK_PATH%\common\lib\x64
CUDA_SDK_PATH  C:\cuda\cudasdk\common(自己安装SDK的地方,即”Cuda\SamplesCuda”中的common)
至此,环境变量配置成功。 重新启动计算机以使环境变量生效
6.检验安装成功与否
这个步骤我们用到两个东西:deviceQuery.exe
启动cmd-进入CUDASDK安装文件件(即”Cuda\SamplesCuda\bin\win64\Release”中)-》bin-》Release,直接执行deviceQuery.exe(要是没找到其他地方应该也有,可以拷贝到此文件夹下)
这里写图片描述
这里写图片描述
得到以上信息。如果两个Result=PASS说明安装都通过了。如果Result=Fail 表明不成功。
7.VS项目检测
打开VS2013,并建立一个空的win32控制台项目,附加选项那里请把”空项目”打钩:
这里写图片描述
这里写图片描述
右键源文件 -> 添加 -> 新建项 如下图所示:
这里写图片描述
在打开的对话框中选择新建一个CUDA格式的源文件
这里写图片描述
右键工程 -> 生成自定义 如下图所示:
这里写图片描述
在弹出的对话框中勾选”CUDA 8.0 *“选项
这里写图片描述
右键项目 -> 属性 -> 配置属性 -> VC++目录,添加以下两个包含目录和库目录:
这里写图片描述
包含目录
E:\Tools\cuda\CudaToolKit\include
E:\Tools\cuda\CudaSamples\common\inc
库目录
E:\Tools\cuda\CudaSamples\common\lib\x64
E:\Tools\cuda\CudaToolKit\lib\x64

右键项目 -> 属性 -> 配置属性 ->连接器 -> 常规 -> 附加库目录,添加以下目录:
这里写图片描述
$(CUDA_PATH_V8_0)\lib\$(Platform)

右键项目 -> 属性 -> 配置属性 ->连接器 -> 输入 -> 附加依赖项,添加以下库:
cublas.lib
cublas_device.lib
cuda.lib
cudadevrt.lib
cudart.lib
cudart_static.lib
cufft.lib
cufftw.lib
curand.lib
cusparse.lib
nppc.lib
nppi.lib
npps.lib
nvblas.lib
nvcuvid.lib
OpenCL.lib

右键项目 -> 属性,如下图所示:
这里写图片描述
将前面改为 否 ,项类型设置为 CUDA C/C++
这里写图片描述
打开配置管理器,如下图所示:
这里写图片描述
点击 新建,如下图所示:
这里写图片描述
选择 X64 平台:
这里写图片描述

好了,至此平台已经完全搭建完毕,可用以下代码进行测试:
把下面的代码粘贴到cu文件中,运行即可:

#include<stdio.h>#include<iostream>#include<cuda_runtime.h>__global__ void Add(int a, int b, int *c){    *c = a + b;}int main(){    int c;    int *devc;    cudaError_t err = cudaSuccess;    err = cudaMalloc((void **)&devc, sizeof(int));    if (err != cudaSuccess)    {        fprintf(stderr, "Failed to allocate device vector A (error code %s)!\n", cudaGetErrorString(err));        exit(EXIT_FAILURE);    }    Add << <1, 1 >> >(2, 7, devc);    err = cudaMemcpy(&c, devc, sizeof(int), cudaMemcpyDeviceToHost);    if (err != cudaSuccess)    {        fprintf(stderr, "Failed to allocate device vector A (error code %s)!\n", cudaGetErrorString(err));        exit(EXIT_FAILURE);    }    printf("2+7=%d\n", c);    cudaFree(devc);    system("pause");    return 0;}

运行结果如下图所示:
这里写图片描述
恭喜,cuda已经在您的机器上安装成功了。
具体的验证程序可在如下地址下载:http://www.pudn.com/downloads782/sourcecode/windows/bitmap/detail3095966.html
- 2.3 CUDNN的配置:
将文件解压,例如解压到d:\cuda\
解压后有三个子目录:bin,include,lib
这里写图片描述
将bin目录(例如 d:\cuda\bin)添加到环境变量 PATH 中。

用 vs2013 新建 cuda 项目,会自动生成演示程序kernel.cu。在vs编辑器正上方,Solution Configuration 的内容如果是Debug ,改为 Release ,旁边Platforms Solution Platforms 中的内容如果是win32,要改选为x64。
这里写图片描述

这里写图片描述

这里写图片描述

接下来修改项目属性:

项目属性/VC++ Directories/Include Directories 中添加入include的路径(例如 d:\cuda\include);

在项目属性/VC++ Directories/Libary Directories 中添加入lib\x64路径(例如 d:\cuda\lib\x64);
在项目属性/Linker/Input/Additional Dependencies 中添加入cudnn.lib;
这里写图片描述
这里写图片描述
项目属性/CUDA C|C++ / Device /Code Generation 中,将sm_20改为 sm_30或更高;
项目属性修改完毕。也可不更改。
这里写图片描述

在main()函数return之前加入如下代码,#include要放在文件开头:

#include <iostream>#include <cuda_runtime.h>#include <cudnn.h>using namespace std;void main(){cudnnHandle_t handle;cudnnStatus_t t = cudnnCreate(&handle);cout<< cudnnGetErrorString(t);getchar();}

如果结果显示:
这里写图片描述
表明配置成功了
具体的验证程序可在如下地址下载:http://www.pudn.com/downloads782/sourcecode/windows/bitmap/detail3095968.html

1 0