用caffe训练自己的数据集(二)
本文主要参考了:https://blog.****.net/heimu24/article/details/53581362
https://blog.****.net/gaohuazhao/article/details/69568267
五、构建网络
在myfile4下建立一个.prototxt文件,来搭建网络。文件名为:myfile4_train_test.prototxt 构建的网络内容如下:
name: "myfile4" layer { name: "data" type: "Data" top: "data" top: "label" include { phase: TRAIN } transform_param{ mean_file:"examples/myfile4/mean.binaryproto" } data_param { source: "examples/myfile4/img_train_lmdb" batch_size: 50 backend: LMDB } } layer { name: "data" type: "Data" top: "data" top: "label" include { phase: TEST } transform_param{ mean_file:"examples/myfile4/mean1.binaryproto" } data_param { source: "examples/myfile4/img_val_lmdb" batch_size: 50 backend: LMDB } } layer { name: "conv1" type: "Convolution" bottom: "data" top: "conv1" param { lr_mult: 1 } param { lr_mult: 2 } convolution_param { num_output: 32 pad:2 kernel_size: 5 stride: 1 weight_filler { type:"gaussian" std:0.0001 } bias_filler { type: "constant" } } } layer { name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layer { name:"relu1" type:"ReLU" bottom:"pool1" top:"pool1" } layer { name: "conv2" type: "Convolution" bottom: "pool1" top: "conv2" param { lr_mult: 1 } param { lr_mult: 2 } convolution_param { num_output: 32 pad:2 kernel_size: 5 stride: 1 weight_filler { type: "gaussian" std:0.01 } bias_filler { type: "constant" } } } layer { name:"relu2" type:"ReLU" bottom:"conv2" top:"conv2" } layer { name: "pool2" type: "Pooling" bottom: "conv2" top: "pool2" pooling_param { pool: AVE kernel_size: 3 stride: 2 } } layer { name:"conv3" type:"Convolution" bottom:"pool2" top:"conv3" param{ lr_mult:1 } param{ lr_mult:2 } convolution_param { num_output:64 pad:2 kernel_size:5 stride:1 weight_filler { type:"gaussian" std:0.01 } bias_filler{ type:"constant" } } } layer { name:"relu3" type:"ReLU" bottom:"conv3" top:"conv3" } layer { name:"pool3" type:"Pooling" bottom:"conv3" top:"pool3" pooling_param { pool:AVE kernel_size:3 stride:2 } } layer { name: "ip1" type: "InnerProduct" bottom: "pool3" top: "ip1" param { lr_mult: 1 } param { lr_mult: 2 } inner_product_param { num_output: 64 weight_filler { type: "gaussian" std:0.1 } bias_filler { type: "constant" } } } layer { name: "ip2" type: "InnerProduct" bottom: "ip1" top: "ip2" param { lr_mult: 1 } param { lr_mult: 2 } inner_product_param { num_output: 10 weight_filler { type: "gaussian" std:0.1 } bias_filler { type: "constant" } } } layer { name:"accuracy" type:"Accuracy" bottom:"ip2" bottom:"label" top:"accuracy" include { phase:TEST } } layer { name: "loss" type: "SoftmaxWithLoss" bottom: "ip2" bottom: "label" }
接下来要设置网络的训练形式,也是建立一个.prototxt文件。文件名为myfile4_solver.prototxt,内容如下:
net: "examples/myfile4/myfile4_train_test.prototxt" test_iter: 2 test_interval: 50 base_lr: 0.001 lr_policy: "step" gamma: 0.1 stepsize:400 momentum:0.9 weight_decay:0.004 display:10 max_iter: 2000 snapshot: 2000 snapshot_prefix: "examples/myfile4" solver_mode: CPU
在caffe的根目录下执行 build/tools/caffe train -solver examples/myfile4/myfile4_solver.prototxt
就会得到想要的模型
注意:有的训练网络TEST,有的训练网络不带测试,就是加不加下面这段
如果不加这段的网络,需要在solver文件中把有关测试的设置去掉否则会出错。需要去掉的语句是test_iter还有test_interval等语句。
至此训练也已经完成,接下来需要做的就是用已经训练好的模型去识别想要识别的数据。