官方Caffe-windows 配置與示例運行


http://blog.csdn.net/guoyk1990/article/details/52909864

標簽: caffewindows配置訓練自己的數據
 分類:
   

本文主要介紹官方給出的caffe-windows的配置及如何訓練mnist數據集,介紹的比較基礎,大神請繞道吐舌頭

1、環境:windows 10\CUDA7.5\cuDNN\VS2013

2、GPU計算環境准備(沒有GPU的同學可以跳過此步)

(1)首先下載並安裝CUDA7.5,下載界面如圖1:

圖 1:CUDA7.5的下載界面

(2)下載cuDNN,注意要下載cuDNN v4版本,下載界面如圖2:

圖 2:cuDNN的下載界面

 

官網下載cuDNN需要先注冊,而且要填一些調查表,也可以在我上傳的資源中下載。下載后解壓會有三個文件夾bin、include、lib。將這三個文件夾復制到cuda的安裝目錄中:\NVIDIA GPU ComputingToolkit\CUDA\v7.5。(cuda的安裝目錄中也有這三個文件夾,將這三個文件夾分別與原來存在的文件夾合並,如3圖所示)。

圖 3:CUDA 7.5 安裝的根目錄

3、caffe-windows准備

(1)下載官方caffe-windows並解壓,將 .\windows\CommonSettings.props.example備份,並改名為CommonSettings.props。如圖4所示:

圖 4:修改后的CommonSettings.props文件

(2)關於CommonSettings.props文件的一點說明。

 

