下面修改自己的样本
1. 这里使用行人的样本,路径为 /home/china409/Myprogram/PythonProgram/yx/TFFRCNN-master/human_data
2. 建立软链接
ln -s/home/china409/Myprogram/PythonProgram/yx/TFFRCNN-master/human_data human2017
并将该链接copy到data目录下
3. train_net.py文件中设置自己数据库的名称,这里我自己设置的叫做‘my_2017_human’
parser.add_argument('--imdb',dest='imdb_name',
help='dataset to train on',
default='my_2017_human', type=str)
4. 修改文件:/home/china409/Myprogram/PythonProgram/yx/TFFRCNN-master/lib/datasets/factory.py
添加如下代码:
#设置自己的数据
for mydbname in['my_2017_human']:
name = 'my_2017_human'
__sets[name] = (lambdasplit=name,year='2017':
pascal_voc(split, year))
5. 修改配置文件config.py
__C.NCLASSES = 7 (此处为训练样本特征的个数加一,本文样本为6个特征)
6. 修改文件:/home/china409/Myprogram/PythonProgram/yx/TFFRCNN-master/lib/datasets/pascal_voc.py。该文件中修改的较多
(1) 首先修改初始化数据库语句:
(2) 设置默认路径:
def _get_default_path(self):
"""
Return thedefault path where PASCAL VOC is expected to be installed.
"""
# return os.path.join(cfg.DATA_DIR,'VOCdevkit' + self._year)
returnos.path.join(cfg.DATA_DIR,
'human_data') # 设置软链接的路径-----/home/china409/Myprogram/PythonProgram/yx/TFFRCNN-master-human/data/human_data
(3) 第二设置训练集路径:
(4) 设置图片集合文件:
def _load_image_set_index(self):
"""
Load theindexes listed in this dataset's image set file.
"""
# Examplepath 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')
image_set_file = os.path.join(self._data_path,'IMAGES_SET',
'walker.txt')#设置图片集合文件
assert os.path.exists(image_set_file), \
'Path doesnot exist: {}'.format(image_set_file)
with open(image_set_file)as f:
image_index = [x.strip() for xin f.readlines()]
return image_index
(5) 设置训练图片路径:
def image_path_from_index(self, index):
"""
Constructan image path from the image's "index" identifier.
"""
image_path = os.path.join(self._data_path,'IMAGES_TRAIN',
index + self._image_ext)
assert os.path.exists(image_path), \
'Path doesnot exist: {}'.format(image_path)
return image_path
(6) 设置样本中检测特征的内容
# 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._classes = ('__background__', # alwaysindex 0
'head','top', 'down','shoes','hat','bag')
(7) 修改_load_pascal_annotation方法,以适合自己样本的xml文件:
def _load_pascal_annotation(self, index):
"""
Load imageand bounding boxes info from XML file in the PASCAL VOC
format.
该函数针对xml文件进行解析。
"""
filename = os.path.join(self._data_path, 'ANNOTATIONS_TRAIN',index + '.xml')
tree = ET.parse(filename)
objs = tree.findall('subcomponent')#检测的事物标签
# if notself.config['use_diff']:
# # Exclude the samples labeled as difficult
# non_diff_objs = [
# obj for obj in objs ifint(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)
ishards = np.zeros((num_objs), dtype=np.int32)
# Loadobject bounding boxes into a data frame.
offset = 0;
for ixtemp, obj in enumerate(objs):
ix = ixtemp + offset;
bbox = obj.find('bndbox')
# Makepixel indexes 0-based
if type(bbox) is type(None) :#没有bndbox节点时为shoes结构或者bag结构,有变化
# printET.tostring(obj)
otherStructure = obj.find('name');
if otherStructure.text == 'shoes':
#shoes
if obj.find('xmin_l').text == 'NULL':
continue
x1 = float(obj.find('xmin_l').text) - 1
y1 = float(obj.find('ymin_l').text) - 1
x2 = float(obj.find('xmax_l').text) - 1
y2 = float(obj.find('ymax_l').text) - 1
# diffc =obj.find('difficult')
# difficult= 0 if diffc == None else int(diffc.text)
#ishards[ix] = difficult
#
# 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)
#
# offset+=1;
# ix =ixtemp + offset;
# x1 =float(obj.find('xmin_r').text) - 1
# y1 =float(obj.find('ymin_r').text) - 1
# x2 = float(obj.find('xmax_r').text)- 1
# y2 =float(obj.find('ymax_r').text) - 1
elif otherStructure.text == 'bag':
#bag
bbbox = obj.find('id_1')
bbox = bbbox.find('bndbox')
if bbox.find('xmin').text != 'NULL':
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
else:
continue
else:
if bbox.find('xmin').text == 'NULL':#在有bndbox节点时排除属性为NULL的节点
continue
else:
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
#排除小于1的情况
if x1 < 0:
x1 = 0
if y1 < 0:
y1 = 0
if x2 < 0:
x2 = 0
if y2 < 0:
y2 = 0
diffc = obj.find('difficult')
difficult = 0 if diffc == None else int(diffc.text)
ishards[ix] = difficult
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_ishard': ishards,
'gt_overlaps': overlaps,
'flipped' : False,
'seg_areas' :seg_areas}
7. 修改文件:/home/china409/Myprogram/PythonProgram/yx/TFFRCNN-master/experiments/cfgs/faster_rcnn_end2end.yml
可以拷贝后修改:
/home/china409/Myprogram/PythonProgram/yx/TFFRCNN-master/experiments/cfgs/faster_rcnn_end2end_human.yml
主要修改前两个路径以及特征的个数。
8. 修改train_net.py文件:
parser.add_argument('--cfg',dest='cfg_file',
help='optional config file',
default='/home/china409/Myprogram/PythonProgram/yx/TFFRCNN-master-human/experiments/cfgs/faster_rcnn_end2end_walker.yml',type=str)
9. 在运行前删除缓存文件
10. 训练完成后修改demo.py文件。
修改为自己模型的特征
# CLASSES = ('__background__',
# 'aeroplane', 'bicycle', 'bird','boat',
# 'bottle', 'bus', 'car', 'cat','chair',
# 'cow', 'diningtable', 'dog','horse',
# 'motorbike', 'person', 'pottedplant',
# 'sheep', 'sofa', 'train','tvmonitor')
CLASSES = ('__background__','head','top', 'down','shoes','hat','bag')
修改模型路径:
parser.add_argument('--model',dest='model',help='Model path',
default='/home/china409/Myprogram/PythonProgram/yx/TFFRCNN-master/output/walker/my_2017_human')
11. 替换/home/china409/Myprogram/PythonProgram/yx/TFFRCNN-master/data/demo中的测试集。运行demo.py进行测试