Faster-RCNN+ZF用自己的数据集训练模型(Python版本and MATLAB版本)

本文详细介绍如何使用Python版Faster-RCNN框架训练自定义数据集,涵盖配置环境、修改配置文件及测试流程等内容。

说明:本博文假设你已经做好了自己的数据集,该数据集格式和VOC2007相同。下面是训练前的一些修改。

(做数据集的过程可以看http://blog.youkuaiyun.com/sinat_30071459/article/details/50723212


Faster-RCNN源码下载地址:

Matlab版本:https://github.com/ShaoqingRen/faster_rcnn

Python版本:https://github.com/rbgirshick/py-faster-rcnn

本文用到的是Python版本,在Linux下运行。

Matlab版本的训练过程:http://blog.youkuaiyun.com/sinat_30071459/article/details/50546891

准备工作:

1.配置caffe

     这个不多说,网上教程很多。

2.其他的注意事项

      这里说的挺详细了,认真看看吧。地址:https://github.com/rbgirshick/py-faster-rcnn(主要内容如下)

下面大概翻译一下上面网址的内容吧。

(1)安装cythonpython-OpenCV,easydict

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. pip install cython  
  2. pip install easydict  
  3. apt-get install python-opencv  

(2)下载py-faster-rcnn

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # Make sure to clone with --recursive  
  2. git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git  

如图:


(3)进入py-faster-rcnn/lib

   执行make

如图:

(4)进入py-faster-rcnn\caffe-fast-rcnn

执行 cp Makefile.config.example Makefile.config

然后,配置Makefile.config文件,可参考我的配置:Makefile.config文件

配置好Makefile.config文件后,执行:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. make -j8 && make pycaffe  

如图:


(5)下载VOC2007数据集

提供一个百度云地址:http://pan.baidu.com/s/1mhMKKw4

解压,然后,将该数据集放在py-faster-rcnn\data下,用你的数据集替换VOC2007数据集。(替换Annotations,ImageSets和JPEGImages)

(用你的Annotations,ImagesSets和JPEGImages替换py-faster-rcnn\data\VOCdevkit2007\VOC2007中对应文件夹)

(6)下载ImageNet数据集下预训练得到的模型参数(用来初始化)

提供一个百度云地址:http://pan.baidu.com/s/1hsxx8OW

解压,然后将该文件放在py-faster-rcnn\data下

下面是训练前的一些修改。

1.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage1_fast_rcnn_train.pt修改

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. layer {  
  2.   name: 'data'  
  3.   type: 'Python'  
  4.   top: 'data'  
  5.   top: 'rois'  
  6.   top: 'labels'  
  7.   top: 'bbox_targets'  
  8.   top: 'bbox_inside_weights'  
  9.   top: 'bbox_outside_weights'  
  10.   python_param {  
  11.     module: 'roi_data_layer.layer'  
  12.     layer: 'RoIDataLayer'  
  13.     param_str: "'num_classes': 16" #按训练集类别改,该值为类别数+1  
  14.   }  
  15. }  

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. layer {  
  2.   name: "cls_score"  
  3.   type: "InnerProduct"  
  4.   bottom: "fc7"  
  5.   top: "cls_score"  
  6.   param { lr_mult: 1.0 }  
  7.   param { lr_mult: 2.0 }  
  8.   inner_product_param {  
  9.     num_output: 16 #按训练集类别改,该值为类别数+1  
  10.     weight_filler {  
  11.       type: "gaussian"  
  12.       std: 0.01  
  13.     }  
  14.     bias_filler {  
  15.       type: "constant"  
  16.       value: 0  
  17.     }  
  18.   }  
  19. }  

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. layer {  
  2.   name: "bbox_pred"  
  3.   type: "InnerProduct"  
  4.   bottom: "fc7"  
  5.   top: "bbox_pred"  
  6.   param { lr_mult: 1.0 }  
  7.   param { lr_mult: 2.0 }  
  8.   inner_product_param {  
  9.     num_output: 64 #按训练集类别改,该值为(类别数+1)*4  
  10.     weight_filler {  
  11.       type: "gaussian"  
  12.       std: 0.001  
  13.     }  
  14.     bias_filler {  
  15.       type: "constant"  
  16.       value: 0  
  17.     }  
  18.   }  
  19. }  

2.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage1_rpn_train.pt修改

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. layer {  
  2.   name: 'input-data'  
  3.   type: 'Python'  
  4.   top: 'data'  
  5.   top: 'im_info'  
  6.   top: 'gt_boxes'  
  7.   python_param {  
  8.     module: 'roi_data_layer.layer'  
  9.     layer: 'RoIDataLayer'  
  10.     param_str: "'num_classes': 16" #按训练集类别改,该值为类别数+1  
  11.   }  
  12. }  

3.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage2_fast_rcnn_train.pt修改

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. layer {  
  2.   name: 'data'  
  3.   type: 'Python'  
  4.   top: 'data'  
  5.   top: 'rois'  
  6.   top: 'labels'  
  7.   top: 'bbox_targets'  
  8.   top: 'bbox_inside_weights'  
  9.   top: 'bbox_outside_weights'  
  10.   python_param {  
  11.     module: 'roi_data_layer.layer'  
  12.     layer: 'RoIDataLayer'  
  13.     param_str: "'num_classes': 16" #按训练集类别改,该值为类别数+1  
  14.   }  
  15. }  

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. layer {  
  2.   name: "cls_score"  
  3.   type: "InnerProduct"  
  4.   bottom: "fc7"  
  5.   top: "cls_score"  
  6.   param { lr_mult: 1.0 }  
  7.   param { lr_mult: 2.0 }  
  8.   inner_product_param {  
  9.     num_output: 16 #按训练集类别改,该值为类别数+1  
  10.     weight_filler {  
  11.       type: "gaussian"  
  12.       std: 0.01  
  13.     }  
  14.     bias_filler {  
  15.       type: "constant"  
  16.       value: 0  
  17.     }  
  18.   }  
  19. }  

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. layer {  
  2.   name: "bbox_pred"  
  3.   type: "InnerProduct"  
  4.   bottom: "fc7"  
  5.   top: "bbox_pred"  
  6.   param { lr_mult: 1.0 }  
  7.   param { lr_mult: 2.0 }  
  8.   inner_product_param {  
  9.     num_output: 64 #按训练集类别改,该值为(类别数+1)*4  
  10.     weight_filler {  
  11.       type: "gaussian"  
  12.       std: 0.001  
  13.     }  
  14.     bias_filler {  
  15.       type: "constant"  
  16.       value: 0  
  17.     }  
  18.   }  
  19. }  

4.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage2_rpn_train.pt修改

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. layer {  
  2.   name: 'input-data'  
  3.   type: 'Python'  
  4.   top: 'data'  
  5.   top: 'im_info'  
  6.   top: 'gt_boxes'  
  7.   python_param {  
  8.     module: 'roi_data_layer.layer'  
  9.     layer: 'RoIDataLayer'  
  10.     param_str: "'num_classes': 16" #按训练集类别改,该值为类别数+1  
  11.   }  
  12. }  

5.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/faster_rcnn_test.pt修改

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. layer {  
  2.   name: "cls_score"  
  3.   type: "InnerProduct"  
  4.   bottom: "fc7"  
  5.   top: "cls_score"  
  6.   inner_product_param {  
  7.     num_output: 16 #按训练集类别改,该值为类别数+1  
  8.   }  
  9. }  

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. layer {  
  2.   name: "bbox_pred"  
  3.   type: "InnerProduct"  
  4.   bottom: "fc7"  
  5.   top: "bbox_pred"  
  6.   inner_product_param {  
  7.     num_output: 64 #按训练集类别改,该值为(类别数+1)*4  
  8.   }  
  9. }  

6.py-faster-rcnn/lib/datasets/pascal_voc.py修改

(1)

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class pascal_voc(imdb):  
  2.     def __init__(self, image_set, year, devkit_path=None):  
  3.         imdb.__init__(self, 'voc_' + year + '_' + image_set)  
  4.         self._year = year  
  5.         self._image_set = image_set  
  6.         self._devkit_path = self._get_default_path() if devkit_path is None \  
  7.                             else devkit_path  
  8.         self._data_path = os.path.join(self._devkit_path, 'VOC' + self._year)  
  9.         self._classes = ('__background__', # always index 0  
  10.                          '你的标签1','你的标签2',你的标签3','你的标签4'  
  11.                       )  

上面要改的地方是

修改训练集文件夹:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. self._data_path = os.path.join(self._devkit_path, 'VOC'+self._year)  

用你的数据集直接替换原来VOC2007内的Annotations,ImageSets和JPEGImages即可,以免出现各种错误。


修改标签:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. self._classes = ('__background__', # always index 0  
  2.                          '你的标签1','你的标签2','你的标签3','你的标签4'  
  3.                       )  

修改成你的数据集的标签就行。


(2)

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. cls = self._class_to_ind[obj.find('name').text.lower().strip()]  
这里把标签转成小写,如果你的标签含有大写字母,可能会出现KeyError的错误,所以建议标签用小写字母。

(去掉lower应该也行)

建议训练的标签还是用小写的字母,如果最终需要用大写字母或中文显示标签,可参考:

http://blog.youkuaiyun.com/sinat_30071459/article/details/51694037

7.py-faster-rcnn/lib/datasets/imdb.py修改

该文件的append_flipped_images(self)函数修改为:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. def append_flipped_images(self):  
  2.         num_images = self.num_images  
  3.         widths = [PIL.Image.open(self.image_path_at(i)).size[0]  
  4.                   for i in xrange(num_images)]  
  5.         for i in xrange(num_images):  
  6.             boxes = self.roidb[i]['boxes'].copy()  
  7.             oldx1 = boxes[:, 0].copy()  
  8.             oldx2 = boxes[:, 2].copy()  
  9.             boxes[:, 0] = widths[i] - oldx2 - 1  
  10.             print boxes[:, 0]  
  11.             boxes[:, 2] = widths[i] - oldx1 - 1  
  12.             print boxes[:, 0]  
  13.             assert (boxes[:, 2] >= boxes[:, 0]).all()  
  14.             entry = {'boxes' : boxes,  
  15.                      'gt_overlaps' : self.roidb[i]['gt_overlaps'],  
  16.                      'gt_classes' : self.roidb[i]['gt_classes'],  
  17.                      'flipped' : True}  
  18.             self.roidb.append(entry)  
  19.         self._image_index = self._image_index * 2  


这里assert (boxes[:, 2] >= boxes[:, 0]).all()可能出现AssertionError,具体解决办法参考:

!!!为防止与之前的模型搞混,训练前把output文件夹删除(或改个其他名),还要把py-faster-rcnn/data/cache中的文件和

py-faster-rcnn/data/VOCdevkit2007/annotations_cache中的文件删除(如果有的话)。

至于学习率等之类的设置,可在py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt中的solve文件设置,迭代次数可在py-faster-rcnn\tools的train_faster_rcnn_alt_opt.py中修改:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. max_iters = [80000, 40000, 80000, 40000]  
分别为4个阶段(rpn第1阶段,fast rcnn第1阶段,rpn第2阶段,fast rcnn第2阶段)的迭代次数。可改成你希望的迭代次数。

如果改了这些数值,最好把py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt里对应的solver文件(有4个)也修改,stepsize小于上面修改的数值。

8.开始训练

进入py-faster-rcnn,执行:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ./experiments/scripts/faster_rcnn_alt_opt.sh 0 ZF pascal_voc  

这样,就开始训练了。

9.测试

将训练得到的py-faster-rcnn\output\faster_rcnn_alt_opt\***_trainval中ZF的caffemodel拷贝至py-faster-rcnn\data\faster_rcnn_models(如果没有这个文件夹,就新建一个),然后,修改:

py-faster-rcnn\tools\demo.py,主要修改:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. CLASSES = ('__background__',  
  2.            '你的标签1', '你的标签2', '你的标签3', '你的标签4')  

改成你的数据集标签;


[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NETS = {'vgg16': ('VGG16',  
  2.                   'VGG16_faster_rcnn_final.caffemodel'),  
  3.         'zf': ('ZF',  
  4.                   'ZF_faster_rcnn_final.caffemodel')}  

上面ZF的caffemodel改成你的caffemodel。

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. im_names = ['1559.jpg','1564.jpg']  

改成你的测试图片。(测试图片放在py-faster-rcnn\data\demo中)

10.结果

在py-faster-rcnn下,

执行:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ./tools/demo.py --net zf  

或者将默认的模型改为zf:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. parser.add_argument('--net', dest='demo_net'help='Network to use [vgg16]',  
  2.                         choices=NETS.keys(), default='vgg16')  
修改:
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. default='zf'  
执行:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ./tools/demo.py  


Faster-RCNN+ZF用自己的数据集训练模型(MATLAB版本) 

说明:本博文假设你已经做好了自己的数据集,该数据集格式和VOC2007相同。下面是训练前的一些修改。

(做数据集的过程可以看http://blog.youkuaiyun.com/sinat_30071459/article/details/50723212

Faster-RCNN源码下载地址:

Matlab版本:https://github.com/ShaoqingRen/faster_rcnn

Python版本:https://github.com/rbgirshick/py-faster-rcnn

本文用到的是Matlab版本,在Windows下运行。

python版本的训练过程:http://blog.youkuaiyun.com/sinat_30071459/article/details/51332084

资源下载:https://github.com/ShaoqingRen/faster_rcnn,网页最后有所有的资源。

准备工作:

(1)

安装vs2013;

安装Matlab;

安装CUDA;

上面的安装顺序最好不要乱,否则可能出现Matlab找不到vs的情况,在Matlab命令行窗口输入:mbuild -setup,如果出现:


说明Matlab可以找到vs2013。CUDA应在安装vs2013后再安装。

(2)

如果你的cuda是6.5,那么,运行一下:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. fetch_data/fetch_caffe_mex_windows_vs2013_cuda65.m  

(运行代码下载失败的话,用百度云下载:https://pan.baidu.com/s/1i3m0i0H  ,解压到faster_rcnn-master下)

得到mex文件。如果不是cuda6.5(如我的是cuda7.5),则需要自己编译mex文件,编译过程参考这里:Caffe for Faster R-CNN按步骤做就行了。


也可以下载我编译得到的文件(注意cuda版本)。

下载地址:Faster-RCNN(Matlab) external文件夹

建议还是自己编译,因为版本问题可能会出错。在训练前,可以先下载作者训练好的模型,测试一下,如果可以的话,就不用自己编译了。

测试过程:

(1)运行faster_rcnn-master\faster_rcnn_build.m

 (2)运行faster_rcnn-master\startup.m

(3)运行faster_rcnn-master\fetch_data\fetch_faster_rcnn_final_model.m  下载训练好的模型

(下载失败的话,可以用百度云下载:https://pan.baidu.com/s/1hsFKmeK ,解压到faster_rcnn-master下)

(4)修改faster_rcnn-master\experiments\script_faster_rcnn_demo.m的model_dir为你下载的模型,然后运行。

最终得到:




在训练前请确保你的路径faster_rcnn-master\external\caffe\matlab\caffe_faster_rcnn下有以下文件:


(我的OpenCV版本是2.4.9,cuda版本是7.5,因版本不同上述文件和你的编译结果可能会有差异。+caffe文件夹是从caffe-master或caffe-faster-R-CNN里拷贝过来的。)


如果你没有按上面说的测试过,请先运行:

(1)faster_rcnn-master\faster_rcnn_build.m

(2)faster_rcnn-master\startup.m

然后再进行下面的修改。

1 、VOCdevkit2007\VOCcode\VOCinit.m的修改

(1)路径的修改

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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相同就行。(图片格式默认是jpg,如果是png,修改上面第二行的代码即可。)

(2)训练集文件夹修改

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. VOCopts.dataset = '你的文件夹名';   

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

(3)标签的修改

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. VOCopts.classes={...  
  2.    '你的标签1'  
  3.    '你的标签2'  
  4.    '你的标签3'  
  5.    '你的标签4'};  
将其改为你的标签。

2 、VOCdevkit2007\results

results下需要新建一个文件夹,名字是1. (2)中“你的文件夹名”。“你的文件夹名”下新建一个Main文件夹。(因为可能会出现找不到文件夹的错误)

3 、VOCdevkit2007\local

local下需要新建一个文件夹,名字是1. (2)中“你的文件夹名”。(同上)

4 、function\fast_rcnn\fast_rcnn_train.m

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ip.addParamValue('val_iters',       500,            @isscalar);   
  2. ip.addParamValue('val_interval',    2000,           @isscalar);  

可能在randperm(N,k)出现错误,根据数据集修改。(VOC2007中val有2510张图像,train有2501张,作者将val_iters设为500,val_interval设为2000,可以参考作者的设置修改,建议和作者一样val_iters约为val的1/5,val_interval不用修改

5、function\rpn\proposal_train.m

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

6.imdb\imdb_eval_voc.m

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. %do_eval = (str2num(year) <= 2007) | ~strcmp(test_set,'test');  
  2. do_eval = 1;  
注释掉
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. do_eval = (str2num(year) <= 2007) | ~strcmp(test_set,'test');  
并令其为1,否则测试会出现精度全为0的情况

7. imdb\roidb_from_voc.m

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ip.addParamValue('exclude_difficult_samples',       true,   @islogical);  
不包括难识别的样本,所以设置为true。(如果有就设置为false)

8.网络模型的修改

(1) models\ fast_rcnn_prototxts\ZF\ train_val.prototxt

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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   ############  
  4. input_dim: 1  
  5. input_dim: 1  

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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_prototxts\ZF\ test.prototxt

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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_prototxts\ZF_fc6\ train_val.prototxt

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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_prototxts\ZF_fc6\ test.prototxt

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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) ##########  

9.solver的修改

solver文件有3个,默认使用的solver是solver_30k40k.prototxt,如下stage 1 rpn,可以在faster_rcnn-master\experiments\+Model\ZF_for_Faster_RCNN_VOC2007.m中更换。
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. model.stage1_rpn.solver_def_file                = fullfile(pwd, 'models', 'rpn_prototxts', 'ZF', 'solver_30k40k.prototxt');%solver_60k80k.prototxt  
  2. model.stage1_rpn.test_net_def_file              = fullfile(pwd, 'models', 'rpn_prototxts', 'ZF', 'test.prototxt');  
  3. model.stage1_rpn.init_net_file                  = model.pre_trained_net_file;  


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

更为简便的方法是直接用你的数据集的Annotations、ImageSets、JPEGImages文件夹替换VOC2007对应文件夹,那么上面只需进行1.(3)、4、5、7、8的修改。

10.开始训练

(1).下载预训练的ZF模型: fetch_data/fetch_model_ZF.m
(下载失败的话用百度云下载:https://pan.baidu.com/s/1o6zipPS ,解压到faster_rcnn-master下,预训练模型参数用于初始化)

(2).运行:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. experiments/script_faster_rcnn_VOC2007_ZF.m  

经过一会的准备工作,就进入迭代了:


11.训练完后

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

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

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. input: "data"  
  2. input_dim: 1  
  3. input_dim: 256  
  4. input_dim: 50  
  5. input_dim: 50  

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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. }  

12.测试

训练完成后,打开\experiments\script_faster_rcnn_demo.m,将模型路径改成训练得到的模型路径:
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. model_dir                   = fullfile(pwd, 'output', 'faster_rcnn_final', 'faster_rcnn_VOC2007_ZF')  
将测试图片改成你的图片:
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. im_names = {'001.jpg', '002.jpg', '003.jpg'};  
注意:
        如果你的数据集类别比voc2007数据集多,把script_faster_rcnn_demo.m中的showboxes(im, boxes_cell, classes, 'voc')作如下修改:
改为:
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. showboxes(im, boxes_cell, classes);  
或者:
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. showboxes(im, boxes_cell, classes, 'default');  
即去掉‘voc’或将其改为‘default’。

如果测试发现出现的框很多,且这些框没有目标,可以将阈值设高一些(默认是0.6):
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. thres = 0.9;  


结果如下:
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值