Faster RCNN 训练自己的检测模型
Faster RCNN 訓(xùn)練自己的檢測模型
一、準備自己的訓(xùn)練數(shù)據(jù)
根據(jù)pascal VOC 2007的訓(xùn)練數(shù)據(jù)集基本架構(gòu),第一步,當(dāng)然是要準備自己的訓(xùn)練圖片集,本文直接將自己的準備的圖片集(.jpg)扔到如下文件夾下:
$(py-faster-rcnn)/data/VOCdevkit2007/VOC2007/JPEGImages- 1
- 1
第二步,根據(jù)上述自己的要訓(xùn)練檢測的物體圖片集,標注相應(yīng)的.xml文件(我是自己寫了一個簡單的矩形框標注工具,生成相應(yīng)的xml文件,在網(wǎng)上找了很久也沒找到相應(yīng)的標注工具,后來只能自己寫了),同樣與VOC 2007的數(shù)據(jù)集中的xml文件放在一起,文件夾路徑如下:
$(py-faster-rcnn)/data/VOCdevkit2007/VOC2007/Annotations- 1
- 1
二、修改訓(xùn)練程序
file1:$(py-faster-rcnn)/models/ZF/faster_rcnn_alt_opt/stage1_fast_rcnn_train.pt
file2:$(py-faster-rcnn)/models/ZF/faster_rcnn_alt_opt/stage2_fast_rcnn_train.pt
file3:$(py-faster-rcnn)/models/ZF/faster_rcnn_alt_opt/stage1_rpn_train.pt
file4:$(py-faster-rcnn)/models/ZF/faster_rcnn_alt_opt/stage2_rpn_train.pt
上述兩個pt文件,所要更改的地方基本一樣,均是更改num_output的值,由于原來是21類物體檢測,本文加入了自己的一類物體進行訓(xùn)練,故由原來的21變成22即可,下面一層相應(yīng)的變?yōu)?8。
name: "ZF" 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': 22"} }......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: 22weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}} } 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: 88weight_filler {type: "gaussian"std: 0.001}bias_filler {type: "constant"value: 0}} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
file5:$(py-faster-rcnn)/lib/datasets/pascal_voc.py
在下面代碼中×××處添加自己加入的類即可。 def __init__(self, image_set, year, devkit_path=None):datasets.imdb.__init__(self, 'voc_' + year + '_' + image_set)self._year = yearself._image_set = image_setself._devkit_path = self._get_default_path() if devkit_path is None \else devkit_pathself._data_path = os.path.join(self._devkit_path, 'VOC' + self._year)self._classes = ('__background__', # always index 0'aeroplane', 'bicycle', 'bird', 'boat','bottle', 'bus', 'car', 'cat', 'chair','cow', 'diningtable', 'dog', 'horse','motorbike', 'person', 'pottedplant','sheep', 'sofa', 'train', 'tvmonitor','×××')- 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
參考資料:
https://github.com/rbgirshick/py-faster-rcnn/issues/34
三、訓(xùn)練過程中錯誤
error 1:assert (boxes[:, 2] >= boxes[:, 0]).all()
Process Process-1: Traceback (most recent call last): File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap self.run() File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run self._target(self._args, *self._kwargs) File "./tools/train_faster_rcnn_alt_opt.py", line 123, in train_rpn roidb, imdb = get_roidb(imdb_name) File "./tools/train_faster_rcnn_alt_opt.py", line 68, in get_roidb roidb = get_training_roidb(imdb) File "/home/microway/test/pytest/py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 121, in get_training_roidb imdb.append_flipped_images() File "/home/microway/test/pytest/py-faster-rcnn/tools/../lib/datasets/imdb.py", line 108, in append_flipped_images assert (boxes[:, 2] >= boxes[:, 0]).all() AssertionError- 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
error1 解決辦法:
將py-faster-rcnn/lib/datasets/imdb.py中的相應(yīng)代碼改成如下代碼即可:
def append_flipped_images(self):num_images = self.num_imageswidths = [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 - 1boxes[:, 2] = widths[i] - oldx1 - 1for b in range(len(boxes)):if boxes[b][2] < boxes[b][0]:boxes[b][0] = 0assert (boxes[:, 2] >= boxes[:, 0]).all()- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
error 2:IndexError: list index out of range
File "./tools/train_net.py", line 85, in roidb = get_training_roidb(imdb) File "/usr/local/fast-rcnn/tools/../lib/fast_rcnn/train.py", line 111, in get_training_roidb rdl_roidb.prepare_roidb(imdb) File "/usr/local/fast-rcnn/tools/../lib/roi_data_layer/roidb.py", line 23, in prepare_roidb roidb[i]['image'] = imdb.image_path_at(i) IndexError: list index out of range- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
error2 解決辦法:
刪除fast-rcnn-master/data/cache/ 文件夾下的.pkl文件,或者改名備份,重新訓(xùn)練即可。
參考資料:
https://github.com/rbgirshick/py-faster-rcnn/issues/34
https://github.com/rbgirshick/fast-rcnn/issues/79
總結(jié)
以上是生活随笔為你收集整理的Faster RCNN 训练自己的检测模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cuDNN兼容性问题造成的caffe/m
- 下一篇: 关于Faster R-CNN的一切——笔