[html]  view plain  copy
 
  1. </pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
  2. <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
  3.     <ImportGroup Label="PropertySheets" />  
  4.     <PropertyGroup Label="UserMacros">  
  5.         <BuildDir>$(SolutionDir)..\Build</BuildDir>  
  6.         <!--NOTE: CpuOnlyBuild and UseCuDNN flags can't be set at the same time.-->  
  7.         <CpuOnlyBuild>false</CpuOnlyBuild><!--注釋里說的很清楚,這兩個值不能同時設為true。若沒有GPU就把CpuOnlyBuild設為true-->  
  8.         <UseCuDNN>true</UseCuDNN>  
  9.         <CudaVersion>7.5</CudaVersion>  
  10.         <!-- NOTE: If Python support is enabled, PythonDir (below) needs to be  
  11.          set to the root of your Python installation. If your Python installation  
  12.          does not contain debug libraries, debug build will not work. -->  
  13.         <PythonSupport>false</PythonSupport><!--設置是否支持python接口,若想支持,需要改后面的PythonDir的值-->  
  14.     <!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be  
  15.          set to the root of your Matlab installation. -->  
  16.         <MatlabSupport>false</MatlabSupport><!--設置是否支持matlab接口,若想支持,需要改后面的MatlabDir的值-->  
  17.         <CudaDependencies></CudaDependencies>  
  18.   
  19.         <!-- Set CUDA architecture suitable for your GPU.  
  20.          Setting proper architecture is important to mimize your run and compile time. -->  
  21.         <CudaArchitecture>compute_35,sm_35;compute_52,sm_52</CudaArchitecture>  
  22.   
  23.         <!-- CuDNN 3 and 4 are supported -->  
  24.         <CuDnnPath></CuDnnPath>  
  25.         <ScriptsDir>$(SolutionDir)\scripts</ScriptsDir>  
  26.     </PropertyGroup>  
  27.     <PropertyGroup Condition="'$(CpuOnlyBuild)'=='false'">  
  28.         <CudaDependencies>cublas.lib;cuda.lib;curand.lib;cudart.lib</CudaDependencies>  
  29.     </PropertyGroup>  
  30.   
  31.     <PropertyGroup Condition="'$(UseCuDNN)'=='true'">  
  32.         <CudaDependencies>cudnn.lib;$(CudaDependencies)</CudaDependencies>  
  33.     </PropertyGroup>  
  34.     <PropertyGroup Condition="'$(UseCuDNN)'=='true' And $(CuDnnPath)!=''">  
  35.         <LibraryPath>$(CuDnnPath)\cuda\lib\x64;$(LibraryPath)</LibraryPath>  
  36.         <IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath>  
  37.     </PropertyGroup>  
  38.   
  39.     <PropertyGroup>  
  40.         <OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir>  
  41.         <IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir>  
  42.     </PropertyGroup>  
  43.     <PropertyGroup>  
  44.         <LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath>  
  45.         <IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath>  
  46.     </PropertyGroup>  
  47.     <PropertyGroup Condition="'$(PythonSupport)'=='true'"><!--與前面python接口設置對應-->  
  48.         <PythonDir>C:\Miniconda2\</PythonDir>  
  49.         <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>  
  50.         <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>  
  51.     </PropertyGroup>  
  52.     <PropertyGroup Condition="'$(MatlabSupport)'=='true'"><!--與前面的matlab接口設置對應-->  
  53.         <MatlabDir>C:\Program Files\MATLAB\R2014b</MatlabDir>  
  54.         <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>  
  55.         <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath>  
  56.     </PropertyGroup>  
  57.     <ItemDefinitionGroup Condition="'$(CpuOnlyBuild)'=='true'">  
  58.         <ClCompile>  
  59.             <PreprocessorDefinitions>CPU_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  60.         </ClCompile>  
  61.     </ItemDefinitionGroup>  
  62.     <ItemDefinitionGroup Condition="'$(UseCuDNN)'=='true'">  
  63.         <ClCompile>  
  64.             <PreprocessorDefinitions>USE_CUDNN;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  65.         </ClCompile>  
  66.         <CudaCompile>  
  67.             <Defines>USE_CUDNN</Defines>  
  68.         </CudaCompile>  
  69.     </ItemDefinitionGroup>  
  70.     <ItemDefinitionGroup Condition="'$(PythonSupport)'=='true'">  
  71.         <ClCompile>  
  72.             <PreprocessorDefinitions>WITH_PYTHON_LAYER;BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  73.         </ClCompile>  
  74.     </ItemDefinitionGroup>  
  75.     <ItemDefinitionGroup Condition="'$(MatlabSupport)'=='true'">  
  76.         <ClCompile>  
  77.             <PreprocessorDefinitions>MATLAB_MEX_FILE;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  78.         </ClCompile>  
  79.     </ItemDefinitionGroup>  
  80.     <ItemDefinitionGroup>  
  81.         <ClCompile>  
  82.             <MinimalRebuild>false</MinimalRebuild>  
  83.             <MultiProcessorCompilation>true</MultiProcessorCompilation>  
  84.             <PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;USE_OPENCV;USE_LEVELDB;USE_LMDB;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  85.             <TreatWarningAsError>true</TreatWarningAsError>  
  86.         </ClCompile>  
  87.     </ItemDefinitionGroup>  
  88.     <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">  
  89.         <ClCompile>  
  90.             <Optimization>Full</Optimization>  
  91.             <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  92.             <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>  
  93.             <FunctionLevelLinking>true</FunctionLevelLinking>  
  94.         </ClCompile>  
  95.         <Link>  
  96.             <EnableCOMDATFolding>true</EnableCOMDATFolding>  
  97.             <GenerateDebugInformation>true</GenerateDebugInformation>  
  98.             <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>  
  99.             <OptimizeReferences>true</OptimizeReferences>  
  100.         </Link>  
  101.     </ItemDefinitionGroup>  
  102.     <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">  
  103.         <ClCompile>  
  104.             <Optimization>Disabled</Optimization>  
  105.             <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  106.             <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>  
  107.         </ClCompile>  
  108.         <Link>  
  109.             <GenerateDebugInformation>true</GenerateDebugInformation>  
  110.         </Link>  
  111.     </ItemDefinitionGroup>  
  112. </Project>  

 

4、編譯caffe-windows

編譯用vs2013打開.\windows\Caffe.sln 並將解決方案的配置改為release,點菜單欄上的“生成->生成解決方案”,會將整個項目全部生成,這個時間會比較長(由於官方caffe-windows 的版本使用了NuGet管理第三方開發包,所以需要在vs2013上安裝NuGet,官方網站下載速度比較慢,可以在我的資源里下載)。生成成功之后的文件都在.\Build\x64\Release中。

