Caffe配置---配置Windows版 Caffe

来源:互联网 发布:php 分班问题 编辑:程序博客网 时间:2024/05/17 02:35

Caffe配置---配置Windows版 Caffe

1.首先去github上下载微软的caffe 

https://github.com/Microsoft/caffe

上有官方的安装说明。 

解压文件到任意位置 

2.下载CUDA7.5,并安装 

https://developer.nvidia.com/cuda-toolkit 
下载cuDNN v4 
cuDNN需要申请才能下载,这里我提供网盘给大家http://pan.baidu.com/s/1pJVB0tt 

将cuDNN解压后放在另外的目录下,cuDNN中有.cuda\bin+include+lib文件夹。后面需要在vs属性页中指定此cudnn的路径、。

我的是 D:\cudnn-win64\cudnn-win64-v4.0\cuda\{bin+include+lib}

3.下载python 

这里使用的是WinPython集合包,请下载64位版本 
https://sourceforge.net/projects/winpython/files/WinPython_2.7/2.7.10.3/ 
安装WinPython 
注意:下载64位python2.7版本,如果不用winpython,anaconda也是可以的 

4.安装vs2013,

这里注意vs2013的安装最好安装在默认的C盘位置。 如果不是在C盘位置,会报错“E:\NugetPackages\OpenCV.2.4.10\build\native\OpenCV.targets(57,5): error MSB3030: 无法复制文件“C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\redist\x64\Microsoft.VC120.CRT\msvcr120.dll” 

这个是可以解决的,你找到你安装的vs目录下,找到Microsoft Visual Studio 12.0文件夹,整个复制到他这个位置即可 

5.配置属性页文件---一定要先打开这个属性文件,修改,这样才能避免后期再添加各种包含目录等等

打开caffe-master\windows文件夹,复制CommonSettings.props.example,再将CommonSettings.props.example.副本 重命名为CommonSettings.props。  用VS打开此配置文件,按照自己的配置进行设定,主要有CUDA/Python/Matlab/cudnn--设定为true,并配置其路径。

cuda路径是默认搜索的。

cudnn的路径可以自己在<CuDnnPath>//<CuDnnPath>下重新指定,

指定之后,另外注意,在cudnn包含目录和库目录前还要加上这个路径,如下: 

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <CuDnnPath>D:\cudnn-win64\cudnn-win64-v4.0</CuDnnPath>  

Matlab和Python的路径直接替换掉原文本中的就可以,如下。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <PythonDir>D:\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\</PythonDir>  
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <pre code_snippet_id="1705925" snippet_file_name="blog_20160602_1_6826621" name="code" class="cpp"><MatlabDir>D:\MATLAB\R2014a</MatlabDir>  

完整配置文件如下图所示:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?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>  
  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>true</PythonSupport>  
  14.         <!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be  
  15.          set to the root of your Matlab installation. -->  
  16.         <MatlabSupport>true</MatlabSupport>  
  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>D:\cudnn-win64\cudnn-win64-v4.0</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.         <CuDnnPath>D:\cudnn-win64\cudnn-win64-v4.0</CuDnnPath>  
  36.         <LibraryPath>$(CuDnnPath)\cuda\lib\x64;$(LibraryPath)</LibraryPath>  
  37.         <IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath>  
  38.     </PropertyGroup>  
  39.   
  40.     <PropertyGroup>  
  41.         <OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir>  
  42.         <IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir>  
  43.     </PropertyGroup>  
  44.     <PropertyGroup>  
  45.         <LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath>  
  46.         <IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath>  
  47.     </PropertyGroup>  
  48.     <PropertyGroup Condition="'$(PythonSupport)'=='true'">  
  49.         <PythonDir>D:\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\</PythonDir>  
  50.         <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>  
  51.         <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>  
  52.     </PropertyGroup>  
  53.     <PropertyGroup Condition="'$(MatlabSupport)'=='true'">  
  54.         <MatlabDir>D:\MATLAB\R2014a</MatlabDir>  
  55.         <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>  
  56.         <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath>  
  57.     </PropertyGroup>  
  58.     <ItemDefinitionGroup Condition="'$(CpuOnlyBuild)'=='true'">  
  59.         <ClCompile>  
  60.             <PreprocessorDefinitions>CPU_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  61.         </ClCompile>  
  62.     </ItemDefinitionGroup>  
  63.     <ItemDefinitionGroup Condition="'$(UseCuDNN)'=='true'">  
  64.         <ClCompile>  
  65.             <PreprocessorDefinitions>USE_CUDNN;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  66.         </ClCompile>  
  67.         <CudaCompile>  
  68.             <Defines>USE_CUDNN</Defines>  
  69.         </CudaCompile>  
  70.     </ItemDefinitionGroup>  
  71.     <ItemDefinitionGroup Condition="'$(PythonSupport)'=='true'">  
  72.         <ClCompile>  
  73.             <PreprocessorDefinitions>WITH_PYTHON_LAYER;BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  74.         </ClCompile>  
  75.     </ItemDefinitionGroup>  
  76.     <ItemDefinitionGroup Condition="'$(MatlabSupport)'=='true'">  
  77.         <ClCompile>  
  78.             <PreprocessorDefinitions>MATLAB_MEX_FILE;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  79.         </ClCompile>  
  80.     </ItemDefinitionGroup>  
  81.     <ItemDefinitionGroup>  
  82.         <ClCompile>  
  83.             <MinimalRebuild>false</MinimalRebuild>  
  84.             <MultiProcessorCompilation>true</MultiProcessorCompilation>  
  85.             <PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;USE_OPENCV;USE_LEVELDB;USE_LMDB;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  86.             <TreatWarningAsError>true</TreatWarningAsError>  
  87.         </ClCompile>  
  88.     </ItemDefinitionGroup>  
  89.     <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">  
  90.         <ClCompile>  
  91.             <Optimization>Full</Optimization>  
  92.             <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  93.             <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>  
  94.             <FunctionLevelLinking>true</FunctionLevelLinking>  
  95.         </ClCompile>  
  96.         <Link>  
  97.             <EnableCOMDATFolding>true</EnableCOMDATFolding>  
  98.             <GenerateDebugInformation>true</GenerateDebugInformation>  
  99.             <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>  
  100.             <OptimizeReferences>true</OptimizeReferences>  
  101.         </Link>  
  102.     </ItemDefinitionGroup>  
  103.     <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">  
  104.         <ClCompile>  
  105.             <Optimization>Disabled</Optimization>  
  106.             <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>  
  107.             <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>  
  108.         </ClCompile>  
  109.         <Link>  
  110.             <GenerateDebugInformation>true</GenerateDebugInformation>  
  111.         </Link>  
  112.     </ItemDefinitionGroup>  
  113. </Project>  

