FasterRcnn训练数据集参数配置
FasterRcnn训练数据集参数配置
FasterRcnn训练数据集参数配置
说明:本博文假设你已经做好了自己的数据集,该数据集格式和VOC2007相同。做好数据集后,我们开始训练,下面是训练前的一些修改。
本文来自:http://www.lai18.com/content/2526443.html
1 、VOCdevkit2007\VOCcode\VOCinit.m的修改
(1)路径的修改
[plain] view plaincopy
- VOCopts.annopath=[VOCopts.datadir VOCopts.dataset ‘/Annotations/%s.xml’];
- VOCopts.imgpath=[VOCopts.datadir VOCopts.dataset ‘/JPEGImages/%s.jpg’];
- VOCopts.imgsetpath=[VOCopts.datadir VOCopts.dataset ‘/ImageSets/Main/%s.txt’];
- VOCopts.clsimgsetpath=[VOCopts.datadir VOCopts.dataset ‘/ImageSets/Main/%s_%s.txt’];
- VOCopts.clsrespath=[VOCopts.resdir ‘Main/%s_cls_’ VOCopts.testset ‘_%s.txt’];
- 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
- VOCopts.dataset = ‘你的文件夹名’;
然后将VOC2007路径注释掉,上面”你的文件夹名”是你放Annotations、ImageSets、JPEGImages文件夹的文件夹名。
(3)标签的修改
[plain] view plaincopy
- VOCopts.classes={…
- ‘你的标签1’
- ‘你的标签2’
- ‘你的标签3’
- ‘你的标签4’};
将其改为你的标签。
2 、VOCdevkit2007\results
results下需要新建一个文件夹,名字和xml中的
3 、VOCdevkit2007\local
local下需要新建一个文件夹,名字和xml中的
4 、function\fast_rcnn\fast_rcnn_train.m
[plain] view plaincopy
- ip.addParamValue(‘val_iters’, 500, @isscalar);
- ip.addParamValue(‘val_interval’, 2000, @isscalar);
可能在randperm(N,k)出现错误,可以将500改小点,比如200.
5、function\rpn\proposal_train.m
这里的问题和fast_rcnn_train.m一样。
6.imdb\imdb_eval_voc.m
[plain] view plaincopy
- %do_eval = (str2num(year) <= 2007) | ~strcmp(test_set,’test’);
- do_eval = 1;
注释掉
[plain] view plaincopy
- do_eval = (str2num(year) <= 2007) | ~strcmp(test_set,’test’);
并令其为1,否则测试会出现精度全为0的情况
7. imdb*roidb_from_voc.m*
[plain] view plaincopy
- ip.addParamValue(‘exclude_difficult_samples’, true, @islogical);
不包括难识别的样本,所以设置为true。(如果有就设置为false)
8.网络模型的修改
(1) models\ fast_rcnn_prototxts\ZF\ train_val.prototxt
[plain] view plaincopy
- input: “bbox_targets”
- input_dim: 1 # to be changed on-the-fly to match num ROIs
- input_dim: 84 # 根据类别数改,该值为(类别数+1)*4 #################
- input_dim: 1
- input_dim: 1
[plain] view plaincopy
- input: “bbox_loss_weights”
- input_dim: 1 # to be changed on-the-fly to match num ROIs
- input_dim: 84 # 根据类别数改,该值为(类别数+1)*4 ############
- input_dim: 1
- input_dim: 1
[plain] view plaincopy
- layer {
bottom: "fc7"
top: "cls_score"
name: "cls_score"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
type: "InnerProduct"
inner_product_param {
num_output: 21 #根据类别数改该值为类别数+1 #########
[plain] view plaincopy
- layer {
bottom: "fc7"
top: "bbox_pred"
name: "bbox_pred"
type: "InnerProduct"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
inner_product_param {
num_output: 84 #根据类别数改,该值为(类别数+1)*4 ##########
(2) models\ fast_rcnn_prototxts\ZF\ test.prototxt
[plain] view plaincopy
- layer {
bottom: "fc7"
top: "cls_score"
name: "cls_score"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
type: "InnerProduct"
inner_product_param {
num_output: 21 #类别数+1 ##########
[plain] view plaincopy
- layer {
bottom: "fc7"
top: "bbox_pred"
name: "bbox_pred"
type: "InnerProduct"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
inner_product_param {
num_output: 84 #4*(类别数+1) ##########
(3) models\ fast_rcnn_prototxts\ZF_fc6\ train_val.prototxt
[plain] view plaincopy
- input: “bbox_targets”
- input_dim: 1 # to be changed on-the-fly to match num ROIs
- input_dim: 84 # 4*(类别数+1) ###########
- input_dim: 1
- input_dim: 1
[plain] view plaincopy
- input: “bbox_loss_weights”
- input_dim: 1 # to be changed on-the-fly to match num ROIs
- input_dim: 84 # 4*(类别数+1) ###########
- input_dim: 1
- input_dim: 1
[plain] view plaincopy
- layer {
bottom: "fc7"
top: "cls_score"
name: "cls_score"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
type: "InnerProduct"
inner_product_param {
num_output: 21 #类别数+1 ############
[plain] view plaincopy
- layer {
bottom: "fc7"
top:"bbox_pred"
name:"bbox_pred"
type:"InnerProduct"
param {
lr_mult:1.0
}
param {
lr_mult:2.0
}
inner_product_param{
num_output: 84 #4*(类别数+1) ###########
(4) models\ fast_rcnn_prototxts\ZF_fc6\ test.prototxt
[plain] view plaincopy
- layer {
bottom: "fc7"
top: "cls_score"
name: "cls_score"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
type: "InnerProduct"
inner_product_param {
num_output: 21 类别数+1 #######
[plain] view plaincopy
- layer {
bottom: "fc7"
top: "bbox_pred"
name: "bbox_pred"
type: "InnerProduct"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
inner_product_param {
num_output: 84 #4*(类别数+1) ##########
!!!为防止与之前的模型搞混,训练前把output文件夹删除(或改个其他名),还要把imdb\cache中的文件删除(如果有的话)
9.开始训练
运行:
[plain] view plaincopy
- 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
- input: “data”
- input_dim: 1
- input_dim: 256
- input_dim: 50
- input_dim: 50
[plain] view plaincopy
-———————– layer 1 -—————————-
- layer {
bottom: "data"
bottom: "rois"
top: "pool5"
name: "roi_pool5"
type: "ROIPooling"
roi_pooling_param {
pooled_w: 6
pooled_h: 6
spatial_scale: 0.0625 # (1/16)
}
- }
11.测试
训练完成后,打开\experiments\script_faster_rcnn_demo.m,将模型路径改成训练得到的模型路径:
[plain] view plaincopy
- model_dir = fullfile(pwd, ‘output’, ‘faster_rcnn_final’, ‘faster_rcnn_VOC2007_ZF’)
[plain] view plaincopy
1. 将测试图片改成你的图片,im_names = {'001.jpg', '002.jpg', '003.jpg'};