转载自:Faster R-CNN教程 - CarryPotMan的博客 - 博客频道 - CSDN.NET http://blog.csdn.net/u012891472/article/details/51282809
本教程主要基于Python版本的faster R-CNN,因为python layer的使用,这个版本会比matlab的版本速度慢10%,但是准确率应该是差不多的。
目前已经实现的有两种方式:
- Alternative training
- Approximate joint training
推荐使用第二种,因为第二种使用的显存更小,而且训练会更快,同时准确率差不多甚至略高一点。
Contents
- 配置环境
- 安装步骤
- Demo
- 建立自己的数据集
- 训练和检测
配置环境
1配置python layers
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
2安装几个依赖cython, python-OpenCV, easydict
- 1
- 2
- 1
- 2
安装步骤
1克隆工程
- 1
- 1
2编译Cython模块
- 1
- 2
- 1
- 2
3编译caffe和pycaffe
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Demo
安装步骤完成后,就可以运行一下demo了。
- 1
- 2
- 1
- 2
训练自己的训练集
工程目录简介
首先工程的根目录简单的称为 FRCN_ROOT,可以看到根目录下有以下几个文件夹
- caffe-fast-rcnn
这里是caffe框架目录
- data
用来存放pretrained模型,比如imagenet上的,以及读取文件的cache缓存
- experiments
存放配置文件以及运行的log文件,另外这个目录下有scripts可以用end2end或者alt_opt两种方式训练。
- lib
用来存放一些python接口文件,如其下的datasets主要负责数据库读取,我们接下来的文件都是放在这个目录下的;config负责cnn一些训练的配置选项,建议根据自己的需要在experiment/cfgs/faster_rcnn_end2end.yml文件中进行设置覆盖config.py中的设置。
- models
里面存放了三个模型文件,小型网络的ZF,大型网络VGG16,中型网络VGG_CNN_M_1024。推荐使用VGG16,如果使用端到端的approximate joint training方法,开启CuDNN,只需要3G的显存即可。
- output
这里存放的是训练完成后的输出目录,默认会在faster_rcnn_end2end文件夹下
- tools
里面存放的是训练和测试的Python文件。
创建数据集
接下来我们就要创建自己的数据集了,这部分主要在lib目录里操作。这里下面存在3个目录:
- datasets
在这里修改读写数据的接口主要是datasets目录下
- fast_rcnn
主要存放的是python的训练和测试脚本,以及训练的配置文件config.py
- nms
做非极大抑制的部分,有gpu和cpu两种实现方式
- roi_data_layer
主要是一些ROI处理操作
- rpn
这就是RPN的核心代码部分,有生成proposals和anchor的方法
-
transform
-
utils
1构建自己的IMDB子类
1.1文件概述
可有看到datasets目录下主要有三个文件,分别是
- factory.py
- imdb.py
- pascal_voc.py
factory.py 是个工厂类,用类生成imdb类并且返回数据库共网络训练和测试使用;imdb.py 这里是数据库读写类的基类,封装了许多db的操作,但是具体的一些文件读写需要继承继续读写;pascal_voc.py Ross在这里用pascal_voc.py这个类来操作。
1.2读取文件函数分析
接下来我来介绍一下pasca_voc.py这个文件,我们主要是基于这个文件进行修改,里面有几个重要的函数需要修改
- def init(self, image_set, year, devkit_path=None)
这个是初始化函数,它对应着的是pascal_voc的数据集访问格式,其实我们将其接口修改的更简单一点。 - def image_path_at(self, i)
根据第i个图像样本返回其对应的path,其调用了image_path_from_index(self, index)作为其具体实现 - def image_path_from_index(self, index)
实现了 image_path的具体功能 - def _load_image_set_index(self)
加载了样本的list文件 - def _get_default_path(self)
获得数据集地址 - def gt_roidb(self)
读取并返回ground_truth的db - def selective_search_roidb
读取并返回ROI的db,这个是fast rcnn用的,faster版本的不用管这个函数。 - def _load_selective_search_roidb(self, gt_roidb)
加载预选框的文件 - def selective_search_IJCV_roidb(self)
在这里调用读取Ground_truth和ROI db并将db合并 - def _load_selective_search_IJCV_roidb(self, gt_roidb)
这里是专门读取作者在IJCV上用的dataset - def _load_pascal_annotation(self, index)
这个函数是读取gt的具体实现 - def _write_voc_results_file(self, all_boxes)
voc的检测结果写入到文件 - def _do_matlab_eval(self, comp_id, output_dir=’output’)
根据matlab的evluation接口来做结果的分析 - def evaluate_detections
其调用了_do_matlab_eval - def competition_mode
设置competitoin_mode,加了一些噪点
1.3训练数据格式
在我的检测任务里,我主要是在SED数据集上做行人检测,因此我这里只有background 和person 两类物体,为了操作方便,我像pascal_voc数据集里面一样每个图像用一个xml来标注。如果大家不知道怎么生成xml文件,可以用这个工具 labelImg?
这里我要特别提醒一下大家,一定要注意坐标格式,一定要注意坐标格式,一定要注意坐标格式,重要的事情说三遍!!!要不然你会犯很多错误都会是因为坐标不一致引起的报错。
1.4修改读取接口
这里是原始的pascal_voc的init函数,在这里,由于我们自己的数据集往往比voc的数据集要更简单的一些,在作者代码里面用了很多的路径拼接,我们不用去迎合他的格式,将这些操作简单化即可,在这里我会一一列举每个我修改过的函数。这里按照文件中的顺序排列。
修改后的初始化函数:
- 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
- 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
修改后的image_path_from_index:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
修改后的_load_image_set_index:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
gt_roidb(self):
这个函数里有个生成ground truth的文件,我需要特别说明一下,如果你再次训练的时候修改了数据库,比如添加或者删除了一些样本,但是你的数据库名字函数原来那个,必须要在data/cache/目录下把数据库的缓存文件.pkl给删除掉,否则其不会重新读取相应的数据库,而是直接从之前读入然后缓存的pkl文件中读取进来,这样修改的数据库并没有进入网络,而是加载了老版本的数据。
修改的_load_pascal_annotation(self, index):
- 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
- 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
因为我和Pascal用了一样的xml格式,所以这个函数我的改动不多。如果你想用txt文件保存ground truth,做出相应的修改即可。
想采用txt方式存储的童鞋,可以参考文末博客的写法。
坐标的顺序强调一下,要左上右下,并且x1必须要小于x2,这个是基本,反了会在坐标水平变换的时候会出错,坐标从0开始,如果已经是0,则不需要再-1。如果怕出错,可以直接把出界的的直接置0.
记得在最后的main下面也修改相应的路径
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
OK,在这里我们已经完成了整个的读取接口的改写。
2修改factory.py
当网络训练时会调用factory里面的get方法获得相应的imdb,
首先在文件头import 把pascal_voc改成hs
- 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
- 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
训练和检测
1.预训练模型介绍
首先在data目录下,有两个目录
-
faster_rcnn_models/
-
imagenet_models/
faster_rcnn_model文件夹下面是作者用faster rcnn训练好的三个网络,分别对应着小、中、大型网络,大家可以试用一下这几个网络,看一些检测效果,他们训练都迭代了80000次,数据集都是pascal_voc的数据集。
imagenet_model文件夹下面是在Imagenet上训练好的通用模型,在这里用来初始化网络的参数.
在这里我比较推荐大型网络,训练也挺快的,差不多25小时(titan black 6G)。
还有一个比较奇怪的现象,开启CuDNN一般情况是可以加速的,但是在训练ZF模型的时候,开启CuDNN反而会特别慢,所以大家如果训练特别慢,可以尝试关掉CuDNN。
2.修改模型文件配置
模型文件在models下面对应的网络文件夹下,在这里我用大型网络的配置文件修改为例子
比如:我的检测目标物是person ,那么我的类别就有两个类别即 background 和 person
因此,首先打开网络的模型文件夹,打开train.prototxt
修改的地方重要有三个
分别是这几个地方
- 首先在data层把num_classes 从原来的21类 20类+背景 ,改成 2类 人+背景
- 把RoI Proposal的’roi-data’层的 num_classes 改为 2
- 接着在cls_score层把num_output 从原来的21 改成 2
- 在bbox_pred层把num_output 从原来的84 改成8, 为检测类别个数乘以4,比如这里是2类那就是2*4=8
测试的时候,test.prototxt也需要做相应的修改。
OK,如果你要进一步修改网络训练中的学习速率,步长,gamma值,以及输出模型的名字,需要在同目录下的solver.prototxt中修改。
3.启动Fast RCNN网络训练
- 1
- 1
参数讲解:
-
这里的–是两个-,不要输错
-
train_net.py是网络的训练文件,之后的参数都是附带的输入参数
-
–gpu 代表机器上的GPU编号,如果是nvidia系列的tesla显卡,可以在终端中输入nvidia-smi来查看当前的显卡负荷,选择合适的显卡
-
–solver 代表模型的配置文件,train.prototxt的文件路径已经包含在这个文件之中
-
–weights 代表初始化的权重文件,这里用的是Imagenet上预训练好的模型,大型的网络我们选择用VGG16.v2.caffemodel
-
–imdb 这里给出的训练的数据库名字需要在factory.py的_sets中,我在文件里面有_sets[‘hs’],train_net.py这个文件会调用factory.py再生成hs这个类,来读取数据
4.启动Fast RCNN网络检测
可以参考tools下面的demo.py 文件,来做检测,并且将检测的坐标结果输出到相应的txt文件中。