FasterRcnn训练数据集参数配置

说明:本博文假设你已经做好了自己的数据集,该数据集格式和VOC2007相同。做好数据集后,我们开始训练,下面是训练前的一些修改。
本文来自:http://www.lai18.com/content/2526443.html

 

1 、VOCdevkit2007VOCcodeVOCinit.m的修改

(1)路径的修改

[plain] view plaincopy
 
  1. VOCopts.annopath=[VOCopts.datadir VOCopts.dataset '/Annotations/%s.xml'];  
  2. VOCopts.imgpath=[VOCopts.datadir VOCopts.dataset '/JPEGImages/%s.jpg'];  
  3. VOCopts.imgsetpath=[VOCopts.datadir VOCopts.dataset '/ImageSets/Main/%s.txt'];  
  4. VOCopts.clsimgsetpath=[VOCopts.datadir VOCopts.dataset '/ImageSets/Main/%s_%s.txt'];  
  5. VOCopts.clsrespath=[VOCopts.resdir 'Main/%s_cls_' VOCopts.testset '_%s.txt'];  
  6. VOCopts.detrespath=[VOCopts.resdir 'Main/%s_det_' VOCopts.testset '_%s.txt'];  


上面这些路径要正确,第一个是xml标签路径;第二个是图片的路径;第三个是放train.txt、val.txt、test.txt和trainval.txt的路径;第四、五、六个不需要;一般来说这些路径不用修改,你做的数据集格式和VOC2007相同就行。

(2)训练集文件夹修改

[plain] view plaincopy
 
  1. VOCopts.dataset = '你的文件夹名';  



然后将VOC2007路径注释掉,上面“你的文件夹名”是你放Annotations、ImageSets、JPEGImages文件夹的文件夹名。

(3)标签的修改

[plain] view plaincopy
 
  1. VOCopts.classes={...  
  2.    '你的标签1'  
  3.    '你的标签2'  
  4.    '你的标签3'  
  5.    '你的标签4'};  

将其改为你的标签。

2 、VOCdevkit2007 esults

results下需要新建一个文件夹,名字和xml中的<folder>***</folder>对应。***文件夹下新建一个Main文件夹。

3 、VOCdevkit2007local

local下需要新建一个文件夹,名字和xml中的<folder>***</folder>对应。

4 、functionfast_rcnnfast_rcnn_train.m

[plain] view plaincopy
 
  1. ip.addParamValue('val_iters',       500,            @isscalar);   
  2. ip.addParamValue('val_interval',    2000,           @isscalar);  


可能在randperm(N,k)出现错误,可以将500改小点,比如200.

5、function pnproposal_train.m

这里的问题和fast_rcnn_train.m一样。

6.imdbimdb_eval_voc.m

[plain] view plaincopy
 
  1. %do_eval = (str2num(year) <= 2007) | ~strcmp(test_set,'test');  
  2. do_eval = 1;  

注释掉

[plain] view plaincopy
 
  1. do_eval = (str2num(year) <= 2007) | ~strcmp(test_set,'test');  

并令其为1,否则测试会出现精度全为0的情况

7. imdbroidb_from_voc.m

[plain] view plaincopy
 
  1. ip.addParamValue('exclude_difficult_samples',       true,   @islogical);</span>  

不包括难识别的样本,所以设置为true。(如果有就设置为false)

8.网络模型的修改

(1) models fast_rcnn_prototxtsF train_val.prototxt

[plain] view plaincopy
 
  1. input: "bbox_targets"  
  2. input_dim: 1  # to be changed on-the-fly to match num ROIs  
  3. input_dim: 84 # 根据类别数改,该值为(类别数+1)*4  #################  
  4. input_dim: 1  
  5. input_dim: 1  



[plain] view plaincopy
 
  1. input: "bbox_loss_weights"  
  2. input_dim: 1  # to be changed on-the-fly to match num ROIs  
  3. input_dim: 84 # 根据类别数改,该值为(类别数+1)*4   ############</span>  
  4. input_dim: 1  
  5. input_dim: 1  



