caffe的基础使用

6 分钟阅读

一、环境搭建

1、安装依赖库

$ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
$ sudo apt-get install --no-install-recommends libboost-all-dev
$ sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
$ sudo apt-get install libatlas-base-dev
$ sudo apt-get install python-dev

2、下载编译Caffe

  • 下载caffe源码:caffe

  • 修改配置文件,cp Makefile.config.example Makefile.config,修改Makefile.config:

    CPU_ONLY := 1
    INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
    LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
    
  • 编译Caffe

    make all
    make test
    make runtest
    

3、样本使用

  • 准备数据集

    $ ./data/mnist/get_mnist.sh
    $ ./examples/mnist/creat_mnist.sh
    
  • examples/mnist/lenet_solver.prototxt中的GPU改成CPU

  • 执行脚本./examples/mnist/train_lenet.sh

二、使用流程

1、定义数据格式处理

将原始图片处理成caffe支持的格式,使用caffe中的convert_imageset工具将原始图片转换成LevelDB或者Lmdb格式。

2、生成均值文件

通常会进行图片减去均值再训练,提高训练速度和精度。由compute_image_mean.cpp生成的工具,使用如下:

$ ./build/tools/compute_image_mean examples/mynet/mynet_train_lmdb examples/mynet/mean.binaryproto

3、定义网络结构文件

定义网络模型,比如mnist/lenet_train_test.prototxt的定义如下:

name: "LeNet"
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
    mean_file: "examples/myfile/mean.binaryproto"
  }
  data_param {
    source: "examples/mnist/mnist_train_lmdb"
    batch_size: 64
    backend: LMDB
  }
}
......

4、定义网络求解文件

定义了网络模型训练过程中需要设置的参数,比如学习率,权重衰减系数,迭代次数,使用GPU还是CPU等。比如mnist/lenet_solver.prototxt定义如下:

# The train/test net protocol buffer definition
net: "examples/mnist/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: "examples/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: CPU
  • 一次迭代指的是一个batch,不是一张图片
  • test_iter表示测试迭代次数,假如test数据集有10000个样本,test batch_size为50,则test_iter配置为200才可以把测试样本测完
  • test_interval测试间隔,表示每迭代多少次后进行一次测试。比如test_interval为500,则表示每训练500次(batch_size为单位),进行一次测试。该测试会完成test_iter的迭代次数。即训练过程是边训练边测试的。
  • max_iter表示训练的最大迭代次数,太大太小都不好。

定义训练网络和测试网络有2种方式:方式一是在网络模式中根据phase区分,定义在一个网络中,(如上);方式二是分开定义到2个网络模式中,如下:

train_net: "examples/hdf5_classification/nonlinear_auto_train.prototxt"
test_net: "examples/hdf5_classification/nonlinear_auto_test.prototxt"

5、训练与测试

主要方式有命令行、python、matlab三种方式,命令行方式如下:

  • 训练

    # 训练示例 (参数: 求解文件)
    caffe train -solver examples/mnist/lenet_solver.prototxt
    # 从训练一半的模型快照中恢复训练 (参数:求解文件 快照)
    caffe train -solver examples/mnist/lenet_solver.prototxt -snapshot examples/mnist/lenet_iter_5000.solversta
    # 由其它训练好的模型 fine-tune  (参数:求解文件 其它训练好的模型参数)
    caffe train -solver examples/finetuning_on_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel
    
  • 测试

    # 测试 (参数: 求解文件 训练好的模型参数 )
    caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 100
    

其他

  • 查看模型结构工具:netron

评论