Caffe编译(Win10+Vs2015+Cuda8.0)

参考博客


  1. 编译
    • Windows下VS2015编译caffe
    • caffe window10 安装 Caffe在window10+VS2015安装
  2. 测试minist数据集
    • CAFFE学习笔记(一)Caffe_Example之训练mnist

环境


  1. Win10
  2. Vs2015 or Vs2013
  3. cmake-3.7.2-win64-x64.zip
  4. cudnn-8.0-windows10-x64-v5.1
  5. Cuda8.0
  6. Anaconda2-4.3.0.1-Windows-x86_64 (Python2.7 or 3.5)
  7. Git-2.12.0-64-bit
  8. Caffe-Windows源码

编译步骤


  1. 安装上述所有环境
  2. 配置环境变量
    • cmake的bin目录加入path
    • cudnn的cuda下bin目录加入path
    • Anaconda安装目录加入path(python就在根目录下)
  3. 解压Caffe-Windows,最好不要含有中文路径
  4. 进入…\caffe-windows\scripts,用文本编辑器如Notepad++打开build_win.cmd文件(我的目录F:\vs2015\caffe\caffe-windows\scripts\build_win.cmd),下面进行修改编译配置:

          我的配置如下,如果你是Vs2013,把MSVC_VERSION=14改为12。WITH_NINJA=0,除非你想用ninja编译器来进行编译。Python版本设置为2或者3,取决于装的版本,这里编译只支持2.7或者3.5版本,其他不用怎么改变。

    else (
        :: Change the settings here to match your setup
        :: Change MSVC_VERSION to 12 to use VS 2013
        if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14
        :: Change to 1 to use Ninja generator (builds much faster)
        if NOT DEFINED WITH_NINJA set WITH_NINJA=0
        :: Change to 1 to build caffe without CUDA support
        if NOT DEFINED CPU_ONLY set CPU_ONLY=0
        :: Change to Debug to build Debug. This is only relevant for the Ninja generator the Visual Studio generator will generate both Debug and Release configs
        if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
        :: Set to 1 to use NCCL
        if NOT DEFINED USE_NCCL set USE_NCCL=0
        :: Change to 1 to build a caffe.dll
        if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=0
        :: Change to 3 if using python 3.5 (only 2.7 and 3.5 are supported)
        if NOT DEFINED PYTHON_VERSION set PYTHON_VERSION=2
        :: Change these options for your needs.
        if NOT DEFINED BUILD_PYTHON set BUILD_PYTHON=1
        if NOT DEFINED BUILD_PYTHON_LAYER set BUILD_PYTHON_LAYER=1
        if NOT DEFINED BUILD_MATLAB set BUILD_MATLAB=0
        :: If python is on your path leave this alone
        if NOT DEFINED PYTHON_EXE set PYTHON_EXE=python
        :: Run the tests
        if NOT DEFINED RUN_TESTS set RUN_TESTS=0
        :: Run lint
        if NOT DEFINED RUN_LINT set RUN_LINT=0
        :: Build the install target
        if NOT DEFINED RUN_INSTALL set RUN_INSTALL=0
    )
    
  5. 修改完build_win.cmd保存好后,打开cmd命令提示符,进入该文件目录,执行

    F:\vs2015\caffe\caffe-windows\scripts>build_win.cmd
    

    等待编译,先下载依赖包,然后进行caffe编译,大约20-30分钟编译成功。最后结果大致:

      CUSTOMBUILD : -- warning : gp_resolved_file_type non-absolute file 'api-ms-win-crt-string-l1-1-0.dll' returning type
    'system' -- possibly incorrect [F:\vs2015\caffe\caffe-windows\scripts\build\tools\upgrade_solver_proto_text_install_pre
    requisites.vcxproj]
      CUSTOMBUILD : warning : cannot resolve item 'api-ms-win-crt-heap-l1-1-0.dll' [F:\vs2015\caffe\caffe-windows\scripts\b
    uild\tools\upgrade_solver_proto_text_install_prerequisites.vcxproj]
      CUSTOMBUILD : -- warning : gp_resolved_file_type non-absolute file 'api-ms-win-crt-heap-l1-1-0.dll' returning type 's
    ystem' -- possibly incorrect [F:\vs2015\caffe\caffe-windows\scripts\build\tools\upgrade_solver_proto_text_install_prere
    quisites.vcxproj]
      CUSTOMBUILD : warning : cannot resolve item 'api-ms-win-crt-stdio-l1-1-0.dll' [F:\vs2015\caffe\caffe-windows\scripts\
    build\tools\upgrade_solver_proto_text_install_prerequisites.vcxproj]
      CUSTOMBUILD : -- warning : gp_resolved_file_type non-absolute file 'api-ms-win-crt-stdio-l1-1-0.dll' returning type '
    system' -- possibly incorrect [F:\vs2015\caffe\caffe-windows\scripts\build\tools\upgrade_solver_proto_text_install_prer
    equisites.vcxproj]
      CUSTOMBUILD : warning : cannot resolve item 'api-ms-win-crt-convert-l1-1-0.dll' [F:\vs2015\caffe\caffe-windows\script
    s\build\tools\upgrade_solver_proto_text_install_prerequisites.vcxproj]
      CUSTOMBUILD : -- warning : gp_resolved_file_type non-absolute file 'api-ms-win-crt-convert-l1-1-0.dll' returning type
     'system' -- possibly incorrect [F:\vs2015\caffe\caffe-windows\scripts\build\tools\upgrade_solver_proto_text_install_pr
    erequisites.vcxproj]
    
        13407 个警告
        0 个错误
    
    已用时间 00:19:12.74
    
  6. 编译完成后,会在…/caffe-windows/scripts目录下多出build文件夹,里面的Caffe.sln用Vs2015打开就可以了,如果想再编译,选择Release方式。在…/caffe-windows/scripts/build/tools/Release下有编译生成的Caffe.exe,使用它便可以测试数据集。

