Caffe修炼之路--模型定义

来源:互联网 发布:深圳知路科技有限公司 编辑:程序博客网 时间:2024/06/17 14:24

平台 Ubuntu14.04LTS

这篇博客主要是讲讲我学习caffe中LeNet MNIST示例的心得。

1、第一层:数据层

layers {  name: "mnist"  type: DATA  data_param {    source: "mnist_train_lmdb"    backend: LMDB    batch_size: 64    scale: 0.00390625  }  top: "data"  top: "label"}
这是第一个层,name指定当前层的名称。type指定当前层的类型,这里就是输入层,所以指定为DATA。data_param是指定当前层的参数,其中source就是输入数据的路径(可以写绝对路径),backend就是数据类型, batch_size是指定运行时的batch大小,scale是将数据进行尺寸变换,比如这里设置的0.00380625,就是1/256。第一个top是指定往后传递data,第二个是指定往后传递label.

2、第二层:卷积层

layers {  name: "conv1"  type: CONVOLUTION  blobs_lr: 1.  blobs_lr: 2.  convolution_param {    num_output: 20    kernelsize: 5    stride: 1    weight_filler {      type: "xavier"    }    bias_filler {      type: "constant"    }  }  bottom: "data"  top: "conv1"}

这里的type就是CONVOLUTION了(好像type指定的属性都是大写的)。blobs_lr: 1. 就是指定weight的学习率的倍数,这里就是1.0了,后面的blobs_lr: 2. 就是指定bias学习率的倍数。num_output指定输出数据个数,kernelsize是指定卷积模板的大小,也就卷积核的矩阵大小。stride就是指定卷积的步长。weight_filler是指定weight初始化,其中type是指定初始化的方式,这里用的是xavier 算法(根据输入输出的神经元个数自动决定初始化的尺度)。下面的bias_filler是类似的,constant就是说指定为常数了,默认为0.然后bottom就是指定这一层的输入数据,显然就是数据层传来的那个data,top就是输出数据。

为什么这一层只有一个bottom,但是上一层的top 有两个呢?label跑哪儿去了?哈哈,继续往下看你就明白了。

3、pooling 层

layers {  name: "pool1"  type: POOLING  pooling_param {    kernel_size: 2    stride: 2    pool: MAX  }  bottom: "conv1"  top: "pool1"}

这里出现的新参数就是那个pool了。pooling_param就是设置这个pooling层的参数啦,pool的方式就是max pooling。值得一提的是这里的kernel_size和stride的设置,这里恰好就相等,所以所有的pooling都不会出现重叠,一般来说,kernel_size的尺寸不小于stride的。

4、中间可以多添加几个卷积、pool层

5、完全连接层

layers {  name: "ip1"  type: INNER_PRODUCT  blobs_lr: 1.  blobs_lr: 2.  inner_product_param {    num_output: 500    weight_filler {      type: "xavier"    }    bias_filler {      type: "constant"    }  }  bottom: "pool2"  top: "ip1"}
这里指定了一个500个输出的完全连接层,没啥新的参数。

6、ReLU层

layers {  name: "relu1"  type: RELU  bottom: "ip1"  top: "ip1"}
这里有趣的地方在于输入和输出是同一个,官网上说只是因为这个ReLU操作可以原地操作(in place),能够节省内存。ReLU是一个替换sigmoid units的一个函数,全称是Rectified Linear Unit,是一个激活函数。wiki和豆瓣有些资料,可以自己看看。

7、完全连接层

layers {  name: "ip2"  type: INNER_PRODUCT  blobs_lr: 1.  blobs_lr: 2.  inner_product_param {    num_output: 10    weight_filler {      type: "xavier"    }    bias_filler {      type: "constant"    }  }  bottom: "ip1"  top: "ip2"}
8、Loss

layers {  name: "loss"  type: SOFTMAX_LOSS  bottom: "ip2"  bottom: "label"}
这里指定的Loss的定义方式是 SOFTMAX_LOSS。到这里也解开了我们心头的一个疑惑哈,为什么第一层的top有两个,哈哈,label是传递到这儿来的。

时间不早了,差不多模型建立就写这么多吧。希望大家积极留言,多多交流,共同进步。如果发现错误,一定要指出来哦!

———————————————————————2015. 2. 6更新————————————————————————————————

有个地方忘了说,就是在layer里面有个include参数,其中的phase参数是指定这一层是什么时候运行的,可以选择TRAIN,表示训练的时候运行,也可以选择TEST。

1 0