py-faster-rcnn用自己的数据训练模型
環境:
- ubuntu14.04
- CUDA7.5
- python2.7
- opencv2.0以上
caffe及py-faster-rcnn的配置安裝過程可以參考我的另一篇博客:深度學習框架caffe及py-faster-rcnn詳細配置安裝過程
做訓練數據集的過程可以參考這篇博客:將數據集做成VOC2007格式用于Faster-RCNN訓練
1、下載VOC2007數據集
百度云地址:http://pan.baidu.com/s/1gfdSFRX
解壓,然后,將該數據集放在py-faster-rcnn\data目錄下,用你的訓練數據集替換VOC2007數據集。(替換Annotations,ImageSets和JPEGImages)
(用你的Annotations,ImagesSets和JPEGImages替換py-faster-rcnn\data\VOCdevkit2007\VOC2007中對應文件夾)
文件結構如下所示:
- 1
- 2
- 1
- 2
Annotations中是所有的xml文件
JPEGImages中是所有的訓練圖片
Main中是4個txt文件,其中test.txt是測試集,train.txt是訓練集,val.txt是驗證集,trainval.txt是訓練和驗證集
2、訓練前的一些修改
(1)py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage1_fast_rcnn_train.pt修改
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': 2" #按訓練集類別改,該值為類別數+1 } }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
(2)py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage1_rpn_train.pt修改
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': 2" #按訓練集類別改,該值為類別數+1 } }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
(3)py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage2_fast_rcnn_train.pt修改
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': 2" #按訓練集類別改,該值為類別數+1 } }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
(4)py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/stage2_rpn_train.pt修改
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': 2" #按訓練集類別改,該值為類別數+1 } }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
(5)py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/faster_rcnn_test.pt修改
layer { name: "cls_score" type: "InnerProduct" bottom: "fc7" top: "cls_score" inner_product_param { num_output: 2 #按訓練集類別改,該值為類別數+1 } }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
(6)py-faster-rcnn/lib/datasets/pascal_voc.py修改
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' )- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
將其中
self._classes = ('__background__', # always index 0 '你的標簽1','你的標簽2','你的標簽3','你的標簽4' )- 1
- 2
- 3
- 1
- 2
- 3
中的標簽修改成你的數據集的標簽。
(7)py-faster-rcnn/lib/datasets/imdb.py修改
該文件的append_flipped_images(self)函數修改為:
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- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
(8)其他
為防止與之前的模型搞混,訓練前把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中修改:
- 1
- 1
分別為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小于上面修改的數值。
3、訓練
在py-faster-rcnn根目錄下執行:
./experiments/scripts/faster_rcnn_alt_opt.sh 0 ZF pascal_voc- 1
- 1
訓練結束如下圖所示:
在py-faster-rcnn/output/faster_rcnn_alt_opt/voc_2007_trainval/下會有ZF_faster_rcnn_final.caffemodel ,這就是我們用自己的數據集訓練得到的最終模型。
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
4、測試
將上述的ZF_faster_rcnn_final.caffemodel復制到py-faster-rcnn\data\faster_rcnn_models,修改修改:
py-faster-rcnn\tools\demo.py:
- 1
- 2
- 1
- 2
改為之前的訓練數據集的標簽。
在修改def parse_args()函數:
- 1
- 2
- 1
- 2
選擇模型ZF:default=’zf’
注:
若修改了模型的名稱,如將ZF_faster_rcnn_final.caffemodel重命名為CAR_faster_rcnn_final.caffemodel,并將需要檢測的圖片放在/data/car/
目錄下,則還需要修改py-faster-rcnn\tools\demo.py:
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 1
- 2
- 3
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
運行demo.py
cd ./tools python demo.py- 1
- 2
- 1
- 2
5、可能遇到的問題
(1)
Traceback (most recent call last):File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrapself.run()File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in runself._target(*self._args, **self._kwargs)File "./tools/train_faster_rcnn_alt_opt.py", line 189, in train_fast_rcnnroidb, imdb = get_roidb(imdb_name, rpn_file=rpn_file)File "./tools/train_faster_rcnn_alt_opt.py", line 67, in get_roidbroidb = get_training_roidb(imdb)File "/home/wlw/language/caffe/py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 118, in get_training_roidbimdb.append_flipped_images()File "/home/wlw/language/caffe/py-faster-rcnn/tools/../lib/datasets/imdb.py", line 114, in append_flipped_imagesassert (boxes[:, 2] >= boxes[:, 0]).all()- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
這是因為用于訓練的圖片中的標記的左上角坐標(x,y)可能為0,或標記的區域溢出圖片。而faster rcnn會對Xmin,Ymin,Xmax,Ymax進行減一操作,如果Xmin為0,減一后變為65535。
解決方法:
修改lib/datasets/imdb.py中的append_flipped_images()函數:
在代碼
- 1
- 1
下,添加代碼:
for b in range(len(boxes)):if boxes[b][2] < boxes[b][0]:boxes[b][0] = 0- 1
- 2
- 3
- 1
- 2
- 3
若仍不能解決,修改lib/datasets/pascal_voc.py中的_load_pascal_annotation()函數,將其中將對Xmin,Ymin,Xmax,Ymax減一去掉,改為:
for ix, obj in enumerate(objs):bbox = obj.find('bndbox')# Make pixel indexes 0-basedx1 = float(bbox.find('xmin').text)y1 = float(bbox.find('ymin').text)x2 = float(bbox.find('xmax').text)y2 = float(bbox.find('ymax').text)- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
(2)
Traceback (most recent call last):File "./tools/test_net.py", line 90, in <module>test_net(net, imdb, max_per_image=args.max_per_image, vis=args.vis)File "/home/wlw/language/caffe/py-faster-rcnn/tools/../lib/fast_rcnn/test.py", line 295, in test_netimdb.evaluate_detections(all_boxes, output_dir)File "/home/wlw/language/caffe/py-faster-rcnn/tools/../lib/datasets/pascal_voc.py", line 317, in evaluate_detectionsself._write_voc_results_file(all_boxes)File "/home/wlw/language/caffe/py-faster-rcnn/tools/../lib/datasets/pascal_voc.py", line 244, in _write_voc_results_filewith open(filename, 'wt') as f: IOError: [Errno 2] No such file or directory: '/home/wlw/language/caffe/py-faster-rcnn/data/VOCdevkit2007/results/VOC2007/Main/comp4_507d2b05-379f-4cf1-a1d4-5bd729d32fb0_det_test_building.txt'- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
解決方法:檢查./data/VOCdevkit2007文件夾是否復制完整,./data/VOCdevkit2007/results/VOC2007目錄下是否有Layout Main Segmentation三個文件夾。
參考資料:
Faster-RCNN+ZF用自己的數據集訓練模型(Python版本)
解決faster-rcnn中訓練時assert(boxes[:,2]>=boxes[:,0]).all()的問題
Faster RCNN 訓練自己的檢測模型
總結
以上是生活随笔為你收集整理的py-faster-rcnn用自己的数据训练模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度学习框架caffe及py-faste
- 下一篇: Deep Learning---py-f