解决win10下配置matlab接口的GPU版Caffe时常遇到的一些错误 - deepJ

时间:2024-02-15 15:24:28

解决win10下配置matlab接口的GPU版Caffe时常遇到的一些错误

如何配置windows10下的MATLAB接口的GPU版本的caffe,网上的教程挺多的,但因为大家用的硬软件不同,按教程配置下来以后,往往会出现一些小问题。在此,分享一下几个常见问题的解决方法。具体配置过程可以参见http://www.cnblogs.com/alcohol/p/caffe-windows.html。

常见问题:

1.版本不兼容。我用的是vs2013+cuda7.5+CuDNN的V5版+matlab2016a,如果选用的是Microsoft的caffe,是可以用Cudnn的V5版的。但cuda8.0尚不被支持,如果你的显卡是最新的gtx10系列,如果cuda7.5不能驱动的话,就只能再等等了。所以首先请先确保你用的各种组件的版本不高于以上列出的版本。

2.在用vs2013生成解决方案时,需要下载第三方包源 NugetPackages,可以在这里下载:http://wangpan.baidu.com/s/1dFl1cp7 密码:d6e3,然后解压到与caffe-master同一个父文件夹下。

3.CommonSettings.props的配置.

  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>false</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_30,sm_30;compute_35,sm_35;compute_52,sm_52</CudaArchitecture>
 22 
 23         <!-- CuDNN 4 and 5 are supported -->
 24         <CuDnnPath>C:\cuDNN</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         <LibraryPath>$(CuDnnPath)\cuda\lib\x64;$(LibraryPath)</LibraryPath>
 36         <IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath>
 37     </PropertyGroup>
 38 
 39     <PropertyGroup>
 40         <OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir>
 41         <IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir>
 42     </PropertyGroup>
 43     <PropertyGroup>
 44         <LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath>
 45         <IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath>
 46     </PropertyGroup>
 47     <PropertyGroup Condition="\'$(PythonSupport)\'==\'true\'">
 48         <PythonDir>C:\Miniconda2\</PythonDir>
 49         <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>
 50         <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>
 51     </PropertyGroup>
 52     <PropertyGroup Condition="\'$(MatlabSupport)\'==\'true\'">
 53         <MatlabDir>C:\Program Files\MATLAB\R2016a</MatlabDir>
 54         <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>
 55         <IncludePath>$(MatlabDir)\extern\include;$(MatlabDir)\toolbox\distcomp\gpu\extern\include;$(IncludePath)</IncludePath>
 56     </PropertyGroup>
 57     <ItemDefinitionGroup Condition="\'$(CpuOnlyBuild)\'==\'true\'">
 58         <ClCompile>
 59             <PreprocessorDefinitions>CPU_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
 60         </ClCompile>
 61     </ItemDefinitionGroup>
 62     <ItemDefinitionGroup Condition="\'$(UseCuDNN)\'==\'true\'">

第7,8,16行改为如上所示;在第24行添加你解压的cudnn的路径,路径下的文件夹应该是cuda;第53行添加你所用的的matlab的安装路径。

3.1如果你用的是Microsoft的caffe,在用vs2013生成matcaffe的解决方案时,可能出现错误:

“无法打开包括文件: “gpu/mxGPUArray.h”: No such file or directory”

请在第55行,添加路径:

$(MatlabDir)\toolbox\distcomp\gpu\extern\include;

matlab版本不要低于2012b,否则这个目录下可能什么都没有;

3.2在系统变量和matlab中添加分别添加了路径.但在用matlab测试caffe的样例bvlc_reference_caffenet.caffemodel时出现了错误:

  

未定义函数或变量 \'caffe_\'。

出错 caffe.set_mode_cpu (line 5)
caffe_(\'set_mode_cpu\');

出错 classification_demo (line 71)
  caffe.set_mode_cpu();

 

可以通过在classification_demo.m文件的59行修改

addpath(\'..\');

为:

addpath(\'../../Build/x64/Release/matcaffe\');

解决.

3.2如果在启用GPU时进行测试时,即运行

[scores, maxlabel] = classification_demo(im, 1)

时,matlab突然crash出错,变成了已停止工作,并退出. 显然这和gpu配置有关,很有可能就是显卡的cuda compute capability版本未被支持导致.打开matlab,在命令行中键入gpuDevice回车,如果出现错误:

错误使用 gpuDevice (line 26)
An unexpected error occurred during CUDA execution. The CUDA error was:
cannot set while device is active in this process

那么请重启matlab,重新运行gpuDevice.直到输出如(否则请显卡驱动):

  CUDADevice (具有属性):

                      Name: \'GeForce GTX ****\'
                     Index: 1
         ComputeCapability: \'x.x\'
            SupportsDouble: 1
             DriverVersion: 8
            ToolkitVersion: 7.5000
      ...

查看第三行属性ComputeCapability, 假设x.x,就去掉小数点,在21行中加入"compute_xx,sm_xx;"(如compute_30,sm_30,如果x.x小于3.0就无法支持gpu了).然后保存,在用vs2013重新生成matcaffe,要先生成libcaffe,因为这里有前者需要的一些环境,解决方案要配置为Release.