本文大多轉載自 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文件的一點說明。
- </pre><pre name="code" class="html"><?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><!--注釋里說的很清楚,這兩個值不能同時設為true。若沒有GPU就把CpuOnlyBuild設為true-->
- <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>false</PythonSupport><!--設置是否支持python接口,若想支持,需要改后面的PythonDir的值-->
- <!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be
- set to the root of your Matlab installation. -->
- <MatlabSupport>false</MatlabSupport><!--設置是否支持matlab接口,若想支持,需要改后面的MatlabDir的值-->
- <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></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'"><!--與前面python接口設置對應-->
- <PythonDir>C:\Miniconda2\</PythonDir>
- <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>
- <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>
- </PropertyGroup>
- <PropertyGroup Condition="'$(MatlabSupport)'=='true'"><!--與前面的matlab接口設置對應-->
- <MatlabDir>C:\Program Files\MATLAB\R2014b</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>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、編譯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數據集及其文件解釋
文件 |
內容 |
訓練集圖片 - 55000 張 訓練圖片, 5000 張 驗證圖片 |
|
訓練集圖片對應的數字標簽 |
|
測試集圖片 - 10000 張 圖片 |
|
測試集圖片對應的數字標簽 |
2)轉換 訓練\測試數據
a) 中的四個文件放到 . \examples\mnist\mnist_data文件夾下。
b) 在caffe-windows安裝的根目錄下,新建一個convert-mnist-data-train.bat文件轉換為訓練數據,並在文件中添加代碼:
- 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
- pause
其中--backend=lmdb 表示轉換為lmdb格式,若要轉換為leveldb將其改寫為--backend=leveldb 即可。
再新建一個convert-mnist-data-test.bat轉換測試數據,代碼為:
- 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
- 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,文件中代碼:
- Build\x64\Release\caffe.exe train --solver=examples/mnist/lenet_solver.prototxt
- 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)訓練自己的數據
這部分可以參考下面的幾個博客:
reference:
【caffe-Windows】caffe+VS2013+Windows無GPU快速配置教程