1. 程式人生 > >caffe學習(1)------windows下基於GPU配置

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