說明:本博文假設你已經(jīng)做好了自己的數(shù)據(jù)集,該數(shù)據(jù)集格式和VOC2007相同。下面是訓練前的一些修改。
(做數(shù)據(jù)集的過程可以看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
???? 這個不多說,網(wǎng)上教程很多。
2.其他的注意事項
????? 這里說的挺詳細了,認真看看吧。地址:https://github.com/rbgirshick/py-faster-rcnn(主要內(nèi)容如下)
下面大概翻譯一下上面網(wǎng)址的內(nèi)容吧。
(1)安裝cython,?python-OpenCV,easydict
[plain]?view plaincopy
pip?install?cython?? pip?install?easydict?? apt-get?install?python-opencv??
(2)下載py-faster-rcnn
[plain]?view plaincopy
#?Make?sure?to?clone?with?--recursive?? git?clone?--recursive?https://github.com/rbgirshick/py-faster-rcnn.git??
如圖:
(3)進入py-faster-rcnn/lib
?? 執(zhí)行make
如圖:
(4)進入py-faster-rcnn\caffe-fast-rcnn
執(zhí)行 cp Makefile.config.example Makefile.config
然后,配置Makefile.config文件,可參考我的配置:Makefile.config文件
配置好Makefile.config文件后,執(zhí)行:
[plain]?view plaincopy
make?-j8?&&?make?pycaffe??
如圖:
(5)下載VOC2007數(shù)據(jù)集
提供一個百度云地址:http://pan.baidu.com/s/1mhMKKw4
解壓,然后,將該數(shù)據(jù)集放在py-faster-rcnn\data下,用你的數(shù)據(jù)集替換VOC2007數(shù)據(jù)集。(替換Annotations,ImageSets和JPEGImages)
(用你的Annotations,ImagesSets和JPEGImages替換py-faster-rcnn\data\VOCdevkit2007\VOC2007中對應文件夾)
(6)下載ImageNet數(shù)據(jù)集下預訓練得到的模型參數(shù)(用來初始化)
提供一個百度云地址: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 plaincopy
layer?{?? ??name:?'data'?? ??type:?'Python'?? ??top:?'data'?? ??top:?'rois'?? ??top:?'labels'?? ??top:?'bbox_targets'?? ??top:?'bbox_inside_weights'?? ??top:?'bbox_outside_weights'?? ??python_param?{?? ????module:?'roi_data_layer.layer'?? ????layer:?'RoIDataLayer'?? ????param_str:?"'num_classes':?16"?#按訓練集類別改,該值為類別數(shù)+1?? ??}?? }??
[plain]?view plaincopy
layer?{?? ??name:?"cls_score"?? ??type:?"InnerProduct"?? ??bottom:?"fc7"?? ??top:?"cls_score"?? ??param?{?lr_mult:?1.0?}?? ??param?{?lr_mult:?2.0?}?? ??inner_product_param?{?? ????num_output:?16?#按訓練集類別改,該值為類別數(shù)+1?? ????weight_filler?{?? ??????type:?"gaussian"?? ??????std:?0.01?? ????}?? ????bias_filler?{?? ??????type:?"constant"?? ??????value:?0?? ????}?? ??}?? }??
[plain]?view plaincopy
layer?{?? ??name:?"bbox_pred"?? ??type:?"InnerProduct"?? ??bottom:?"fc7"?? ??top:?"bbox_pred"?? ??param?{?lr_mult:?1.0?}?? ??param?{?lr_mult:?2.0?}?? ??inner_product_param?{?? ????num_output:?64?#按訓練集類別改,該值為(類別數(shù)+1)*4?? ????weight_filler?{?? ??????type:?"gaussian"?? ??????std:?0.001?? ????}?? ????bias_filler?{?? ??????type:?"constant"?? ??????value:?0?? ????}?? ??}?? }??
2.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage1_rpn_train.pt修改
[plain]?view plaincopy
layer?{?? ??name:?'input-data'?? ??type:?'Python'?? ??top:?'data'?? ??top:?'im_info'?? ??top:?'gt_boxes'?? ??python_param?{?? ????module:?'roi_data_layer.layer'?? ????layer:?'RoIDataLayer'?? ????param_str:?"'num_classes':?16"?#按訓練集類別改,該值為類別數(shù)+1?? ??}?? }??
3.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage2_fast_rcnn_train.pt修改
[plain]?view plaincopy
layer?{?? ??name:?'data'?? ??type:?'Python'?? ??top:?'data'?? ??top:?'rois'?? ??top:?'labels'?? ??top:?'bbox_targets'?? ??top:?'bbox_inside_weights'?? ??top:?'bbox_outside_weights'?? ??python_param?{?? ????module:?'roi_data_layer.layer'?? ????layer:?'RoIDataLayer'?? ????param_str:?"'num_classes':?16"?#按訓練集類別改,該值為類別數(shù)+1?? ??}?? }??
[plain]?view plaincopy
layer?{?? ??name:?"cls_score"?? ??type:?"InnerProduct"?? ??bottom:?"fc7"?? ??top:?"cls_score"?? ??param?{?lr_mult:?1.0?}?? ??param?{?lr_mult:?2.0?}?? ??inner_product_param?{?? ????num_output:?16?#按訓練集類別改,該值為類別數(shù)+1?? ????weight_filler?{?? ??????type:?"gaussian"?? ??????std:?0.01?? ????}?? ????bias_filler?{?? ??????type:?"constant"?? ??????value:?0?? ????}?? ??}?? }??
[plain]?view plaincopy
layer?{?? ??name:?"bbox_pred"?? ??type:?"InnerProduct"?? ??bottom:?"fc7"?? ??top:?"bbox_pred"?? ??param?{?lr_mult:?1.0?}?? ??param?{?lr_mult:?2.0?}?? ??inner_product_param?{?? ????num_output:?64?#按訓練集類別改,該值為(類別數(shù)+1)*4?? ????weight_filler?{?? ??????type:?"gaussian"?? ??????std:?0.001?? ????}?? ????bias_filler?{?? ??????type:?"constant"?? ??????value:?0?? ????}?? ??}?? }??
4.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage2_rpn_train.pt修改
[plain]?view plaincopy
layer?{?? ??name:?'input-data'?? ??type:?'Python'?? ??top:?'data'?? ??top:?'im_info'?? ??top:?'gt_boxes'?? ??python_param?{?? ????module:?'roi_data_layer.layer'?? ????layer:?'RoIDataLayer'?? ????param_str:?"'num_classes':?16"?#按訓練集類別改,該值為類別數(shù)+1?? ??}?? }??
5.py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/faster_rcnn_test.pt修改
[plain]?view plaincopy
layer?{?? ??name:?"cls_score"?? ??type:?"InnerProduct"?? ??bottom:?"fc7"?? ??top:?"cls_score"?? ??inner_product_param?{?? ????num_output:?16?#按訓練集類別改,該值為類別數(shù)+1?? ??}?? }??
[plain]?view plaincopy
layer?{?? ??name:?"bbox_pred"?? ??type:?"InnerProduct"?? ??bottom:?"fc7"?? ??top:?"bbox_pred"?? ??inner_product_param?{?? ????num_output:?64?#按訓練集類別改,該值為(類別數(shù)+1)*4?? ??}?? }??
6.py-faster-rcnn/lib/datasets/pascal_voc.py修改
(1)
[plain]?view plaincopy
class?pascal_voc(imdb):?? ????def?__init__(self,?image_set,?year,?devkit_path=None):?? ????????imdb.__init__(self,?'voc_'?+?year?+?'_'?+?image_set)?? ????????self._year?=?year?? ????????self._image_set?=?image_set?? ????????self._devkit_path?=?self._get_default_path()?if?devkit_path?is?None?\?? ????????????????????????????else?devkit_path?? ????????self._data_path?=?os.path.join(self._devkit_path,?'VOC'?+?self._year)?? ????????self._classes?=?('__background__',?#?always?index?0?? ?????????????????????????'你的標簽1','你的標簽2',你的標簽3','你的標簽4'?? ??????????????????????)??
上面要改的地方是
修改訓練集文件夾:
[plain]?view plaincopy
self._data_path?=?os.path.join(self._devkit_path,?'VOC'+self._year)??
用你的數(shù)據(jù)集直接替換原來VOC2007內(nèi)的Annotations,ImageSets和JPEGImages即可,以免出現(xiàn)各種錯誤。
修改標簽:
[plain]?view plaincopy
self._classes?=?('__background__',?#?always?index?0?? ?????????????????????????'你的標簽1','你的標簽2','你的標簽3','你的標簽4'?? ??????????????????????)??
修改成你的數(shù)據(jù)集的標簽就行。
(2)
[html]?view plaincopy
cls?=?self._class_to_ind[obj.find('name').text.lower().strip()]??
這里把標簽轉(zhuǎn)成小寫,如果你的標簽含有大寫字母,可能會出現(xiàn)KeyError的錯誤,所以建議標簽用小寫字母。
(去掉lower應該也行)
建議訓練的標簽還是用小寫的字母,如果最終需要用大寫字母或中文顯示標簽,可參考:
http://blog.csdn.net/sinat_30071459/article/details/51694037
7.py-faster-rcnn/lib/datasets/imdb.py修改
該文件的append_flipped_images(self)函數(shù)修改為:
[plain]?view plaincopy
def?append_flipped_images(self):?? ????????num_images?=?self.num_images?? ????????widths?=?[PIL.Image.open(self.image_path_at(i)).size[0]?? ??????????????????for?i?in?xrange(num_images)]?? ????????for?i?in?xrange(num_images):?? ????????????boxes?=?self.roidb[i]['boxes'].copy()?? ????????????oldx1?=?boxes[:,?0].copy()?? ????????????oldx2?=?boxes[:,?2].copy()?? ????????????boxes[:,?0]?=?widths[i]?-?oldx2?-?1?? ????????????print?boxes[:,?0]?? ????????????boxes[:,?2]?=?widths[i]?-?oldx1?-?1?? ????????????print?boxes[:,?0]?? ????????????assert?(boxes[:,?2]?>=?boxes[:,?0]).all()?? ????????????entry?=?{'boxes'?:?boxes,?? ?????????????????????'gt_overlaps'?:?self.roidb[i]['gt_overlaps'],?? ?????????????????????'gt_classes'?:?self.roidb[i]['gt_classes'],?? ?????????????????????'flipped'?:?True}?? ????????????self.roidb.append(entry)?? ????????self._image_index?=?self._image_index?*?2??
這里assert (boxes[:, 2] >= boxes[:, 0]).all()可能出現(xiàn)AssertionError,具體解決辦法參考:
http://blog.csdn.net/xzzppp/article/details/52036794
!!!為防止與之前的模型搞混,訓練前把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文件設置,迭代次數(shù)可在py-faster-rcnn\tools的train_faster_rcnn_alt_opt.py中修改:
[plain]?view plaincopy
max_iters?=?[80000,?40000,?80000,?40000]??
分別為4個階段(rpn第1階段,fast rcnn第1階段,rpn第2階段,fast rcnn第2階段)的迭代次數(shù)。可改成你希望的迭代次數(shù)。
如果改了這些數(shù)值,最好把py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt里對應的solver文件(有4個)也修改,stepsize小于上面修改的數(shù)值。
8.開始訓練
進入py-faster-rcnn,執(zhí)行:
[plain]?view plaincopy
./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 plaincopy
CLASSES?=?('__background__',?? ???????????'你的標簽1',?'你的標簽2',?'你的標簽3',?'你的標簽4')??
改成你的數(shù)據(jù)集標簽;
[plain]?view plaincopy
NETS?=?{'vgg16':?('VGG16',?? ??????????????????'VGG16_faster_rcnn_final.caffemodel'),?? ????????'zf':?('ZF',?? ??????????????????'ZF_faster_rcnn_final.caffemodel')}??
上面ZF的caffemodel改成你的caffemodel。
[plain]?view plaincopy
im_names?=?['1559.jpg','1564.jpg']??
改成你的測試圖片。(測試圖片放在py-faster-rcnn\data\demo中)
10.結(jié)果
在py-faster-rcnn下,
執(zhí)行:
[plain]?view plaincopy
./tools/demo.py?--net?zf??
或者將默認的模型改為zf:
[html]?view plaincopy
parser.add_argument('--net',?dest='demo_net',?help='Network?to?use?[vgg16]',?? ????????????????????????choices=NETS.keys(),?default='vgg16')??
修改:
[html]?view plaincopy
default='zf'??
執(zhí)行:
[plain]?view plaincopy
./tools/demo.py??
總結(jié)
以上是生活随笔為你收集整理的Faster-RCNN+ZF用自己的数据集训练模型(Python版本)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。