1. 程式人生 > >深度學習之Caffe環境搭建

深度學習之Caffe環境搭建

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

  1. 配置過程
    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();
}