PS:生成時可能遇到的錯誤:errorC2220: 警告被視為錯誤 - 沒有生成“object”文件 (..\..\src\caffe\util\math_functions.cpp)。這個錯誤可參考Sunshine_in_Moon 的解決方案

5、測試

1)下載MNIST數據集,MNIST數據集包含四個文件,如表1所示:

表1:MNIST數據集及其文件解釋

 

文件

內容

train-images-idx3-ubyte.gz

訓練集圖片 - 55000 張 訓練圖片, 5000 張 驗證圖片

train-labels-idx1-ubyte.gz

訓練集圖片對應的數字標簽

t10k-images-idx3-ubyte.gz

測試集圖片 - 10000 張 圖片

t10k-labels-idx1-ubyte.gz

測試集圖片對應的數字標簽

 

下載完后解壓得到對應的四個文件,這四個文件不能直接用於caffe的訓練和測試。需要利用第4步生成的convert_mnist_data.exe把四個文件轉換為caffe所支持的leveldb或lmdb文件。

 

2)轉換 訓練\測試數據

 

a)  中的四個文件放到 . \examples\mnist\mnist_data文件夾下。

b)  在caffe-windows安裝的根目錄下,新建一個convert-mnist-data-train.bat文件轉換為訓練數據,並在文件中添加代碼:

 

[python]  view plain  copy
 
  1. Build\x64\Release\convert_mnist_data.exe --backend=lmdbexamples\mnist\mnist_data\train-images.idx3-ubyteexamples\mnist\mnist_data\train-labels.idx1-ubyte examples\mnist\mnist_data\mnist_train_lmdb  
  2. pause  

 

其中--backend=lmdb 表示轉換為lmdb格式,若要轉換為leveldb將其改寫為--backend=leveldb 即可。

再新建一個convert-mnist-data-test.bat轉換測試數據,代碼為:

 

[python]  view plain  copy
 
  1. Build\x64\Release\convert_mnist_data.exe --backend=lmdb examples\mnist\mnist_data\t10k-images.idx3-ubyte examples\mnist\mnist_data\t10k-labels.idx1-ubyte examples\mnist\mnist_data\mnist_test_lmdb  
  2. Pause  

 

Ps:(1)convert_mnist_data.exe的命令格式為:

convert_mnist_data [FLAGS] input_image_file input_label_file output_db_file

[FLAGS]:轉換的文件格式可取leveldb或lmdb,示例:--backend=leveldb

Input_image_file:輸入的圖片文件,示例:train-images.idx3-ubyte

input_label_file:輸入的圖片標簽文件,示例:train-labels.idx1-ubyte

output:保存輸出文件的文件夾,示例:mnist_train_lmdb

(2)如果感覺很麻煩,也可以直接下載我轉換好的MNIST文件(leveldb和lmdb)。

3)運行測試

 

(1)將第2)步中轉換好的訓練\測試數據集(mnist_train_lmdb\ mnist_train_lmdb或mnist_train_leveldb\mnist_train_leveldb)文件夾放在.\examples\mnist中。

(2)在caffe-windows根目錄下新建一個run.bat,文件中代碼:

 

[python]  view plain  copy
 
  1. Build\x64\Release\caffe.exe  train --solver=examples/mnist/lenet_solver.prototxt  
  2. pause  

 

保存並雙擊運行,如果運行成功,說明caffe配置成功了。

注意使用leveldb或lmdb格式的數據時,需要將lenet_train_test.prototxt 文件里面的data_param-> source和data_param-> backend相對應,如圖5紅框所標注處。
 

圖 5:lenet_train_test.prototxt文件中需要注意與訓練\測試數據對應的部分

4)訓練自己的數據

這部分可以參考下面的幾個博客:

1.在caffe上跑自己的數據

2.windows下caffe訓練自己的數據

 

References

BVLC/caffe

從零到一:caffe-windows(CPU)配置與利用mnist數據集訓練第一個caffemodel

Windows+VS2013爆詳細Caffe編譯安裝教程

【caffe-Windows】caffe+VS2013+Windows無GPU快速配置教程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM