1. 程式人生 > >windows下vs2013配置caffe及基本使用(一)

windows下vs2013配置caffe及基本使用(一)

目前已經用python程式碼搭建了CNN來做目標分類、檢測、分割(用python搭建速度快,非常方便)。之後又轉用tensorflow的C++ API來搭建CNN重新實現一遍目標分類、檢測、分割(執行效率高)。現在再轉用caffe實現一遍。

先從配置說起。

1.準備環境

(1) VS2013

 (2) cuda8.0+cudnnv5.1(如何安裝配置驗證參考我的之前博文)

 (3) caffe-master 點選開啟連結

     (4)  因為在編譯過程中會自動nuget一些相關包,所以要確保安裝了Nuget,如果沒有可以在此連結下下載點選開啟連結,選擇vs2013版的

     (5) matlab2014a    //編譯caffe的matlab介面要用

     (6) Anaconda2     //編譯caffe的python介面要用

2. 用VS2013開啟caffe-master資料夾下的Caffe.sln解決方案

 

 (1)拷貝一份CommonSettings.props.example並把備份檔案改名為CommonSettings.props


(2)修改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>  //用GPU編譯這裡設為false
        <UseCuDNN>true</UseCuDNN>          //啟用cudnn
        <CudaVersion>8.0</CudaVersion>     //cuda版本
<!-- 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> //支援生成python介面
<!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be set to the root of your Matlab installation. --> <MatlabSupport>true</MatlabSupport> //支援生成matlab介面 <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 4 and 5 are supported --> <CuDnnPath>D:\software\cudnn-8.0-windows7-x64-v5.1</CuDnnPath> //cuddnv5.1的路徑 <CUDA_PATH>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0</CUDA_PATH> //這行是我自己加上的,選擇好cuda8.0的位置 <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>C:\Program Files\Anaconda2</PythonDir> //python35.dll python.exe所在的路徑 <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath> <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup Condition="'$(MatlabSupport)'=='true'"> <MatlabDir>C:\Program Files\MATLAB\R2014a</MatlabDir> //matlab所在的路徑 <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath> <IncludePath>$(MatlabDir)\extern\include;$(MatlabDir)\toolbox\distcomp\gpu\extern\include;$(IncludePath)</IncludePath> //自己添加了一個$(MatlabDir)\toolbox\distcomp\gpu\extern\include </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>true</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>
(3)右擊libcaffe,在屬性頁c/c++ 下General裡的Treat Warning As Errors設為No(/WX-)

(4)生成解決方案。由於設定的libcaffe為啟動項,且libcaffe的依賴項如下圖所示:

    

       libcaffe並沒有依賴於其它項,所以在D:\caffe_learn\caffe-master\Build\x64\Release目錄下

   

       只看到了libcaffe的相關dll。其它工程如果有需要編譯,可以手動去執行編譯生成。接下來的部落格(二)中有許多例子依賴這些工程的生成。

     接下來的工作主要為一下幾項:

        (1)用caffe.exe或者命令列模式(python、matlab)去訓練minist、cifar、mydata資料集

        (2)預測一張minist、cifar、mydata資料集影象(用c++(exe形式或者命令列)、python、matlab介面去預測)