这部分主要讲如何修改Faster R-CNN的代码,来训练自己的数据集,首先确保你已经编译安装了py-faster-rcnn,并且准备好了数据集,具体可参考我上一篇文章。
所有需要修改的训练代码都放到了py-faster-rcnn/lib
文件夹下,我们进入文件夹,里面主要用到的文件夹有:
读写数据接口都放在datasets/
文件夹下,我们进入文件夹,里面主要文件有:
从上面可以看出,我们主要对pascal_voc.py
文件进行修改。
我们主要是基于pasca_voc.py
这个文件进行修改,里面有几个重要的函数需要介绍:
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
文件拷贝成caltech.py
文件:
cp pascal_voc.py caltech.py
下面我们对caltech.py
文件进行修改,在这里我会一一列举每个我修改过的函数。这里按照文件中的顺序排列。。
这里是原始的pascal_voc的init函数,在这里,由于我们自己的数据集往往比voc的数据集要更简单的一些,在作者额代码里面用了很多的路径拼接,我们不用去迎合他的格式,将这些操作简单化即可。
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)
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文件中没有标签,否则之后训练会报错
'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)
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
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
其实没改,只是加了一行注释,从而更好理解路径问题。
直接注释即可
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}
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}
原始的函数
if __name__ == '__main__':
from datasets.pascal_voc import pascal_voc
d = pascal_voc('trainval', '2007')
res = d.roidb
from IPython import embed; embed()
修改后的函数
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里面的get方法获得相应的imdb,首先在文件头import 把pascal_voc改成caltech
在这个文件作者生成了多个数据库的路径,我们自己数据库只要给定根路径即可,修改主要有以下4个
devkit
。caltech_{}
。# --------------------------------------------------------
# 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__ 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_
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_
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()
# --------------------------------------------------------
# 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__ 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_
#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_
#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_
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()
在行首添加上 from .caltech import caltech
Faster R-CNN教程