[plain] view plaincopy
 
  1. layer {  
  2.     bottom: "fc7"  
  3.     top: "cls_score"  
  4.     name: "cls_score"  
  5.     param {  
  6.         lr_mult: 1.0  
  7.     }  
  8.     param {  
  9.         lr_mult: 2.0  
  10.     }  
  11.     type: "InnerProduct"  
  12.     inner_product_param {  
  13.         num_output: 21 #根据类别数改该值为类别数+1   #########  



[plain] view plaincopy
 
  1. layer {  
  2.     bottom: "fc7"  
  3.     top: "bbox_pred"  
  4.     name: "bbox_pred"  
  5.     type: "InnerProduct"  
  6.     param {  
  7.         lr_mult: 1.0  
  8.     }  
  9.     param {  
  10.         lr_mult: 2.0  
  11.     }  
  12.     inner_product_param {  
  13.         num_output: 84  #根据类别数改,该值为(类别数+1)*4  ##########  

(2) models fast_rcnn_prototxtsF test.prototxt

[plain] view plaincopy
 
  1. layer {  
  2.     bottom: "fc7"  
  3.     top: "cls_score"  
  4.     name: "cls_score"  
  5.     param {  
  6.         lr_mult: 1.0  
  7.     }  
  8.     param {  
  9.         lr_mult: 2.0  
  10.     }  
  11.     type: "InnerProduct"  
  12.     inner_product_param {  
  13.         num_output: 21  #类别数+1  ##########  
[plain] view plaincopy
 
  1. layer {  
  2.     bottom: "fc7"  
  3.     top: "bbox_pred"  
  4.     name: "bbox_pred"  
  5.     type: "InnerProduct"  
  6.     param {  
  7.         lr_mult: 1.0  
  8.     }  
  9.     param {  
  10.         lr_mult: 2.0  
  11.     }  
  12.     inner_product_param {  
  13.         num_output: 84  #4*(类别数+1)  ##########  



(3) models fast_rcnn_prototxtsF_fc6 train_val.prototxt

[plain] view plaincopy
 
  1. input: "bbox_targets"  
  2. input_dim: 1  # to be changed on-the-fly to match num ROIs  
  3. input_dim: 84 # 4*(类别数+1)  ###########  
  4. input_dim: 1  
  5. input_dim: 1  



[plain] view plaincopy
 
  1. input: "bbox_loss_weights"  
  2. input_dim: 1  # to be changed on-the-fly to match num ROIs  
  3. input_dim: 84 # 4*(类别数+1)  ###########  
  4. input_dim: 1  
  5. input_dim: 1  



[plain] view plaincopy
 
  1. layer {  
  2.     bottom: "fc7"  
  3.     top: "cls_score"  
  4.     name: "cls_score"  
  5.     param {  
  6.         lr_mult: 1.0  
  7.     }  
  8.     param {  
  9.         lr_mult: 2.0  
  10.     }  
  11.     type: "InnerProduct"  
  12.     inner_product_param {  
  13.         num_output: 21 #类别数+1   ############  



[plain] view plaincopy
 
  1. layer {  
  2.     bottom: "fc7"  
  3.     top:"bbox_pred"  
  4.     name:"bbox_pred"  
  5.     type:"InnerProduct"  
  6.     param {  
  7.        lr_mult:1.0  
  8.     }  
  9.     param {  
  10.        lr_mult:2.0  
  11.     }  
  12.     inner_product_param{  
  13.        num_output: 84   #4*(类别数+1)   ###########  



(4) models fast_rcnn_prototxtsF_fc6 test.prototxt

[plain] view plaincopy
 
  1. layer {  
  2.     bottom: "fc7"  
  3.     top: "cls_score"  
  4.     name: "cls_score"  
  5.     param {  
  6.         lr_mult: 1.0  
  7.     }  
  8.     param {  
  9.         lr_mult: 2.0  
  10.     }  
  11.     type: "InnerProduct"  
  12.     inner_product_param {  
  13.         num_output: 21  类别数+1 #######  



[plain] view plaincopy
 
  1. layer {  
  2.     bottom: "fc7"  
  3.     top: "bbox_pred"  
  4.     name: "bbox_pred"  
  5.     type: "InnerProduct"  
  6.     param {  
  7.         lr_mult: 1.0  
  8.     }  
  9.     param {  
  10.         lr_mult: 2.0  
  11.     }  
  12.     inner_product_param {  
  13.         num_output: 84  #4*(类别数+1) ##########  



!!!为防止与之前的模型搞混,训练前把output文件夹删除(或改个其他名),还要把imdbcache中的文件删除(如果有的话)

9.开始训练

运行:

[plain] view plaincopy
 
  1. experiments/script_faster_rcnn_VOC2007_ZF.m  



10.训练完后

训练完后,不要急着马上测试,先打开output/faster_rcnn_final/faster_rcnn_VOC2007_ZF文件夹,打开detection_test.prototxt,作如下修改:

将relu5(包括relu5)前的层删除,并将roi_pool5的bottom改为data和rois。并且前面的input_dim:分别改为1,256,50,50(如果是VGG就是1,512,50,50,其他修改基本一样),具体如下

[plain] view plaincopy
 
  1. input: "data"  
  2. input_dim: 1  
  3. input_dim: 256  
  4. input_dim: 50  
  5. input_dim: 50  



[plain] view plaincopy
 
  1. # ------------------------ layer 1 -----------------------------  
  2. layer {  
  3.     bottom: "data"  
  4.     bottom: "rois"  
  5.     top: "pool5"  
  6.     name: "roi_pool5"  
  7.     type: "ROIPooling"  
  8.     roi_pooling_param {  
  9.         pooled_w: 6  
  10.         pooled_h: 6  
  11.         spatial_scale: 0.0625  # (1/16)  
  12.     }  
  13. }  



11.测试

训练完成后,打开experimentsscript_faster_rcnn_demo.m,将模型路径改成训练得到的模型路径:

[plain] view plaincopy
 
  1. model_dir                   = fullfile(pwd, 'output', 'faster_rcnn_final', 'faster_rcnn_VOC2007_ZF')  
[plain] view plaincopy
 
    1. 将测试图片改成你的图片,im_names = {'001.jpg', '002.jpg', '003.jpg'};  
原文地址:https://www.cnblogs.com/songxingzhu/p/7077120.html