最后更新日期:2016年4月29日html
本教程主要基于python版本的faster R-CNN,由于python layer的使用,这个版本会比matlab的版本速度慢10%,可是准确率应该是差很少的。python
目前已经实现的有两种方式:git
推荐使用第二种,由于第二种使用的显存更小,并且训练会更快,同时准确率差很少甚至略高一点。github
1配置python layersshell
#In your Makefile.config, make sure to have this line uncommented WITH_PYTHON_LAYER := 1 # Unrelatedly, it's also recommended that you use CUDNN USE_CUDNN := 1
2安装几个依赖cython, python-opencv, easydict
数据库
sudo apt-get install python-opencv sudo pip install cython easydict
1克隆工程缓存
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
2编译Cython模块网络
cd $FRCN_ROOT/lib make
3编译caffe和pycaffeapp
cd $FRCN_ROOT/caffe-fast-rcnn # Now follow the Caffe installation instructions here: # http://caffe.berkeleyvision.org/installation.html # If you're experienced with Caffe and have all of the requirements installed # and your Makefile.config in place, then simply do: make -j8 && make pycaffe
安装步骤完成后,就能够运行一下demo了。框架
cd $FRCN_ROOT ./tools/demo.py
首先工程的根目录简单的称为 FRCN_ROOT,能够看到根目录下有如下几个文件夹
这里是caffe框架目录
用来存放pretrained模型,好比imagenet上的,以及读取文件的cache缓存
存放配置文件以及运行的log文件,另外这个目录下有scripts能够用end2end或者alt_opt两种方式训练。
用来存放一些python接口文件,如其下的datasets主要负责数据库读取,config负责cnn一些训练的配置选项。
里面存放了三个模型文件,小型网络的ZF,大型网络VGG16,中型网络VGG_CNN_M_1024。推荐使用VGG16,若是使用端到端的approximate joint training方法,开启CuDNN,只须要3G的显存便可。
这里存放的是训练完成后的输出目录,默认会在faster_rcnn_end2end文件夹下
里面存放的是训练和测试的Python文件。
接下来咱们就要建立本身的数据集了,这部分主要在lib目录里操做。这里下面存在3个目录:
在这里修改读写数据的接口主要是datasets目录下
主要存放的是python的训练和测试脚本,以及训练的配置文件config.py
作非极大抑制的部分,有gpu和cpu两种实现方式
主要是一些ROI处理操做
这就是RPN的核心代码部分,有生成proposals和anchor的方法
transform
utils
1构建本身的IMDB子类
1.1文件概述
可有看到datasets目录下主要有三个文件,分别是
factory.py 是个工厂类,用类生成imdb类而且返回数据库共网络训练和测试使用;imdb.py 这里是数据库读写类的基类,分装了许多db的操做,可是具体的一些文件读写须要继承继续读写;pascal_voc.py Ross在这里用pascal_voc.py这个类来操做。
1.2读取文件函数分析
接下来我来介绍一下pasca_voc.py这个文件,咱们主要是基于这个文件进行修改,里面有几个重要的函数须要修改
1.3训练数据格式
在个人检测任务里,我主要是在SED数据集上作行人检测,所以我这里只有background 和person 两类物体,为了操做方便,我像pascal_voc数据集里面同样每一个图像用一个xml来标注。若是你们不知道怎么生成xml文件,能够用这个工具 labelImg?
这里我要特别提醒一下你们,必定要注意坐标格式,必定要注意坐标格式,必定要注意坐标格式,重要的事情说三遍!!!要否则你会犯不少错误都会是由于坐标不一致引发的报错。
1.4修改读取接口
这里是原始的pascal_voc的init函数,在这里,因为咱们本身的数据集每每比voc的数据集要更简单的一些,在做者代码里面用了不少的路径拼接,咱们不用去迎合他的格式,将这些操做简单化便可,在这里我会一一列举每一个我修改过的函数。这里按照文件中的顺序排列。
修改后的初始化函数:
class hs(imdb): def __init__(self, image_set, devkit_path=None): # modified imdb.__init__(self, image_set) self._image_set = image_set self._devkit_path = devkit_path #datasets路径 self._data_path = os.path.join(self._devkit_path,image_set) #图片文件夹路径 self._classes = ('__background__', # always index 0 'person') #two classes self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes))) # form the dict{'__background__':'0','person':'1'} self._image_ext = '.jpg' self._image_index = self._load_image_set_index('ImageList.txt') # Default to roidb handler self._roidb_handler = self.selective_search_roidb self._salt = str(uuid.uuid4()) self._comp_id = 'comp4' # PASCAL specific config options self.config = {'cleanup' : True, 'use_salt' : True, 'use_diff' : False, 'matlab_eval' : False, 'rpn_file' : None, 'min_size' : 16} #小于16个像素的框扔掉 assert os.path.exists(self._devkit_path), \ 'VOCdevkit path does not exist: {}'.format(self._devkit_path) assert os.path.exists(self._data_path), \ 'Path does not exist: {}'.format(self._data_path)
修改后的image_path_from_index:
def image_path_from_index(self, index): #modified """ Construct an image path from the image's "index" identifier. """ image_path = os.path.join(self._data_path,index +'.jpg') assert os.path.exists(image_path), \ 'Path does not exist: {}'.format(image_path) return image_path
修改后的_load_image_set_index:
def _load_image_set_index(self,imagelist): # modified """ Load the indexes listed in this dataset's image set file. """ # Example path to image set file: # self._devkit_path + /VOCdevkit2007/VOC2007/ImageSets/Main/val.txt image_set_file = os.path.join(self._devkit_path, imagelist) assert os.path.exists(image_set_file), \ 'Path does not exist: {}'.format(image_set_file) with open(image_set_file) as f: image_index = [x.strip() for x in f.readlines()] return image_index
gt_roidb(self):
这个函数里有个生成ground truth的文件,我须要特别说明一下,若是你再次训练的时候修改了数据库,好比添加或者删除了一些样本,可是你的数据库名字函数原来那个,必需要在data/cache/目录下把数据库的缓存文件.pkl给删除掉,不然其不会从新读取相应的数据库,而是直接从以前读入而后缓存的pkl文件中读取进来,这样修改的数据库并无进入网络,而是加载了老版本的数据。
修改的_load_pascal_annotation(self, index):
def _load_pascal_annotation(self, index): #modified """ Load image and bounding boxes info from XML file in the PASCAL VOC format. """ filename = os.path.join(self._devkit_path, 'Annotations', index + '.xml') tree = ET.parse(filename) objs = tree.findall('object') if not self.config['use_diff']: # Exclude the samples labeled as difficult non_diff_objs = [ obj for obj in objs if int(obj.find('difficult').text) == 0] # if len(non_diff_objs) != len(objs): # print 'Removed {} difficult objects'.format( # len(objs) - len(non_diff_objs)) objs = non_diff_objs num_objs = len(objs) boxes = np.zeros((num_objs, 4), dtype=np.uint16) gt_classes = np.zeros((num_objs), dtype=np.int32) overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32) # "Seg" area for pascal is just the box area seg_areas = np.zeros((num_objs), dtype=np.float32) # Load object bounding boxes into a data frame. for ix, obj in enumerate(objs): bbox = obj.find('bndbox') # Make pixel indexes 0-based x1 = float(bbox.find('xmin').text) y1 = float(bbox.find('ymin').text) x2 = float(bbox.find('xmax').text) y2 = float(bbox.find('ymax').text) cls = self._class_to_ind[obj.find('name').text.lower().strip()] boxes[ix, :] = [x1, y1, x2, y2] gt_classes[ix] = cls overlaps[ix, cls] = 1.0 seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1) overlaps = scipy.sparse.csr_matrix(overlaps) return {'boxes' : boxes, 'gt_classes': gt_classes, 'gt_overlaps' : overlaps, 'flipped' : False, 'seg_areas' : seg_areas}
由于我和Pascal用了同样的xml格式,因此这个函数个人改动很少。若是你想用txt文件保存ground truth,作出相应的修改便可。
坐标的顺序强调一下,要左上右下,而且x1必需要小于x2,这个是基本,反了会在坐标水平变换的时候会出错,坐标从0开始,若是已是0,则不须要再-1。若是怕出错,能够直接把出界的的直接置0.
记得在最后的main下面也修改相应的路径
from datasets.hs import hs d = hs('hs', '/home/zyy/workspace/wangml/py-faster-rcnn/lib/datasets/') res = d.roidb from IPython import embed; embed()
OK,在这里咱们已经完成了整个的读取接口的改写。
2修改factory.py
当网络训练时会调用factory里面的get方法得到相应的imdb,
首先在文件头import 把pascal_voc改为hs
# -------------------------------------------------------- # Fast R-CNN # Copyright (c) 2015 Microsoft # Licensed under The MIT License [see LICENSE for details] # Written by Ross Girshick # -------------------------------------------------------- """Factory method for easily getting imdbs by name.""" __sets = {} from datasets.hs import hs import numpy as np # # Set up voc_<year>_<split> using selective search "fast" mode # for year in ['2007', '2012']: # for split in ['train', 'val', 'trainval', 'test']: # name = 'voc_{}_{}'.format(year, split) # __sets[name] = (lambda split=split, year=year: pascal_voc(split, year)) # # # Set up coco_2014_<split> # for year in ['2014']: # for split in ['train', 'val', 'minival', 'valminusminival']: # name = 'coco_{}_{}'.format(year, split) # __sets[name] = (lambda split=split, year=year: coco(split, year)) # # # Set up coco_2015_<split> # for year in ['2015']: # for split in ['test', 'test-dev']: # name = 'coco_{}_{}'.format(year, split) # __sets[name] = (lambda split=split, year=year: coco(split, year)) name = 'hs' devkit = '/home/zyy/workspace/wangml/py-faster-rcnn/lib/datasets/' __sets['hs'] = (lambda name = name,devkit = devkit: hs(name,devkit)) def get_imdb(name): """Get an imdb (image database) by name.""" if not __sets.has_key(name): raise KeyError('Unknown dataset: {}'.format(name)) return __sets[name]() def list_imdbs(): """List all registered imdbs.""" return __sets.keys()
1.预训练模型介绍
首先在data目录下,有两个目录
faster_rcnn_models/
imagenet_models/
faster_rcnn_model文件夹下面是做者用faster rcnn训练好的三个网络,分别对应着小、中、大型网络,你们能够试用一下这几个网络,看一些检测效果,他们训练都迭代了80000次,数据集都是pascal_voc的数据集。
imagenet_model文件夹下面是在Imagenet上训练好的通用模型,在这里用来初始化网络的参数.
在这里我比较推荐先用中型网络训练,中型网络训练和检测的速度都比较快,效果也都比较理想,大型网络的话训练速度比较慢,中型网络训练大概半天,大型网络的话用25个小时。
2.修改模型文件配置
模型文件在models下面对应的网络文件夹下,在这里我用中型网络的配置文件修改成例子
好比:个人检测目标物是person ,那么个人类别就有两个类别即 background 和 person
所以,首先打开网络的模型文件夹,打开train.prototxt
修改的地方重要有三个
分别是个地方
OK,若是你要进一步修改网络训练中的学习速率,步长,gamma值,以及输出模型的名字,须要在同目录下的solver.prototxt中修改。
3.启动Fast RCNN网络训练
python ./tools/train_net.py --gpu 1 --solver models/hs/faster_rcnn_end2end/solver.prototxt --weights data/imagenet_models/VGG_CNN_M_1024.v2.caffemodel --imdb hs --iters 80000 --cfg experiments/cfgs/faster_rcnn_end2end.yml
参数讲解:
这里的–是两个-,不要输错
train_net.py是网络的训练文件,以后的参数都是附带的输入参数
--gpu 表明机器上的GPU编号,若是是nvidia系列的tesla显卡,能够在终端中输入nvidia-smi来查看当前的显卡负荷,选择合适的显卡
--solver 表明模型的配置文件,train.prototxt的文件路径已经包含在这个文件之中
--weights 表明初始化的权重文件,这里用的是Imagenet上预训练好的模型,中型的网络咱们选择用VGG_CNN_M_1024.v2.caffemodel
--imdb 这里给出的训练的数据库名字须要在factory.py的_sets中,我在文件里面有_sets[‘hs’],train_net.py这个文件会调用factory.py再生成hs这个类,来读取数据
4.启动Fast RCNN网络检测
能够参考tools下面的demo.py 文件,来作检测,而且将检测的坐标结果输出到相应的txt文件中。
鉴于以前我用的版本是15年11月的版本,有些小伙伴在使用此教程时会有一些错误,因此我从新作了部分修订,目前可以在2016年4月29日版本的版本上成功运行,若是有问题,随时联系我。
参考博客:http://www.cnblogs.com/louyihang-loves-baiyan/p/4885659.html