Faster-RCNN+ZF用自己的數據集訓練模型(Python版本)

原文:http://blog.csdn.net/sinat_30071459/article/details/51332084

說明:本博文假設你已經做好了自己的數據集,該數據集格式和VOC2007相同。下面是訓練前的一些修改。

(做數據集的過程可以看http://blog.csdn.net/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.csdn.net/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.csdn.net/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  

!!!爲防止與之前的模型搞混,訓練前把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  








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章