如何配置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.