Faster RCNN 训练自己的检测模型

Faster RCNN 训练自己的检测模型

一、准备自己的训练数据

根据pascal VOC 2007的训练数据集基本架构,第一步,当然是要准备自己的训练图片集,本文直接将自己的准备的图片集(.jpg)扔到如下文件夹下:

$(py-faster-rcnn)/data/VOCdevkit2007/VOC2007/JPEGImages

第二步,根据上述自己的要训练检测的物体图片集,标注相应的.xml文件(我是自己写了一个简单的矩形框标注工具,生成相应的xml文件,在网上找了很久也没找到相应的标注工具,后来只能自己写了),同样与VOC 2007的数据集中的xml文件放在一起,文件夹路径如下:

$(py-faster-rcnn)/data/VOCdevkit2007/VOC2007/Annotations

二、修改训练程序

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类物体检测,本文加入了自己的一类物体进行训练,故由原来的21变成22即可,下面一层相应的变为88。

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: 22
    weight_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: 88
    weight_filler {
      type: "gaussian"
      std: 0.001
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}

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 = 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
                         'aeroplane', 'bicycle', 'bird', 'boat',
                         'bottle', 'bus', 'car', 'cat', 'chair',
                         'cow', 'diningtable', 'dog', 'horse',
                         'motorbike', 'person', 'pottedplant',
                         'sheep', 'sofa', 'train', 'tvmonitor','×××')

参考资料:
https://github.com/rbgirshick/py-faster-rcnn/issues/34

三、训练过程中错误

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

error1 解决办法:

将py-faster-rcnn/lib/datasets/imdb.py中的相应代码改成如下代码即可:

    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
            boxes[:, 2] = widths[i] - oldx1 - 1

            for b in range(len(boxes)):
                if boxes[b][2] < boxes[b][0]:
                   boxes[b][0] = 0

            assert (boxes[:, 2] >= boxes[:, 0]).all()

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

error2 解决办法:

删除fast-rcnn-master/data/cache/ 文件夹下的.pkl文件,或者改名备份,重新训练即可。

参考资料:
https://github.com/rbgirshick/py-faster-rcnn/issues/34
https://github.com/rbgirshick/fast-rcnn/issues/79

你可能感兴趣的:(RCNN)