## Define the MNIST Network name: "LeNet" // 网络(Net)的名称
### Writing the Data Layer layer { // 定义一个层(Layer) name: "mnist" // 层的名称为mnist type: "Data" // 层的类型为数据层 top: "data" // 层的输出Blob有两个:data和label top: "label" include { phase: TRAIN // 该层参数只在训练阶段有效 } transform_param { scale: 0.00390625 // 数据变换使用的数据缩放因子 # mean_file: "../../examples/cifar10/mean.binaryproto" // 均值文件;(图像像素值-均值) } data_param { // 数据层参数 source: "examples\\mnist\\mnist_train_lmdb" //LMDB路径,一定要配置好相对路径 batch_size: 64 // 批量数据,一次读取64张图 backend: LMDB // 数据格式为LMDB } } layer { // 一个新数据层,名字也叫mnist,输出blob也是data和label,但这里定义的参数只在分类阶段有效 name: "mnist" type: "Data" top: "data" top: "label" include { phase: TEST } transform_param { scale: 0.00390625 } data_param { source: "..\\..\\examples\\mnist\\mnist_test_lmdb" batch_size: 100 backend: LMDB } } layer { name: "data" type: "Input" top: "data" input_param { shape: { dim: 10 dim: 3 dim: 227 dim: 227 } } }
### Writing the Convolution Layer layer { // 定义一个新的卷积层conv1,输入blob为data,输出为conv1 name: "conv1" type: "Convolution" bottom: "data" top: "conv1" param { lr_mult: 1 // 权值学习速率倍乘因子,1倍表示保持与全局参数一致;该层lr=lr_mult*base_lr decay_mult: 1 // 权重的衰减值的倍乘因子 } param { lr_mult: 2 // bias学习速率倍乘因子,是全局参数的2倍;一般偏置项的学习率是权值学习率的两倍。 decay_mult: 1 // 权重的衰减值倍乘因子 } convolution_param { // 卷积计算参数 num_output: 20 // 输出feature map数目为20 kernel_size: 5 // 卷积核尺寸,5*5 stride: 1 // 卷积输出跳跃间隔(即滑动窗口平移步进量),1表示连续输出,无跳跃 # pad:2 // 扩充边缘,默认为0,不扩充。 扩充的时候是左右、上下对称的,比如卷积核的大小为5*5,那么pad设置为2,则四个边缘都扩充2个像素,即宽度和高度都扩充了4个像素,这样卷积运算之后的特征图就不会变小。也可以通过pad_h和pad_w来分别设定。 bias_term: ture // 是否开启偏置项,默认为true, 开启 # group:2 // 分组,默认为1组。如果大于1,我们限制卷积的连接操作在一个子集内。如果我们根据图像的通道来分组,那么第i个输出分组只能与第i个输入分组进行连接。 weight_filler { // 权值使用xavier填充器 type: "xavier" # type: "gaussian" # std: 0.0001 // 权值使用gaussian填充器 } bias_filler { // bias使用常数填充器,默认为0 type: "constant" value:0 //可以指定填充值 } } }
### Writing the ReLU Layer layer { // 新的非线性层,用ReLU方法 name: "relu1" type: "ReLU" bottom: "conv1" top: "conv1" }
### Writing the Pooling Layer layer { // 定义新的下采样层pool1,输入blob为conv1,输出blob为pool1 name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { // 下采样参数 pool: MAX // 使用最大值下采样方法;有三种池化方法:MAX,AVG,STOCHASTIC kernel_size: 2 // 下采样窗口尺寸2*2 stride: 2 // 下采样输出跳跃间隔2*2(每次平移的步进量是2) } }
### Writing the Local Response Normalization(LRN) Layer 局部归一化层 layer { name: "norm1" type: "LRN" bottom: "pool1" top: "norm1" lrn_param { local_size: 3 // 对于cross channel LRN,表示需要求和的channel的数量;对于within channel LRN,表示需要求和的空间区域的边长。默认为5 alpha: 5e-05 beta: 0.75 norm_region: WITHIN_CHANNEL // 默认为ACROSS_CHANNELS。有两个选择,ACROSS_CHANNELS表示在相邻的通道间求和归一化。WITHIN_CHANNEL表示在一个通道内部特定的区域内进行求和归一化。与前面的local_size参数对应。 } }
### Writing the Fully Connected Layer layer { // 新的全连接层,输入blob为pool2,输出blob为ip1 name: "ip1" type: "InnerProduct" bottom: "pool2" top: "ip1" param { lr_mult: 1 # decay_mult: 250 // 损失函数中正则项的的系数,防止过拟合???? } param { lr_mult: 2 } inner_product_param { // 全连接层参数 num_output: 500 // 该层输出元素个数为500 weight_filler { type: "xavier" } bias_filler { type: "constant" } } }
### Writing the Accuracy Layer layer { // 分类准确率层,只在Testing阶段有效,输入blob为ip2和label,输出blob为accuracy,该层用于计算分类准确率。统计所有测试数据中的分类正确的概率 name: "accuracy" type: "Accuracy" bottom: "ip2" bottom: "label" top: "accuracy" include { phase: TEST } accuracy_param { top_k: 5 // 默认为top_1,添加该项后,选择测试top_k准确率。 } }
### Writing the Loss Layer layer { // 损失层,损失函数采用SoftmaxLoss,输入blob为ip2和label,输出blob为Loss name: "loss" type: "SoftmaxWithLoss" bottom: "ip2" bottom: "label" top: "loss" }
layer { name: "reshape" type: "Reshape" bottom: "input" top: "output" reshape_param { shape { dim: 0 # copy the dimension from below dim: 2 dim: 3 dim: -1 # infer it from the other dimensions } } }
reshape_param { shape { dim: 0 dim: 0 dim: 14 dim: -1 } }
layer { name: "drop7" type: "Dropout" bottom: "fc7-conv" top: "fc7-conv" dropout_param { dropout_ratio: 0.5 } }
## Define the MNIST Solver Check out the comments explaining each line in the prototxt `$CAFFE_ROOT/examples/mnist/lenet_solver.prototxt`: # The train/test net protocol buffer definition # 用于训练/预测的网络描述文件(ProtoBuffer文件格式) 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. # 也测阶段迭代次数。在MNIST例程下,预测样本组(test batch)大小为100; # 这里设置预测阶段迭代次数为100可以覆盖全部10000个测试集。 test_iter: 100 # Carry out testing every 500 training iterations. # 训练时每迭代500次,进行一次预测 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 # 每经过100次迭代,在屏幕上打印一次运行log display: 100 # The maximum number of iterations # 最大迭代次数 max_iter: 10000 # snapshot intermediate results # 每5000次迭代打印一次快照 snapshot: 5000 snapshot_prefix: "examples/mnist/lenet" # solver mode: CPU or GPU # caffe求解模式为CPU还是GPU solver_mode: GPU # 求解器类型,默认是SGD type: "AdaDelta" # 迭代次数,控制权值更新的次数,默认为1 iter_size:1
原文:https://www.cnblogs.com/aiguilai/p/15211804.html