windows7+visual studio 2013+CUDA7.5 编译caffe+配置matcaffe+配置pycaffe

通过朋友指导,终于成功在windows7上成功编译了caffe,这里将编译过程记录python

 

 

1. 安装文件准备

1.1 visual studio 2013安装包下载

  • 进入visual studio下载页
  • 选择Visual Studio 2013–>Ultimate 2013版–>简体中文iso文件–>下载 
    这里写图片描述

1.2 CUDA7.5 (optional)

若是不须要cuda版本的caffe,本步跳过git

1.3 windows版本caffe

1.4 下载cuDNN (optional)

  • 若是不须要配置cuDNN,该步请跳过
  • nVidia官网下载cuDNN v3或者是cuDNN v4,注意,须要注册才能下载 
    下载后解压缩,以下图所示 
    这里写图片描述

1.5 下载Anaconda安装包 (optional)

  • 该安装包是用来安装python的,从而能够配置pycaffe
  • 这里选择的是利用Anaconda的方法按照python,因此才须要下载Anaconda安装包,固然,也能够按照其余的方法安装
  • 若是不须要配置pycaffe,该步请跳过
  • 通常状况下,可能你们的机器上都已经安装了python,那么,也请跳过该步
  • Anaconda安装包下载地址:https://www.continuum.io/downloads

1.6 下载Matlab安装包 (optional)

  • 该安装包是用来安装matlab的,从而能够配置matcaffe
  • 若是不须要配置matcaffe ,该步请跳过
  • 通常状况下,可能你们的机器上都已经安装了matlab,那么,也请跳过该步
  • matlab下载地址这里就不列出了,请自行寻找

2.安装visual studio 2013

具体安装方法见: 安装visual studio 2013github

3.安装cuda7.5 (optional)

4. 利用Anaconda安装python (optional)

若是你的本机没有安装Python,而且你须要配置pycaffe,那么,请按照win7系统安装python,并按照Pycharm集成开发环境安装python工具

安装完python后,再安装一下protobuf,由于后面编译pycaffe时有须要:visual-studio

  • 利用pip工具安装protobuf:pip install protobuf 
    这里写图片描述

5. 安装matlab (optional)

若是你的本机没有安装matlab ,而且你须要配置matcaffe,那么,请安装matlab,具体安装方法请自行查阅测试

6.修改配置文件

在github上下载的window caffe把呢你自带一个配置文件的例子,在进行编译以前,须要先更改该文件内容ui

6.1 解压缩下载的caffe-windows文件

这里假设caffe-windows被解压到了以下的文件夹中 
这里写图片描述spa

6.2 进入到windows文件夹

这里写图片描述

6.3 复制配置文件,并重命名

将该文件夹(即.\windows)下的CommonSettings.props.example文件复制一份儿,并将复制的文件命名为CommonSettings.props 
这里写图片描述

6.4 修改配置文件(修改工程的属性文件)

6.4.1 配置文件说明

  • caffe-windows自带的配置文件CommonSettings.props.example中与咱们后面要进行的编译密切相关的就是以下部分 
    这里写图片描述

  • 例子中全部配置参数都是默认值

  • CpuOnlyBuild:是否使用CPU

    • 默认值为False(即便用GPU编译)
    • 若是本机没有配置好CUDA,那么,该值应该赋值为True;
    • 若是本机已经配置好CUDA,而且须要编译CUDA版本的caffe,那么,该值应该赋值为True、
  • UseCuDNN:是否使用CuDNN

    • 默认值为True;
    • 若是本机没有配置好CUDA,那么,该值应该赋值为False;
    • 注意:CpuOnlyBuild和CuDNN不能同时为True;
    • 若是这里设置为True,那么还须要对<CuDnnPath></CuDnnPath>赋值,即下载的CuDNN的路径
  • CudaVersion:CUDA的版本

  • PythonSupport:是否支持python

    • 默认值为False,即后续不编译pycaffe;
    • 若是须要编译pycaffe,那么该值设置为True
    • 若是这里设置为true,那么还须要对 <PythonDir>C:\Miniconda2\</PythonDir>赋值 
      • 上面的值是默认的,应该修改成本机的python安装路径,也就是python.exe所在路径,例如,本机是利用Anaconda安装的python,路径以下:D:\program file\Anaconda2 ,那么,应该令 <PythonDir>D:\program file\Anaconda2</PythonDir> 
        这里写图片描述
  • MatlabSupport:是否支持matlab

    • 默认值为False;
    • 若是须要编译matcaffe,那么该值设置为True
    • 若是该值设置为true,那么还须要对<MatlabDir>C:\Program Files\MATLAB\R2014b</MatlabDir>进行赋值: 
      • 上面的路径是默认的,须要更改成本机的matlab安装目录,例如,本机的matlab安装路径以下:D:\Program Files\MATLAB\R2014b,那么,<MatlabDir>D:\Program Files\MATLAB\R2014b</MatlabDir> 
        这里写图片描述

6.4.2 非CUDA版本的caffe

配置文件部分设置以下:

<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>

6.4.3 CUDA版本的caffe

配置文件部分设置以下:

<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>,即上面提到的方法

7 编译caffe

7.1 打开名称为Caffe的解决方案

利用VS2013打开名称为Caffe的解决方案 
这里写图片描述

7.2 编译libcaffe项目

  • 首先编译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 
      将第一次下载获得的这两个文件放入该文件夹,问题解决 
      注:这里的“第一次下载获得的这两个文件”是指NugetPackages中的glog.0.3.3.0文件夹下的两个文件,见下图 
      这里写图片描述

7.3 编译caffe项目

一样,在caffe项目处右键生成

7.4 编译pycaffe

直接在项目pycaffe右键,点击生成便可

7.5 编译matcaffe

直接在项目matcaffe右键,点击生成便可

  • 第二次在编译matcaffe时,出现以下错误: 
    这里写图片描述

    • 即编译matcaffe提示说找不到mex.h
    • 将matlab_root/extend/include目录添加到matcaffe项目的include目录中,问题解决
  • 上面这个问题解决后,又出现了另一个错误 
    这里写图片描述

    • 即连接器找不到libmx.lib(matlab的静态连接库),查看VC++库目录,没有发现设置问题 
      这里写图片描述
    • 不知道为何会这样, 想了一个临时的解决方便,将matlab安装路径下的\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>
  •  

这里写图片描述

7.6 编译其余项目

接下来,再依次编译其余项目,一样的方法

  • classification:用来分类
  • compute_image_mean:计算均值
  • convert_cifar_dat:对cifat数据集进行转换
  • convert_mnist_data:对mnist数据集进行转换

8.运行第一个caffe测试程序

为了测试编译好的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
  • 1
  • 1

注意,由于上一步已经将D:\software\caffe-vs\caffe-windows\Build\x64\Debug路径添加到环境变量PATH中了,因此,这里能够直接使用caffe.exe

下面两步主要配置cuDnn、python和matlab,具体配置时,应该在4.5以前进行

9. 配置python (optional)

若是已经编译好pycaffe,那么,为了可以在python使用caffe,还须要在python中进行一些相关配置

  • 添加环境变量 
    • 在环境变量的用户变量中,新建用户变量,变量名“PythonPath”,变量值“caffe_root\Build\x64\Release\pycaffe”
    • 或者将文件夹\Build\x64\Release\pycaffe\caffe 复制到\lib\site-packages.

10.配置matlab (optional)

若是已经编译好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.

    这里写图片描述

最后来张图

这里写图片描述

相关文章
相关标签/搜索