深度学习实践经验:用Faster R-CNN训练Caltech数据集——修改读写接口

前言

这部分主要讲如何修改Faster R-CNN的代码,来训练自己的数据集,首先确保你已经编译安装了py-faster-rcnn,并且准备好了数据集,具体可参考我上一篇文章

py-faster-rcnn文件结构

  • caffe-fast-rcnn
    这里是caffe框架目录,用来进行caffe编译安装
  • data
    用来存放pre trained模型,比如ImageNet上的,要训练的数据集以及读取文件的cache缓存。
  • experiments
    存放配置文件,运行的log文件,另外这个目录下有scripts 用来获取imagenet的模型,以及作者训练好的fast rcnn模型,以及相应的pascal-voc数据集
  • lib
    用来存放一些python接口文件,如其下的datasets主要负责数据库读取,config负责cnn一些训练的配置选项
  • matlab
    放置matlab与python的接口,用matlab来调用实现detection
  • models
    里面存放了三个模型文件,小型网络的ZF,大型网络VGG16,中型网络VGG_CNN_M_1024
  • output
    这里存放的是训练完成后的输出目录,默认会在default文件夹下
  • tools
    里面存放的是训练和测试的Python文件

修改训练代码

所要操作文件结构介绍

所有需要修改的训练代码都放到了py-faster-rcnn/lib文件夹下,我们进入文件夹,里面主要用到的文件夹有:

  • datasets:该目录下主要存放读写数据接口。
  • fast-rcnn:该目录下主要存放的是python的训练和测试脚本,以及训练的配置文件。
  • roi_data_layer:该目录下主要存放一些ROI处理操作文件。
  • utils:该目录下主要存放一些通用操作比如非极大值nms,以及计算bounding box的重叠率等常用功能。

读写数据接口都放在datasets/文件夹下,我们进入文件夹,里面主要文件有:

  • factory.py:这是个工厂类,用类生成imdb类并且返回数据库共网络训练和测试使用。
  • imdb.py:这是数据库读写类的基类,分装了许多db的操作,但是具体的一些文件读写需要继承继续读写
  • pascal_voc.py:这是imdb的子类,里面定义许多函数用来进行所有的数据读写操作。

从上面可以看出,我们主要对pascal_voc.py文件进行修改。

pascal_voc.py文件代码分析

我们主要是基于pasca_voc.py这个文件进行修改,里面有几个重要的函数需要介绍:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def __init__(self, image_set, 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文件,根据ImageSet/Main/文件夹下的文件进行image_index的加载。
def _get_default_path(self): # 获得数据集地址
def gt_roidb(self): # 读取并返回ground_truth的db
def rpn_roidb(self): # 加载rpn产生的roi,调用_load_rpn_roidb(self, gt_roidb):函数作为其具体实现
def _load_rpn_roidb(self, gt_roidb): # 加载rpn_file
def _load_pascal_annotation(self, index): # 这个函数是读取gt的具体实现
def _write_voc_results_file(self, all_boxes): # 将voc的检测结果写入到文件
def _do_python_eval(self, output_dir = 'output'): # 根据python的evluation接口来做结果的分析

修改pascal_voc.py文件

要想对自己的数据集进行读取,我们主要是进行pascal_voc.py文件的修改,但是为了不破坏源文件,我们可以将pascal_voc.py进行拷贝复制,从而进行修改。这里我将pascal_voc.py文件拷贝成caltech.py文件:

1
cp pascal_voc.py caltech.py

下面我们对caltech.py文件进行修改,在这里我会一一列举每个我修改过的函数。这里按照文件中的顺序排列。。

init函数修改

这里是原始的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
30
31
32
33
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
'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor')
self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes)))
self._image_ext = '.jpg'
self._image_index = self._load_image_set_index()
# 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' : 2}
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)
修改后的函数
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
def __init__(self, image_set, devkit_path=None):# initial function,把year删除
imdb.__init__(self, image_set) # imageset is train.txt or test.txt
self._image_set = image_set
self._devkit_path = devkit_path # devkit_path = '~/py-faster-rcnn/data/VOCdevkit'
self._data_path = os.path.join(self._devkit_path, 'Caltech') # _data_path = '~/py-faster-rcnn/data/VOCdevkit/Caltech'
self._classes = ('__background__', # always index 0
'person') # 我只有‘background’和‘person’两类
self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes)))
self._image_ext = '.jpg'
self._image_index = self._load_image_set_index()
# 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' : True, # 我把use_diff改为true了,因为我的数据集xml文件中没有<difficult>标签,否则之后训练会报错
'matlab_eval' : False,
'rpn_file' : None,
'min_size' : 2}
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)