测试mnist数据集


  1. 下载mnist leveldb格式的数据集,包含mnist-train-leveldb以及mnist-test-leveldb。
  2. 在…\caffe-windows\examples\mnist目录下,有lenet_train.prototxt、lenet_solver.prototxt和train_lenet.sh文件。先明确作用:
    • 在lenet_train.prototxt中:
      • 给出用于训练的MNIST数据mnist-train-leveldb以及mnist-test-leveldb的路径;
      • 在此定义这个网络都有哪些层,给出每一层的定义。
    • 在lenet_solver.prototxt中:
      • 在此指明我们要训练的网络是lenet_train.prototxt,给出lenet_train.prototxt的路径;
      • 在此指定是用CPU还是GPU。
    • 文件train_lenet.sh,在里面给出:
      • caffe编译后生成的可执行文件exe的名称(带不带“.exe”都可以),后面跟“train”代表选择“训练”;
      • 刚才写好的lenet_solver.prototxt的路径。
  3. 在…\caffe-windows\scripts\build\tools\Release下新建一个文件夹,我的叫mnist_test。将mnist-test-leveldb、 mnist-train-leveldb 和…\caffe-windows\examples\mnist目录下的lenet_train_test.prototxt、lenet_solver.prototxt这四个文件全部拷贝到mnist_test文件夹中去。
  4. 设定prototxt文件,给出相应的路径:

    • lenet_train_test.prototxt,这个文件即之前说的lenet_train.prototxt,为了便于区分在后面加了一个_test。路径改变如下(修改部分截取,未修改部分没有贴上来):

      name: "LeNet"
      layer {
        name: "mnist"
        type: "Data"
        top: "data"
        top: "label"
        include {
          phase: TRAIN
        }
        transform_param {
          scale: 0.00390625
        }
        data_param {
          source: "mnist_test/mnist-train-leveldb"  ---------》修改部分
          batch_size: 64
          backend: LEVELDB  ---------》修改部分
        }
      }
      layer {
        name: "mnist"
        type: "Data"
        top: "data"
        top: "label"
        include {
          phase: TEST
        }
        transform_param {
          scale: 0.00390625
        }
        data_param {
          source: "mnist_test/mnist-test-leveldb"   ---------》修改部分
          batch_size: 100
          backend: LEVELDB  ---------》修改部分
        }
      }
      ......
      .....
      
    • lenet_solver.prototxt 设置相对路径:

      # The train/test net protocol buffer definition
      net: "mnist_test/lenet_train_test.prototxt"  ---------》修改部分
      # test_iter specifies how many forward passes the test should carry out.
      # In the case of MNIST, we have test batch size 100 and 100 test iterations,
      # covering the full 10,000 testing images.
      test_iter: 100
      # Carry out testing every 500 training iterations.
      test_interval: 500
      # The base learning rate, momentum and the weight decay of the network.
      base_lr: 0.01
      momentum: 0.9
      weight_decay: 0.0005
      # The learning rate policy
      lr_policy: "inv"
      gamma: 0.0001
      power: 0.75
      # Display every 100 iterations
      display: 100
      # The maximum number of iterations
      max_iter: 10000
      # snapshot intermediate results
      snapshot: 5000
      snapshot_prefix: "mnist_test/lenet"   ---------》修改部分
      # solver mode: CPU or GPU
      solver_mode: GPU    ---------》使用GPU
      
  5. 打开cmd,进入caffe.exe的目录:

        C:\Users\Mr.sorrow>f:
    
        F:\>cd F:\vs2015\caffe\caffe-windows\scripts\build\tools\Release
    
        F:\vs2015\caffe\caffe-windows\scripts\build\tools\Release>
    

    运行:

        caffe.exe train --solver=mnist_test/lenet_solver.prototxt
    

    结果:

        .......
        .......
        I0309 13:46:14.446481  3708 solver.cpp:219] Iteration 9600 (165.724 iter/s, 0.603412s/100 iters), loss = 0.0029485
        I0309 13:46:14.446481  3708 solver.cpp:238]     Train net output #0: loss = 0.0029485 (* 1 = 0.0029485 loss)
        I0309 13:46:14.447450  3708 sgd_solver.cpp:105] Iteration 9600, lr = 0.00603682
        I0309 13:46:15.059074  3708 solver.cpp:219] Iteration 9700 (164.181 iter/s, 0.609083s/100 iters), loss = 0.00178553
        I0309 13:46:15.059074  3708 solver.cpp:238]     Train net output #0: loss = 0.00178552 (* 1 = 0.00178552 loss)
        I0309 13:46:15.060080  3708 sgd_solver.cpp:105] Iteration 9700, lr = 0.00601382
        I0309 13:46:15.664719  3708 solver.cpp:219] Iteration 9800 (165.979 iter/s, 0.602486s/100 iters), loss = 0.0119374
        I0309 13:46:15.665698  3708 solver.cpp:238]     Train net output #0: loss = 0.0119374 (* 1 = 0.0119374 loss)
        I0309 13:46:15.665698  3708 sgd_solver.cpp:105] Iteration 9800, lr = 0.00599102
        I0309 13:46:16.286525  3708 solver.cpp:219] Iteration 9900 (161.895 iter/s, 0.617683s/100 iters), loss = 0.00379853
        I0309 13:46:16.286990  3708 solver.cpp:238]     Train net output #0: loss = 0.00379854 (* 1 = 0.00379854 loss)
        I0309 13:46:16.287490  3708 sgd_solver.cpp:105] Iteration 9900, lr = 0.00596843
        I0309 13:46:16.890439  3708 solver.cpp:448] Snapshotting to binary proto file mnist_test/mnist/lenet_iter_10000.caffemodel
        I0309 13:46:16.903506  3708 sgd_solver.cpp:273] Snapshotting solver state to binary proto file mnist_test/mnist/lenet_iter_10000.solverstate
        I0309 13:46:16.910498  3708 solver.cpp:311] Iteration 10000, loss = 0.00267322
        I0309 13:46:16.910498  3708 solver.cpp:331] Iteration 10000, Testing net (#0)
        I0309 13:46:17.131078  7880 data_layer.cpp:73] Restarting data prefetching from start.
        I0309 13:46:17.140102  3708 solver.cpp:398]     Test net output #0: accuracy = 0.99
        I0309 13:46:17.140102  3708 solver.cpp:398]     Test net output #1: loss = 0.0297552 (* 1 = 0.0297552 loss)
        I0309 13:46:17.143108  3708 solver.cpp:316] Optimization Done.
        I0309 13:46:17.143108  3708 caffe.cpp:260] Optimization Done.
    

    在….\caffe-windows\scripts\build\tools\Release\mnist_test目录下,即生成四个文件:

    • lenet_iter_5000.caffemodel
    • lenet_iter_5000.solverstate
    • lenet_iter_10000.caffemodel
    • lenet_iter_10000.solverstate

你可能感兴趣的:(深度学习)