caffe-windows + CUDA + cudnn +PyCaffe + MatCaffe配置

caffe-windows使用happynear提供的,好处是设置好了依赖库,不需要自己再进行设置,可以省很多麻烦事。GitHub地址:https://github.com/happynear/caffe-windows/tree/ms

配置时使用的环境:

1、windows 10

2、CUDA 10.1 + cudnn for CUDA 10.1

3、Visual Studio 2015 + Python 3.6 + Matlab 2016b

配置过程:

1、安装 CUDA 和 cudnn

这里只叙述大概过程,下载 CUDA 10.1,下载完成后,得到一个.exe文件,然后运行它,选择一个存放提取文件的位置,进入图形安装界面,我选择了精简,于是一路就 next 下去,最后 CUDA 安装在了:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1 。安装结束后在系统变量内多了:

下载 cudnn v7.5 for CUDA 10.1,下载后解压,将解压后的 bin、include、lib 拷贝到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1 下面,然后将 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64(这个也是cudnn的路径,后面会用上) 添加到 系统变量 ——> 变量 Path 下。

cmd 后输入 nvcc -V 可以显示 CUDA 的相关信息。

2、下载 caffe-windows,将caffe-windows存放的地址表示为[caffe path]。下载第三方依赖,下载好的文件解压到 [caffe path]/windows/thirdparty/ 下,再将 [caffe path]/windows/thirdparty/bins 添加到 环境变量 ——> 系统变量 ——> Path变量

3、在 [caffe path]/windows/ 下,找到 CommonSettings.props.example 这个文件,复制为 CommonSettings.props

4、打开 CommonSettings.props 文件,修改文件内容,原文件内容如下,红色字体是我标注的解读内容,也是需要我们修改的部分。

5、根据自己的需要进行修改:是否使用GPU?是否使用cuDNN?为caffe设置Python接口(PyCaffe)?为caffe设置Matlab接口(MatCaffe)?

不使用GPU,则需要将 CpuOnlyBuild 修改成 true,把 UseCuDnn 修改为 false;

使用GPU,不需要更改,需要设置 CudaVersion 为我们使用的版本,还要添加 cuDNN(上文提到的路径)的路径;

不使用 PyCaffe 或是 MatCaffe,只需将对应的 PythonSupport 或 MATLABSupport 设置为 false 即可;

若选择使用  PyCaffe 或是 MatCaffe,则需要在 PythonDir 或 MatlabDir 处添加 Python 或 Matlab 的路径。

