caffe matlab无法使用gpu


解决windows10下配置matlab接口的GPU版Caffe时出现的一些常见问题

原始地址:http://www.echojb.com/cuda/2016/10/18/238102.html


常见问题:1.版本不兼容。我用的是vs2013+cuda7.5+CuDNN的V5版+matlab2016a,如果选用的是Microsoft的caffe,是可以用Cudnn的V5版的。但cuda8.0尚不被支持,如果你的显卡是最新的gtx10系列,如果cuda7.5不能驱动的话,就只能再等等了。所以首先请先确保你用的各种组件的版本不高于以上列出的版本。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)cudalibx64;$(LibraryPath)</LibraryPath>
 36         <IncludePath>$(CuDnnPath)cudainclude;$(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)..includecaffeproto;$(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 FilesMATLABR2016a</MatlabDir>
 54         <LibraryPath>$(MatlabDir)externlibwin64microsoft;$(LibraryPath)</LibraryPath>
 55         <IncludePath>$(MatlabDir)externinclude;$(MatlabDir)toolboxdistcompgpuexterninclude;$(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'">
 63         <ClCompile>
 64             <PreprocessorDefinitions>USE_CUDNN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
 65         </ClCompile>
 66         <CudaCompile>
 67             <Defines>USE_CUDNN</Defines>
 68         </CudaCompile>
 69     </ItemDefinitionGroup>
 70     <ItemDefinitionGroup Condition="'$(PythonSupport)'=='true'">
 71         <ClCompile>
 72             <PreprocessorDefinitions>WITH_PYTHON_LAYER;BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
 73         </ClCompile>
 74     </ItemDefinitionGroup>
 75     <ItemDefinitionGroup Condition="'$(MatlabSupport)'=='true'">
 76         <ClCompile>
 77             <PreprocessorDefinitions>MATLAB_MEX_FILE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
 78         </ClCompile>
 79     </ItemDefinitionGroup>
 80     <ItemDefinitionGroup>
 81         <ClCompile>
 82             <MinimalRebuild>false</MinimalRebuild>
 83             <MultiProcessorCompilation>true</MultiProcessorCompilation>
 84             <PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;USE_OPENCV;USE_LEVELDB;USE_LMDB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
 85             <TreatWarningAsError>true</TreatWarningAsError>
 86         </ClCompile>
 87     </ItemDefinitionGroup>
 88     <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
 89         <ClCompile>
 90             <Optimization>Full</Optimization>
 91             <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
 92             <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
 93             <FunctionLevelLinking>true</FunctionLevelLinking>
 94         </ClCompile>
 95         <Link>
 96             <EnableCOMDATFolding>true</EnableCOMDATFolding>
 97             <GenerateDebugInformation>true</GenerateDebugInformation>
 98             <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
 99             <OptimizeReferences>true</OptimizeReferences>
100         </Link>
101     </ItemDefinitionGroup>
102     <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
103         <ClCompile>
104             <Optimization>Disabled</Optimization>
105             <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
106             <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
107         </ClCompile>
108         <Link>
109             <GenerateDebugInformation>true</GenerateDebugInformation>
110         </Link>
111     </ItemDefinitionGroup>
112 </Project>
请保证第7,8,16行 如上所示;在第24行添加你解压的cudnn的路径,路径下的文件夹应该是cuda;第53行添加你所用的的matlab的安装路径。3.1如果你用的是Microsoft的caffe,在用vs2013生成matcaffe的解决方案时,最后出现了错误:“无法打开包括文件: “gpu/mxGPUArray.h”: No such file or directory”那么请在第55行,添加路径:
$(MatlabDir)toolboxdistcompgpuexterninclude;
当然你的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.
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值