caffe上手:修改已训练好的网络并训练模型

来源:互联网 发布:软件上线方案 编辑:程序博客网 时间:2024/05/28 05:16

前面做好了lmdb和均值文件,下面以Googlenet为例修改网络并训练模型。


我们将caffe-master\models下的bvlc_googlenet文件夹复制到caffe-master\examples\imagenet下。(因为我们的lmdb和均值都在这里,放一起方便些)

打开train_val.txt,修改:

1.修改data层:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. layer {  
  2.   name: "data"  
  3.   type: "Data"  
  4.   top: "data"  
  5.   top: "label"  
  6.   include {  
  7.     phase: TRAIN  
  8.   }  
  9.   transform_param {  
  10.     mirror: true  
  11.     crop_size: 224  
  12.     mean_file: "examples/imagenet/mydata_mean.binaryproto" #均值文件  
  13.     #mean_value: 104 #这些注释掉  
  14.     #mean_value: 117  
  15.     #mean_value: 123  
  16.   }  
  17.   data_param {  
  18.     source: "examples/imagenet/mydata_train_lmdb" #训练集的lmdb  
  19.     batch_size: 32 #根据GPU修改  
  20.     backend: LMDB  
  21.   }  
  22. }  

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. layer {  
  2.   name: "data"  
  3.   type: "Data"  
  4.   top: "data"  
  5.   top: "label"  
  6.   include {  
  7.     phase: TEST  
  8.   }  
  9.   transform_param {  
  10.     mirror: false  
  11.     crop_size: 224  
  12.     mean_file: "examples/imagenet/mydata_mean.binaryproto" #均值文件  
  13.     #mean_value: 104  
  14.     #mean_value: 117  
  15.     #mean_value: 123  
  16.   }  
  17.   data_param {  
  18.     source: "examples/imagenet/mydata_val_lmdb" #验证集lmdb  
  19.     batch_size: 50 #和solver中的test_iter相乘约等于验证集大小  
  20.     backend: LMDB  
  21.   }  
  22. }  


2.修改输出:

由于Googlenet有三个输出,所以改三个地方,其他网络一般只有一个输出,则改一个地方即可。

如果是微调,那么输出层的层名也要修改。(参数根据层名来初始化,由于输出改了,该层参数就不对应了,因此要改名)

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. layer {  
  2.   name: "loss1/classifier"  
  3.   type: "InnerProduct"  
  4.   bottom: "loss1/fc"  
  5.   top: "loss1/classifier"  
  6.   param {  
  7.     lr_mult: 1  
  8.     decay_mult: 1  
  9.   }  
  10.   param {  
  11.     lr_mult: 2  
  12.     decay_mult: 0  
  13.   }  
  14.   inner_product_param {  
  15.     num_output: 1000 #改成你的数据集类别数  
  16.     weight_filler {  
  17.       type: "xavier"  
  18.     }  
  19.     bias_filler {  
  20.       type: "constant"  
  21.       value: 0  
  22.     }  
  23.   }  
  24. }  

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. layer {  
  2.   name: "loss2/classifier"  
  3.   type: "InnerProduct"  
  4.   bottom: "loss2/fc"  
  5.   top: "loss2/classifier"  
  6.   param {  
  7.     lr_mult: 1  
  8.     decay_mult: 1  
  9.   }  
  10.   param {  
  11.     lr_mult: 2  
  12.     decay_mult: 0  
  13.   }  
  14.   inner_product_param {  
  15.     num_output: 1000 #改成你的数据集类别数  
  16.     weight_filler {  
  17.       type: "xavier"  
  18.     }  
  19.     bias_filler {  
  20.       type: "constant"  
  21.       value: 0  
  22.     }  
  23.   }  
  24. }  

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. layer {  
  2.   name: "loss3/classifier"  
  3.   type: "InnerProduct"  
  4.   bottom: "pool5/7x7_s1"  
  5.   top: "loss3/classifier"  
  6.   param {  
  7.     lr_mult: 1  
  8.     decay_mult: 1  
  9.   }  
  10.   param {  
  11.     lr_mult: 2  
  12.     decay_mult: 0  
  13.   }  
  14.   inner_product_param {  
  15.     num_output: 1000 #改成你的数据集类别数  
  16.     weight_filler {  
  17.       type: "xavier"  
  18.     }  
  19.     bias_filler {  
  20.       type: "constant"  
  21.       value: 0  
  22.     }  
  23.   }  
  24. }  


3.打开deploy.prototxt,修改:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. layer {  
  2.   name: "loss3/classifier"  
  3.   type: "InnerProduct"  
  4.   bottom: "pool5/7x7_s1"  
  5.   top: "loss3/classifier"  
  6.   param {  
  7.     lr_mult: 1  
  8.     decay_mult: 1  
  9.   }  
  10.   param {  
  11.     lr_mult: 2  
  12.     decay_mult: 0  
  13.   }  
  14.   inner_product_param {  
  15.     num_output: 1000 #改成你的数据集类别数  
  16.     weight_filler {  
  17.       type: "xavier"  
  18.     }  
  19.     bias_filler {  
  20.       type: "constant"  
  21.       value: 0  
  22.     }  
  23.   }  
  24. }  
如果是微调,该层层名和train_val.prototxt修改一致。


接着,打开solver,修改:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. net: "examples/imagenet/bvlc_googlenet/train_val.prototxt" #路径不要错  
  2. test_iter: 1000 #前面已说明该值  
  3. test_interval: 4000 #迭代多少次测试一次  
  4. test_initialization: false  
  5. display: 40  
  6. average_loss: 40  
  7. base_lr: 0.01  
  8. lr_policy: "step"  
  9. stepsize: 320000 #迭代多少次改变一次学习率  
  10. gamma: 0.96  
  11. max_iter: 10000000 #迭代次数  
  12. momentum: 0.9  
  13. weight_decay: 0.0002  
  14. snapshot: 40000  
  15. snapshot_prefix: "examples/imagenet/bvlc_googlenet" #生成的caffemodel保存在imagenet下,形如bvlc_googlenet_iter_***.caffemodel  
  16. solver_mode: GPU  

这时,我们回到caffe-master\examples\imagenet下,打开train_caffenet.sh,修改:

(如果是微调,在脚本里加入-weights **/**/**.caffemodel即可,即用来微调的caffemodel路径)

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #!/usr/bin/env sh  
  2.   
  3. ./build/tools/caffe train \  
  4.     -solver examples/imagenet/bvlc_googlenet/solver.prototxt -gpu 0  
(如果有多个GPU,可自行选择)
然后,在caffe-master下执行改脚本即可开始训练:$caffe-master ./examples/imagenet/train_caffenet.sh


训练得到的caffemodel就可以用来做图像分类了,此时,需要(1)得到的labels.txt,(2)得到的mydata_mean.binaryproto,(3)得到的caffemodel以及已经修改过的deploy.prototxt,共四个文件,具体过程看:http://blog.csdn.net/sinat_30071459/article/details/50974695

0 0
原创粉丝点击