Caffe卷积层,池化层,全连接层定义

参考这个例子用Caffe在MNIST数据集上训练LeNet
网络结构域定义在这个prototxt文件中$CAFFE_ROOT/examples/mnist/lenet_train_test.prototxt

网络名

name: "LeNet"

数据输入层

layer {
  name: "mnist" #层名
  type: "Data" #类型
  transform_param {
    scale: 0.00390625 #1/255,像素点归一化到0~1
  }
  data_param {
    source: "mnist_train_lmdb"
    backend: LMDB
    batch_size: 64 #批尺寸
  }
  top: "data" #输出
  top: "label" #输出
}

卷积层

layer {
  name: "conv1"
  type: "Convolution" #卷积
  param { lr_mult: 1 } #学习率是solver里的1倍
  param { lr_mult: 2 } #学习率是2倍
  convolution_param {
    num_output: 20 #输出通道数
    kernel_size: 5 #卷积核尺寸
    stride: 1 #步长
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
  bottom: "data"
  top: "conv1"
}

池化层

layer {
  name: "pool1"
  type: "Pooling" #池化层
  pooling_param {
    kernel_size: 2
    stride: 2
    pool: MAX
  }
  bottom: "conv1"
  top: "pool1"
}

全连接层

layer {
  name: "ip1"
  type: "InnerProduct" #全连接层
  param { lr_mult: 1 }
  param { lr_mult: 2 }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
  bottom: "pool2"
  top: "ip1"
}

ReLu输出

layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}

再前连接层

layer {
  name: "ip2"
  type: "InnerProduct"
  param { lr_mult: 1 }
  param { lr_mult: 2 }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
  bottom: "ip1"
  top: "ip2"
}

代价函数

layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
}

Solver配置

$CAFFE_ROOT/examples/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: GPU

训练

./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt

推荐阅读更多精彩内容