6、我的需求:caffe-windows + GPU + CUDA 10.1 + cuDNN + PyCaffe + MatCaffe,根据我的需求,修改后的 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>
    <UseCuDNN>true</UseCuDNN>
    <UseNCCL>true</UseNCCL>
    <UseMKL>false</UseMKL>
    <!--<CudaVersion>8.0</CudaVersion> -->
	<CudaVersion>10.1</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>true</PythonSupport>

    <!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be
         set to the root of your Matlab installation. -->
    <MatlabSupport>true</MatlabSupport>
    <MXNetSupport>false</MXNetSupport>
    <CudaDependencies>cufft.lib</CudaDependencies>
    <BoostIncludeFolder>$(SolutionDir)thirdparty\Boost</BoostIncludeFolder>
    <BoostLibraryFolder>$(SolutionDir)thirdparty\Boost\lib64-msvc-14.0</BoostLibraryFolder>
    <HDF5Root>$(SolutionDir)thirdparty\HDF5</HDF5Root>
    <GFlagsRoot>$(SolutionDir)thirdparty\GFlags</GFlagsRoot>
    <GLogRoot>$(SolutionDir)thirdparty\Glog</GLogRoot>
    <ProtobufRoot>$(SolutionDir)thirdparty\Protobuf</ProtobufRoot>
    <ProtocDir>$(ProtobufRoot)\bin\</ProtocDir>
    <OpenCVRoot>$(SolutionDir)thirdparty\OpenCV</OpenCVRoot>
    <LMDBRoot>$(SolutionDir)thirdparty\LMDB</LMDBRoot>
    <OpenBLASRoot>$(SolutionDir)thirdparty\OpenBLAS</OpenBLASRoot>
    <LevelDBRoot>$(SolutionDir)thirdparty\LEVELDB</LevelDBRoot>
    <NCCLRoot>$(SolutionDir)thirdparty\NCCL</NCCLRoot>
    <MKLRoot>D:\ThirdPartyLibrary\IntelSWTools\compilers_and_libraries_2017.4.210\windows\mkl</MKLRoot>
    <MXNetRoot>D:\deepLearning\mxnet</MXNetRoot>

    <!-- Set CUDA architecture suitable for your GPU.
         Setting proper architecture is important to mimize your run and compile time. -->
    <CudaArchitecture>compute_52,sm_52;compute_60,sm_60;</CudaArchitecture>

    <!-- CuDNN 3 and 4 are supported -->
    <!--<CuDnnPath>D:\CUDA\CuDNNv5</CuDnnPath> -->
	<CuDnnPath>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1</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)\lib\x64;$(LibraryPath)</LibraryPath>
    <IncludePath>$(CuDnnPath)\include;$(IncludePath)</IncludePath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(UseNCCL)'=='true' And $(NCCLRoot)!=''">
    <CudaDependencies>nccl.lib;$(CudaDependencies)</CudaDependencies>
    <LibraryPath>$(NCCLRoot)\lib;$(LibraryPath)</LibraryPath>
    <IncludePath>$(NCCLRoot)\include;$(IncludePath)</IncludePath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(UseMKL)'=='true' And $(MKLRoot)!=''">
    <LibraryPath>$(MKLRoot)\lib\intel64_win;$(LibraryPath)</LibraryPath>
    <IncludePath>$(MKLRoot)\include;$(IncludePath)</IncludePath>
    <AdditionalDependencies>mkl_rt.lib;$(AdditionalDependencies)</AdditionalDependencies>
  </PropertyGroup>
  <PropertyGroup Condition="'$(UseMKL)'=='false' Or $(MKLRoot)==''">
    <LibraryPath>$(OpenBLASRoot)\lib;$(LibraryPath)</LibraryPath>
    <IncludePath>$(OpenBLASRoot)\include;$(IncludePath)</IncludePath>
    <AdditionalDependencies>libopenblas.dll.a;$(AdditionalDependencies)</AdditionalDependencies>
  </PropertyGroup>

  <PropertyGroup>
    <OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir>
    <IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir>
  </PropertyGroup>
  <PropertyGroup>
    <LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(BoostLibraryFolder);$(HDF5Root)\lib;$(GFlagsRoot)\lib;$(GLogRoot)\lib;$(ProtobufRoot)\lib;$(OpenCVRoot)\x64\vc14\lib;$(LMDBRoot)\lib;$(LevelDBRoot)\lib;$(LibraryPath)</LibraryPath>
    <IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(BoostIncludeFolder);$(HDF5Root)\include;$(GFlagsRoot)\include;$(GLogRoot)\include;$(ProtobufRoot)\include;$(OpenCVRoot)\include;$(LMDBRoot)\include;$(LevelDBRoot)\include;$(IncludePath)</IncludePath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(PythonSupport)'=='true'">
    <PythonDir>E:\IDE\Anaconda3</PythonDir>
    <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>
    <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(MatlabSupport)'=='true'">
    <MatlabDir>E:\IDE\MATLAB\R2016b</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="'$(UseNCCL)'=='true'">
    <ClCompile>
      <PreprocessorDefinitions>USE_NCCL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
    <CudaCompile>
      <Defines>USE_NCCL</Defines>
    </CudaCompile>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(UseMKL)'=='true'">
    <ClCompile>
      <PreprocessorDefinitions>USE_MKL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
    <CudaCompile>
      <Defines>USE_MKL</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>
      <DisableSpecificWarnings>4819;</DisableSpecificWarnings>
    </ClCompile>
    <Link>
      <EnableCOMDATFolding>true</EnableCOMDATFolding>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
      <OptimizeReferences>true</OptimizeReferences>
      <AdditionalDependencies>leveldb.lib;Advapi32.lib;Shlwapi.lib;lmdb.lib;opencv_world310.lib;libprotobuf.lib;glog.lib;gflags.lib;hdf5_tools.lib;hdf5_hl_fortran.lib;hdf5_fortran.lib;hdf5_hl_f90cstub.lib;hdf5_f90cstub.lib;hdf5_cpp.lib;hdf5_hl_cpp.lib;hdf5_hl.lib;hdf5.lib;zlib.lib;szip.lib;$(AdditionalDependencies)</AdditionalDependencies>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <ClCompile>
      <Optimization>Disabled</Optimization>
      <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
      <DisableSpecificWarnings>4819;</DisableSpecificWarnings>
    </ClCompile>
    <Link>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <AdditionalDependencies>leveldbd.lib;Advapi32.lib;Shlwapi.lib;lmdbd.lib;opencv_world310d.lib;libprotobufd.lib;glogd.lib;gflagsd.lib;hdf5_tools.lib;hdf5_hl_fortran.lib;hdf5_fortran.lib;hdf5_hl_f90cstub.lib;hdf5_f90cstub.lib;hdf5_cpp.lib;hdf5_hl_cpp.lib;hdf5_hl.lib;hdf5.lib;zlib.lib;szip.lib;$(AdditionalDependencies)</AdditionalDependencies>
    </Link>
  </ItemDefinitionGroup>
</Project>

7、完成上述的所有操作以后,我们就可以在 visual studio 2015 中进行编译了。使用 visual studio 2015 打开 [caffe path]/windows/Caffe.sln ,将编译模式设置为: Release + x64。

如果有生成失败的,或是 error C2220: 警告被视为错误 - 没有生成“object”文件,在它的 属性 --> 配置属性 --> C/C++ --> 常规 中,把将警告视为错误改成 ,可以使之生成成功。

如果看到 matlab 运行时需要版本10.x的SDK… 在 matcaffe 的 属性 --> 配置属性 常规 --> 目标平台版本 修改为你有的(看一下其它项目使用的是哪个,修改成一样即可)。

8、编译完成后,会在 [caffe path]\Build\x64\Release\pycaffe\caffe 下看到一个 _caffe.pyd 文件;在 [caffe path]\matlab\+caffe\private 下看到一个 caffe_.mexw64 文件。

 

 

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页