Ubuntu16.04配置py-faster-rcnn

说明

本来用的Ubuntu14.04,毕竟都说14比16稳定嘛。但是配置好了后,运行demo.py都没问题了,但是训练的时候出现 

TypeError numpy.float64 object cannot be interpreted as an index

都说是numpy版本问题,得用1.11.0,因此降到1.11.0(sudo pip install -U numpy==1.11.0)

但是装了numpy1.11.0后出现

 numpy.core.multiarray failed to import 

网上都说版本不兼容,得升级到新版本。升级又出现上面那个问题。

。。。。。。。我就无语了,上面两个是循环的问题啊,无法解决。

卡了好几天,最后通过apt-cache show python-numpy,查看到numpy版本居然是1.8!!!!无论怎么更新,就算通过

import numpy
numpy.__version__

显示的都不是1.8。但其实内核是1.8,因为Ubuntu14只能支持到1.8,所以。。。。。。所以放弃了Ubuntu14

一、必要配置安装

CUDA、opencv我的其他博客已经写过了,也可参考网上其他教程,这里就跳过。

二、安装一些包

1 pip install cython  
2 pip install easydict  
3 apt-get install python-opencv  

还有些包根据错误提示安装就好了。

三、下载py-faster-rcnn

使用git clone,保证caffe分支一起clone下来

git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git  

四、进入py-faster-rcnn/lib

make

五、进入py-faster-rcnn\caffe-fast-rcnn

cp Makefile.config.example Makefile.config

然后配置Makefile.config:是否用CUDNN、是否是opencv3

重要的一点是必须

WITH_PYTHON_LAYER :=1

配置好后,执行:

make -j8 && make pycaffe  

六、下载VOC2007数据集

 (当然也可以直接用自己的数据集)

百度网盘下载VOC2007:http://pan.baidu.com/s/1mhMKKw4

把VOC2007数据集放在py-faster-rcnn\data目录下,用自己的数据集替换VOC2007数据集。(替换py-faster-rcnn\data\VOCdevkit2007\VOC2007目录下的Annotations,ImageSets和JPEGImages)

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

百度网盘下载地址:http://pan.baidu.com/s/1hsxx8OW

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

七、训练参数修改(以ZF网络为例)

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

 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 }  
 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 }  
 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

 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

 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 }  
 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 }  
 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

 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

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 }  
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 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                       )  

上面要改的地方是

修改训练集文件夹:

self._data_path = os.path.join(self._devkit_path, 'VOC'+self._year)  

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

修改标签:

self._classes = ('__background__', # always index 0  
                         '你的标签1','你的标签2','你的标签3','你的标签4'  
                      )  

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

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

 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  

8、学习率和迭代次数修改

至于学习率等之类的设置,可在py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt中的solve文件设置。

迭代次数可在py-faster-rcnn\tools的train_faster_rcnn_alt_opt.py中修改:

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小于上面修改的数值。

训练前其他说明:

为防止与之前的模型搞混,训练前把output文件夹删除,还要把py-faster-rcnn/data/cache中的文件和

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

八、开始训练(以ZF网络为例)

进入py-faster-rcnn,执行:

./experiments/scripts/faster_rcnn_alt_opt.sh 0 ZF pascal_voc  

九、测试(以ZF网络为例)

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

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

CLASSES = ('__background__',  
           '你的标签1', '你的标签2', '你的标签3', '你的标签4')  
NETS = {'vgg16': ('VGG16',  
                  'VGG16_faster_rcnn_final.caffemodel'),  
        'zf': ('ZF',  
                  'ZF_faster_rcnn_final.caffemodel')} 

上面ZF的caffemodel改成你的caffemodel。没做修改就不改。

im_names = ['test1.jpg','test2.jpg']  

改成你的测试图片。(测试图片放在py-faster-rcnn\data\demo中)。也可自己看代码再修改。

开始测试:

在py-faster-rcnn下,执行:

./tools/demo.py --net zf  

如果不出错的话就会显示检测结果。

posted @ 2017-09-20 16:02  _云深不知处  阅读(743)  评论(0编辑  收藏  举报