1. 程式人生 > >Windows下caffe安裝詳解(僅CPU)

Windows下caffe安裝詳解(僅CPU)

net library dad 新建 math 錯誤 format ack 訓練集

本文大多轉載自 http://blog.csdn.net/guoyk1990/article/details/52909864,加入部分自己實戰心得。

1、環境:windows 7\VS2013

2、caffe-windows準備

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

技術分享

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

附帶說明,現在最新版的github已經更新,沒有上述文件,根據大佬說法用cmake編譯後能產生sln文件,筆者不才,並不會,這裏提供百度雲盤的老版本:

caffe提供Windows工具包(caffe-windows):https://github.com/BVLC/caffe/tree/windows 百度雲下載地址:鏈接:http://pan.baidu.com/s/1bp1BFH1 密碼:phf3

(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. <!-- Set CUDA architecture suitable for your GPU.
  19. Setting proper architecture is important to mimize your run and compile time. -->
  20. <CudaArchitecture>compute_35,sm_35;compute_52,sm_52</CudaArchitecture>
  21. <!-- CuDNN 3 and 4 are supported -->
  22. <CuDnnPath></CuDnnPath>
  23. <ScriptsDir>$(SolutionDir)\scripts</ScriptsDir>
  24. </PropertyGroup>
  25. <PropertyGroup Condition="‘$(CpuOnlyBuild)‘==‘false‘">
  26. <CudaDependencies>cublas.lib;cuda.lib;curand.lib;cudart.lib</CudaDependencies>
  27. </PropertyGroup>
  28. <PropertyGroup Condition="‘$(UseCuDNN)‘==‘true‘">
  29. <CudaDependencies>cudnn.lib;$(CudaDependencies)</CudaDependencies>
  30. </PropertyGroup>
  31. <PropertyGroup Condition="‘$(UseCuDNN)‘==‘true‘ And $(CuDnnPath)!=‘‘">
  32. <LibraryPath>$(CuDnnPath)\cuda\lib\x64;$(LibraryPath)</LibraryPath>
  33. <IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath>
  34. </PropertyGroup>
  35. <PropertyGroup>
  36. <OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir>
  37. <IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir>
  38. </PropertyGroup>
  39. <PropertyGroup>
  40. <LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath>
  41. <IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath>
  42. </PropertyGroup>
  43. <PropertyGroup Condition="‘$(PythonSupport)‘==‘true‘"><!--與前面python接口設置對應-->
  44. <PythonDir>C:\Miniconda2\</PythonDir>
  45. <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>
  46. <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>
  47. </PropertyGroup>
  48. <PropertyGroup Condition="‘$(MatlabSupport)‘==‘true‘"><!--與前面的matlab接口設置對應-->
  49. <MatlabDir>C:\Program Files\MATLAB\R2014b</MatlabDir>
  50. <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>
  51. <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath>
  52. </PropertyGroup>
  53. <ItemDefinitionGroup Condition="‘$(CpuOnlyBuild)‘==‘true‘">
  54. <ClCompile>
  55. <PreprocessorDefinitions>CPU_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  56. </ClCompile>
  57. </ItemDefinitionGroup>
  58. <ItemDefinitionGroup Condition="‘$(UseCuDNN)‘==‘true‘">
  59. <ClCompile>
  60. <PreprocessorDefinitions>USE_CUDNN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  61. </ClCompile>
  62. <CudaCompile>
  63. <Defines>USE_CUDNN</Defines>
  64. </CudaCompile>
  65. </ItemDefinitionGroup>
  66. <ItemDefinitionGroup Condition="‘$(PythonSupport)‘==‘true‘">
  67. <ClCompile>
  68. <PreprocessorDefinitions>WITH_PYTHON_LAYER;BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  69. </ClCompile>
  70. </ItemDefinitionGroup>
  71. <ItemDefinitionGroup Condition="‘$(MatlabSupport)‘==‘true‘">
  72. <ClCompile>
  73. <PreprocessorDefinitions>MATLAB_MEX_FILE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  74. </ClCompile>
  75. </ItemDefinitionGroup>
  76. <ItemDefinitionGroup>
  77. <ClCompile>
  78. <MinimalRebuild>false</MinimalRebuild>
  79. <MultiProcessorCompilation>true</MultiProcessorCompilation>
  80. <PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;USE_OPENCV;USE_LEVELDB;USE_LMDB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  81. <TreatWarningAsError>true</TreatWarningAsError>
  82. </ClCompile>
  83. </ItemDefinitionGroup>
  84. <ItemDefinitionGroup Condition="‘$(Configuration)|$(Platform)‘==‘Release|x64‘">
  85. <ClCompile>
  86. <Optimization>Full</Optimization>
  87. <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  88. <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
  89. <FunctionLevelLinking>true</FunctionLevelLinking>
  90. </ClCompile>
  91. <Link>
  92. <EnableCOMDATFolding>true</EnableCOMDATFolding>
  93. <GenerateDebugInformation>true</GenerateDebugInformation>
  94. <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
  95. <OptimizeReferences>true</OptimizeReferences>
  96. </Link>
  97. </ItemDefinitionGroup>
  98. <ItemDefinitionGroup Condition="‘$(Configuration)|$(Platform)‘==‘Debug|x64‘">
  99. <ClCompile>
  100. <Optimization>Disabled</Optimization>
  101. <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  102. <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
  103. </ClCompile>
  104. <Link>
  105. <GenerateDebugInformation>true</GenerateDebugInformation>
  106. </Link>
  107. </ItemDefinitionGroup>
  108. </Project>

3、編譯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 的解決方案。

4、測試

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配置成功了。

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

技術分享

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

註意2將lenet_solver.prototxt 文件裏面的最後一行改為solver_mode:CPU。

4)訓練自己的數據

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

1.在caffe上跑自己的數據

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

reference:

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

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

Windows下caffe安裝詳解(cpu+gpu+matcaffe+pycaffe)

Windows下caffe安裝詳解(僅CPU)