目前已经用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>
<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接口去预测)
参考博客:点击打开链接