配置完成后如下:


6.编译caffe 

点击E:\caffe-master\windows文件夹里的Caffe.sln,这时你发现解决方案的9个项目中libcaffe和test_all没有加载成功。不过不影响,先把编译器切换到Release x64.然后点击本地windows调试器,nuget会先下载安装需要的一些库文件,下载完后后继续编译操作,中间可能会出现一些报错 
在解决方案后中右击每一个项目,属性->C/C++,把“警告等级”调整为等级3,把“将警告视为错误”改成否。 
报错“error C2220: 警告被视为错误 - 没有生成“object”文件”,解决方法是用vs2013打开这里报错的cpp,然后点击保存即可。我这里是nuget中的boost报错。

这里只是完成了 libcaffe项目的编译,下面一个一个右击剩余的8个项目,然后点击“生成”,即可。 

7.添加环境变量 

在环境变量的用户变量中,新建用户变量,变量名“PythonPath”,变量值“E:\caffe-master\Build\x64\Release\pycaffe”。 

运行build下的 test.exe时会提示找不到python27.dll,这是因为没有把python的python27.dll添加到系统路径path下。

往path下添加 D:\WinPython-64bit-2.7.10.3\python-2.7.10.amd64 即可。

在Matlab的搜素路径中加入,.\build\release\X64\matcaffe进去,即让matlab能找到 编译后的 caffemex_.win64这个文件。

或者直接将matcaffe文件夹拷贝到根目录下,并在每次构建新项目时,将 此路径添加为搜索路径。

在系统环境变量 path中添加".\caffe-master\Build\x64\Release",以供程序找到一些相关的dll模块。 

否则在运行上述matlab demo时会提示“invalid .mexw64 找不到指定的模块.....”

*********若还有可能出现“caffe_.mexw64 找不到指定模块”这个Bug,原因可能是配置属性页的时候CUDA路径是默认的,如果装cuda的时候,先装cuda7.5, 再装6.5,那么系统变量中的CUDA_PATH和CUDASAMPLES_PATH都会被覆盖成v6.5,需要改回来再重新编译。 

8.测试及常见Bug问题

至此,已完成caffe在win7上的配置,python和matlab接口已配置完毕。

(1).可以打开winpython 或 anaconda/python,输入import caffe,观察导入模块,如无报错,则caffe配置成功.

(2).找到.matlab\demo下的classification.m运行如下: 

需要将编译后的+caffe添加到搜索路径中去,这样有些麻烦,可在每次新建matlab项目时,选择性的添加这个路径。如下所示:。

本文是将编译后的matcaffe文件夹直接拖到了根目录下,屏蔽掉demo同路径下的+caffe,并修改classification_demo文件中的路径配置部分。


 

(2).找到.build\x64\release下的test_all运行如下:

0 0
原创粉丝点击