_load_image_set_index函数修改

原始的函数
1
2
3
4
5
6
7
8
9
10
11
12
13
def _load_image_set_index(self):
"""
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._data_path, 'ImageSets', 'Main',
self._image_set + '.txt')
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
修改后的函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def _load_image_set_index(self):
"""
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
# /home/jk/py-faster-rcnn/data/VOCdevkit/Caltech/ImageSets/Main/train.txt
image_set_file = os.path.join(self._data_path, 'ImageSets', 'Main',
self._image_set + '.txt')
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

其实没改,只是加了一行注释,从而更好理解路径问题。

_get_default_path函数修改

直接注释即可

_load_pascal_annotation函数修改

原始的函数
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
def _load_pascal_annotation(self, index):
"""
Load image and bounding boxes info from XML file in the PASCAL VOC
format.
"""
filename = os.path.join(self._data_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) - 1
y1 = float(bbox.find('ymin').text) - 1
x2 = float(bbox.find('xmax').text) - 1
y2 = float(bbox.find('ymax').text) - 1
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}
修改后的函数
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
def _load_pascal_annotation(self, index):
"""
Load image and bounding boxes info from XML file in the PASCAL VOC
format.
"""
filename = os.path.join(self._data_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
# 这里我把‘-1’全部删除掉了,防止有的数据是0开始,然后‘-1’导致变为负数,产生AssertError错误
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}

main函数修改

原始的函数

1
2
3
4
5
if __name__ == '__main__':
from datasets.pascal_voc import pascal_voc
d = pascal_voc('trainval', '2007')
res = d.roidb
from IPython import embed; embed()

修改后的函数

1
2
3
4
5
if __name__ == '__main__':
from datasets.caltech import caltech # 导入caltech包
d = caltech('train', '/home/jk/py-faster-rcnn/data/VOCdevkit')#调用构造函数,传入imageset和路径
res = d.roidb
from IPython import embed; embed()

至此读取接口修改完毕,该文件中的其他函数并未修改。

修改factory.py文件

当网络训练时会调用factory里面的get方法获得相应的imdb,首先在文件头import 把pascal_voc改成caltech

在这个文件作者生成了多个数据库的路径,我们自己数据库只要给定根路径即可,修改主要有以下4个

  • 函数之后有两个多级的for循环,也将其注释
  • 直接定义devkit
  • 利用创建自己的训练和测试的imdb set,这里的name的格式为caltech_{}

原始的代码

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
# --------------------------------------------------------
# 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.pascal_voc import pascal_voc
from datasets.coco import coco
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))
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
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
# --------------------------------------------------------
# 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.caltech import caltech # 导入caltech包
#from datasets.coco import coco
#import numpy as np
devkit = '/home/jk/py-faster-rcnn/data/VOCdevkit'
# 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))
# Set up caltech_<split>
for split in ['train', 'test']:
name = 'caltech_{}'.format(split)
__sets[name] = (lambda imageset=split, devkit=devkit: caltech(imageset, 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()

修改init.py文件

在行首添加上 from .caltech import caltech

总结

  • 坐标的顺序我再说一次,要左上右下,并且x1必须要小于x2,这个是基本,反了会在坐标水平变换的时候会出错,坐标从0开始,如果已经是0,则不需要再-1。
  • 训练图像的大小不要太大,否则生成的OP也会太多,速度太慢,图像样本大小最好调整到500,600左右,然后再提取OP
  • 如果读取并生成pkl文件之后,实际数据内容或者顺序还有问题,记得要把data/cache/下面的pkl文件给删掉。

参考博客

  1. Fast RCNN训练自己的数据集 (2修改读写接口)
  2. Faster R-CNN教程