通過朋友指導,終於成功在windows7上成功編譯了caffe,這裏將編譯過程記錄python
若是不須要cuda版本的caffe,本步跳過git
具體安裝方法見: 安裝visual studio 2013github
若是不須要cuda版本的caffe,本部分能夠跳過windows
安裝方法與theano安裝(二)windows安裝visual studio 2010及cuda中的cuda安裝方法一致,只不過是版本號不一樣而已網絡
若是你的本機沒有安裝Python,而且你須要配置pycaffe,那麼,請按照win7系統安裝python,並按照Pycharm集成開發環境安裝python工具
安裝完python後,再安裝一下protobuf,由於後面編譯pycaffe時有須要:visual-studio
pip install protobuf
若是你的本機沒有安裝matlab ,而且你須要配置matcaffe,那麼,請安裝matlab,具體安裝方法請自行查閱測試
在github上下載的window caffe把呢你自帶一個配置文件的例子,在進行編譯以前,須要先更改該文件內容ui
這裏假設caffe-windows被解壓到了以下的文件夾中
spa
將該文件夾(即.\windows)下的CommonSettings.props.example文件複製一份兒,並將複製的文件命名爲CommonSettings.props
-
caffe-windows自帶的配置文件CommonSettings.props.example中與咱們後面要進行的編譯密切相關的就是以下部分
例子中全部配置參數都是默認值
CpuOnlyBuild
:是否使用CPU
UseCuDNN
:是否使用CuDNN
<CuDnnPath></CuDnnPath>
賦值,即下載的CuDNN的路徑CudaVersion
:CUDA的版本
PythonSupport
:是否支持python
<PythonDir>C:\Miniconda2\</PythonDir>
賦值
D:\program file\Anaconda2
,那麼,應該令 <PythonDir>D:\program file\Anaconda2</PythonDir>
MatlabSupport
:是否支持matlab
<MatlabDir>C:\Program Files\MATLAB\R2014b</MatlabDir>
進行賦值:
D:\Program Files\MATLAB\R2014b
,那麼,<MatlabDir>D:\Program Files\MATLAB\R2014b</MatlabDir>
配置文件部分設置以下:
<PropertyGroup Label="UserMacros"> <BuildDir>$(SolutionDir)..\Build</BuildDir> <!--NOTE: CpuOnlyBuild and UseCuDNN flags can't be set at the same time.--> <CpuOnlyBuild>true</CpuOnlyBuild> <UseCuDNN>false</UseCuDNN> <CudaVersion>7.5</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>false</PythonSupport> <!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be set to the root of your Matlab installation. --> <MatlabSupport>false</MatlabSupport> <CudaDependencies></CudaDependencies> <!-- Set CUDA architecture suitable for your GPU. Setting proper architecture is important to mimize your run and compile time. --> <CudaArchitecture>compute_35,sm_35;compute_52,sm_52</CudaArchitecture> <!-- CuDNN 3 and 4 are supported --> <CuDnnPath></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)\cuda\lib\x64;$(LibraryPath)</LibraryPath> <IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup> <OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir> <IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir> </PropertyGroup> <PropertyGroup> <LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath> <IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup Condition="'$(PythonSupport)'=='true'"> <PythonDir>C:\Miniconda2\</PythonDir> <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath> <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup Condition="'$(MatlabSupport)'=='true'"> <MatlabDir>C:\Program Files\MATLAB\R2014b</MatlabDir> <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath> <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath> </PropertyGroup>
配置文件部分設置以下:
<PropertyGroup Label="UserMacros"> <BuildDir>$(SolutionDir)..\Build</BuildDir> <!--NOTE: CpuOnlyBuild and UseCuDNN flags can't be set at the same time.--> <CpuOnlyBuild>false</CpuOnlyBuild> <UseCuDNN>ture</UseCuDNN> <CudaVersion>7.5</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> <CudaDependencies></CudaDependencies> <!-- Set CUDA architecture suitable for your GPU. Setting proper architecture is important to mimize your run and compile time. --> <CudaArchitecture>compute_35,sm_35;compute_52,sm_52</CudaArchitecture> <!-- CuDNN 3 and 4 are supported --> <CuDnnPath>D:\software\caffe-vs\cuda-cuDNN</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)\cuda\lib\x64;$(LibraryPath)</LibraryPath> <IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup> <OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir> <IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir> </PropertyGroup> <PropertyGroup> <LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath> <IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup Condition="'$(PythonSupport)'=='true'"> <PythonDir>D:\File Program\Anaconda\</PythonDir> <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath> <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath> </PropertyGroup> <PropertyGroup Condition="'$(MatlabSupport)'=='true'"> <MatlabDir>D:\File Program\Matlab\</MatlabDir> <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath> <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath> </PropertyGroup>
注:事實上,對cuDNN路徑的配置有兩種方案:
- 方案1:將cuDNN v4的壓縮包內的bin、include、lib中的文件分別放置在%CUDA_PATH% (即cuda的安裝路徑)的相應目錄中
- 方案2:將 .\windows\CommonSettings.props文件中的CuDnnPath設置爲該解壓縮路徑,例如,將cuDNN解壓後文件放置在D:\software\caffe-vs\cuda-cuDNN
中,那麼,在CommonSettings.props中,令<CuDnnPath>D:\software\caffe-vs\cuda-cuDNN\</CuDnnPath>
,即上面提到的方法
利用VS2013打開名稱爲Caffe的解決方案
首先編譯libcaffe項目(其餘的項目依賴於libcaffe項目)
右鍵libcaffe,選擇生成,開始編譯libcaffe
…
…
還原NuGet包,即從網絡下載編譯須要的各類依賴包
通過一段時間等待後,編譯成功
這裏有兩個須要注意的地方:
(1)編譯過程當中,可能會出現錯誤
error C2220: 警告被視爲錯誤 - 沒有生成「object」文件
錯誤的緣由是編譯過程當中出現了警告,而致使編譯沒法進行,此時,須要進行以下修改:
即:設置項目屬性,不要將警告視爲錯誤
右鍵->項目屬性->C/C++->常規->將警告視爲錯誤 設爲否
另外,有一次在另一個機器上出現了一樣的問題,但按照上述方法卻未解決,是由於文件的代碼頁爲英文,而個人系統中的代碼頁爲中文,解決方案:利用VS打開出錯的文件->從新保存->重啓vs->從新編譯libcaffe->問題解決。
(2)caffe編譯過程當中,須要一些依賴包,在有網絡的狀況下,當編譯第一個項目libcaffe時,程序會自動從網路下載這些依賴包,下載到與caffe-windows文件夾同級的NugetPackages文件夾中,下圖所示即爲程序自動下載的依賴包
但在沒有網絡狀況下,須要事先將這些依賴包下載好,並將它們的路徑在vs的管理NuGet程序包中進行設置:工具->選項->NuGet Package Manager->程序包源->利用右側的加號添加本地的package
…
手動添加這些package地址後,還須要手動對這些NuGet Package進行安裝
(3)另外,第二次編譯過程出現以下錯誤
發現是下載的NegetPackages\glog.0.3.3.0下載缺乏兩個文件:
glog.overlay-x64_v120_Release_dynamic.0.3.3.0
glog.overlay-x64_v120_Debug_dynamic.0.3.3.0
一樣,在caffe項目處右鍵生成
直接在項目pycaffe
右鍵,點擊生成便可
直接在項目matcaffe
右鍵,點擊生成便可
第二次在編譯matcaffe時,出現以下錯誤:
上面這個問題解決後,又出現了另一個錯誤
\extern\lib\win64\microsoft
添加到matcaffe項目->連接器->常規->附加目錄中,而後再次編譯,成功;這是由於編譯程序沒法找到matlab的相關靜態連接庫的緣由;後來通過仔細查詢,發現,原來是由於配置文件CommonSettings中的matlabDir不當心填寫錯誤了,纔會致使上面兩個問題!將此處改正,上面2個問題成功解決,matcaffe編譯成功
<PropertyGroup Condition="'$(MatlabSupport)'=='true'"> <MatlabDir>D:\Program Files\MATLAB\R2014b</MatlabDir> <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath> <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath> </PropertyGroup>
接下來,再依次編譯其餘項目,一樣的方法
classification
:用來分類compute_image_mean
:計算均值convert_cifar_dat
:對cifat數據集進行轉換convert_mnist_data
:對mnist數據集進行轉換爲了測試編譯好的caffe可否正常使用,須要進行測試,主要利用mnist數據集進行分類問題的測試
(1)下載測試數據集
(2)修改配置文件
進入文件夾D:\software\caffe-vs\caffe-windows\examples\mnist
,即examples路徑下的mnist文件夾
① 打開lenet_solver.prototxt
設置網絡配置文件路徑,這裏使用了絕對路徑,相對路徑尚未嘗試
② 打開lenet_train_test.prototxt
(3)運行caffe程序,具體地
① 打開cmd命令行,cd到caffe-windows的.\Build\x64\Debug
路徑下,在該路徑下,能夠直接執行caffe.exe
D:\software\caffe-vs\caffe-windows\Build\x64\Debug
或者,爲了方便,能夠將caffe的路徑添加到環境變量Path中,這樣,cmd就能夠直接識別caffe命令了
② 運行caffe
caffe train -solver lenet_solver.prototxt
注意,由於上一步已經將D:\software\caffe-vs\caffe-windows\Build\x64\Debug
路徑添加到環境變量PATH中了,因此,這裏能夠直接使用caffe.exe
下面兩步主要配置cuDnn、python和matlab,具體配置時,應該在4.5以前進行
若是已經編譯好pycaffe,那麼,爲了可以在python使用caffe,還須要在python中進行一些相關配置
若是已經編譯好matcaffe,那麼,爲了可以在matlab使用caffe,還須要在matlab中進行一些相關配置
添加環境變量
<caffe_root>\Build\x64\Release\matcaffe
路徑添加到matlab的搜索路徑中<caffe_root>\Build\x64\Release
路徑添加到環境變量PATH中After you have built solution with Matlab support, in order to use it you have to:
add the generated matcaffe folder to Matlab search path, and
add \Build\x64\Release to your system path.
最後來張圖