上一篇已经把 detectron装起来了
现在就把voc数据转为coco数据,因为detectron只支持coco。
原谅我不懂caffe2.
我有一批数据,是人脸检测的。
我要用这个框架就必须进行如下几个步骤。
- 数据转换
- 下载pkl预训练文件
- 设置yaml,这是配置文件
- 修改文件
- 推送数据
1. 数据转换
使用voc_to_coco.py把voc装换成coco
"""
Created on Tue Jun 12 10:24:36 2018
将voc格式转json格式用于caffe2的detectron的训练
在detectron中voc_2007_train.json和voc_2007_val.json中categories的顺序必须保持一致
因此最好事先确定category的顺序,书写在category_set中
@author: yantianwang
"""
import xml.etree.ElementTree as ET
import os
import json
import collections
coco = dict()
coco['images'] = []
coco['type'] = 'instances'
coco['annotations'] = []
coco['categories'] = []
#category_set = dict()
image_set = set()
image_id = 2019000001 #train:2018xxx; val:2019xxx; test:2020xxx
category_item_id = 1
annotation_id = 1
# category_set = ['small-vehicle', 'large-vehicle', 'plane', 'harbor', 'ship',
# 'tennis-court', 'soccer-ball-field', 'ground-track-field',
# 'baseball-diamond', 'swimming-pool', 'roundabout', 'basketball-court',
# 'storage-tank', 'bridge', 'helicopter']
category_set = ['face', ]
'''
def addCatItem(name):
global category_item_id
category_item = dict()
category_item['supercategory'] = 'none'
category_item_id += 1
category_item['id'] = category_item_id
category_item['name'] = name
coco['categories'].append(category_item)
category_set[name] = category_item_id
return category_item_id
'''
def addCatItem(name):
'''
增加json格式中的categories部分
'''
global category_item_id
category_item = collections.OrderedDict()
category_item['supercategory'] = 'none'
category_item['id'] = category_item_id
category_item['name'] = name
coco['categories'].append(category_item)
category_item_id += 1
def addImgItem(file_name, size):
global image_id
if file_name is None:
raise Exception('Could not find filename tag in xml file.')
if size['width'] is None:
raise Exception('Could not find width tag in xml file.')
if size['height'] is None:
raise Exception('Could not find height tag in xml file.')
#image_item = dict() #按照一定的顺序,这里采用collections.OrderedDict()
image_item = collections.OrderedDict()
jpg_name = os.path.splitext(file_name)[0]+'.png'
image_item['file_name'] = jpg_name
image_item['width'] = size['width']
image_item['height'] = size['height']
image_item['id'] = image_id
coco['images'].append(image_item)
image_set.add(jpg_name)
image_id = image_id+1
return image_id
def addAnnoItem(object_name, image_id, category_id, bbox):
global annotation_id
#annotation_item = dict()
annotation_item = collections.OrderedDict()
annotation_item['segmentation'] = []
seg = []
# bbox[] is x,y,w,h
# left_top
seg.append(bbox[0])
seg.append(bbox[1])
# left_bottom
seg.append(bbox[0])
seg.append(bbox[1] + bbox[3])
# right_bottom
seg.append(bbox[0] + bbox[2])
seg.append(bbox[1] + bbox[3])
# right_top
seg.append(bbox[0] + bbox[2])
seg.append(bbox[1])
annotation_item['segmentation'].append(seg)
annotation_item['area'] = bbox[2] * bbox[3]
annotation_item['iscrowd'] = 0
annotation_item['image_id'] = image_id
annotation_item['bbox'] = bbox
annotation_item['category_id'] = category_id
annotation_item['id'] = annotation_id
annotation_item['ignore'] = 0
annotation_id += 1
coco['annotations'].append(annotation_item)
def parseXmlFiles(xml_path):
xmllist = os.listdir(xml_path)
xmllist.sort()
for f in xmllist:
if not f.endswith('.xml'):
continue
bndbox = dict()
size = dict()
current_image_id = None
current_category_id = None
file_name = None
size['width'] = None
size['height'] = None
size['depth'] = None
xml_file = os.path.join(xml_path, f)
print(xml_file)
tree = ET.parse(xml_file)
root = tree.getroot() #抓根结点元素
if root.tag != 'annotation': #根节点标签
raise Exception('pascal voc xml root element should be annotation, rather than {}'.format(root.tag))
# elem is <folder>, <filename>, <size>, <object>
for elem in root:
current_parent = elem.tag
current_sub = None
object_name = None
#elem.tag, elem.attrib,elem.text
if elem.tag == 'folder':
continue
if elem.tag == 'filename':
file_name = elem.text
if file_name in category_set:
raise Exception('file_name duplicated')
# add img item only after parse <size> tag
elif current_image_id is None and file_name is not None and size['width'] is not None:
if file_name not in image_set:
current_image_id = addImgItem(file_name, size)#图片信息
print('add image with {} and {}'.format(file_name, size))
else:
raise Exception('duplicated image: {}'.format(file_name))
# subelem is <width>, <height>, <depth>, <name>, <bndbox>
for subelem in elem:
bndbox['xmin'] = None
bndbox['xmax'] = None
bndbox['ymin'] = None
bndbox['ymax'] = None
current_sub = subelem.tag
if current_parent == 'object' and subelem.tag == 'name':
object_name = subelem.text
#if object_name not in category_set:
# current_category_id = addCatItem(object_name)
#else:
#current_category_id = category_set[object_name]
current_category_id = category_set.index(object_name)+1 #index默认从0开始,但是json文件是从1开始,所以+1
elif current_parent == 'size':
if size[subelem.tag] is not None:
raise Exception('xml structure broken at size tag.')
size[subelem.tag] = int(subelem.text)
# option is <xmin>, <ymin>, <xmax>, <ymax>, when subelem is <bndbox>
for option in subelem:
if current_sub == 'bndbox':
if bndbox[option.tag] is not None:
raise Exception('xml structure corrupted at bndbox tag.')
bndbox[option.tag] = int(option.text)
# only after parse the <object> tag
if bndbox['xmin'] is not None:
if object_name is None:
raise Exception('xml structure broken at bndbox tag')
if current_image_id is None:
raise Exception('xml structure broken at bndbox tag')
if current_category_id is None:
raise Exception('xml structure broken at bndbox tag')
bbox = []
# x
bbox.append(bndbox['xmin'])
# y
bbox.append(bndbox['ymin'])
# w
bbox.append(bndbox['xmax'] - bndbox['xmin'])
# h
bbox.append(bndbox['ymax'] - bndbox['ymin'])
print(
'add annotation with {},{},{},{}'.format(object_name, current_image_id-1, current_category_id, bbox))
addAnnoItem(object_name, current_image_id-1, current_category_id, bbox)
#categories部分
for categoryname in category_set:
addCatItem(categoryname)
if __name__ == '__main__':
xml_path = '/home/yantianwang/clone/VOC2007/val_Annotations'
json_file = '/home/yantianwang/clone/VOC2007/Annotations/voc_2007_val.json'
parseXmlFiles(xml_path)
json.dump(coco, open(json_file, 'w'))
唯一需要修改的的地方就是category_set=['face', ]
2. 下载pkl预训练文件
我需要ResNeXt-101-64x4d,从预训练参数开始
我下的是X-101-64x4d.pkl:
- 设置yaml,这是参数
从docker中的config文件中cp一份yaml文件出来。。
如下:
我使用e2e_faster_rcnn_X-101-64x4d-FPN_2x.yaml。。。
如下e2e_faster_rcnn_X-101-64x4d-FPN_2x.yaml:
MODEL:
TYPE: generalized_rcnn
CONV_BODY: FPN.add_fpn_ResNet101_conv5_body
NUM_CLASSES: 2
FASTER_RCNN: True
NUM_GPUS: 1
SOLVER:
WEIGHT_DECAY: 0.0001
LR_POLICY: steps_with_decay
# 2x schedule (note TRAIN.IMS_PER_BATCH: 1)
BASE_LR: 0.001
GAMMA: 0.1
MAX_ITER: 360000
STEPS: [0, 240000, 320000]
FPN:
FPN_ON: True
MULTILEVEL_ROIS: True
MULTILEVEL_RPN: True
RPN_ANCHOR_START_SIZE: 8
RESNETS:
STRIDE_1X1: False # default True for MSRA; False for C2 or Torch models
TRANS_FUNC: bottleneck_transformation
NUM_GROUPS: 64
WIDTH_PER_GROUP: 4
FAST_RCNN:
ROI_BOX_HEAD: fast_rcnn_heads.add_roi_2mlp_head
ROI_XFORM_METHOD: RoIAlign
ROI_XFORM_RESOLUTION: 7
ROI_XFORM_SAMPLING_RATIO: 2
TRAIN:
# md5sum of weights pkl file: aa14062280226e48f569ef1c7212e7c7
WEIGHTS: /detectron/work/data/pkl/X-101-64x4d.pkl
DATASETS: ('train_data', )
SCALES: (550,650,750,850,950)
MAX_SIZE: 1600
IMS_PER_BATCH: 1
BATCH_SIZE_PER_IM: 512
RPN_PRE_NMS_TOP_N: 2000 # Per FPN level
TEST:
DATASETS: ('val_data',)
SCALE: 750
MAX_SIZE: 1600
NMS: 0.5
RPN_PRE_NMS_TOP_N: 1000 # Per FPN level
RPN_POST_NMS_TOP_N: 1000
DETECTIONS_PER_IM: 5000
BBOX_AUG:
ENABLED: True
SCORE_HEUR: UNION
COORD_HEUR: UNION
H_FLIP: True
SCALES: (550, 650,750, 850,950)
MAX_SIZE: 1600
SCALE_H_FLIP: True
SCALE_SIZE_DEP: False
AREA_TH_LO: 64 # 8^2
AREA_TH_HI: 32400 # 180^2
BBOX_VOTE:
ENABLED: True
VOTE_TH: 0.8
SOFT_NMS:
ENABLED: True
OUTPUT_DIR: .
- 修改文件
训练之前还需要修改data_cotalog.py
在docker中有一个文件需要修改,但是我先在本地修改再推上去。。。
就是detectron/datasets/data_cotalog.py
在文件最后加上train_data和test_data就是对应yaml文件的数据地址keywords。
'voc_2012_train': {
_IM_DIR:
_DATA_DIR + '/VOC2012/JPEGImages',
_ANN_FN:
_DATA_DIR + '/VOC2012/annotations/voc_2012_train.json',
_DEVKIT_DIR:
_DATA_DIR + '/VOC2012/VOCdevkit2012'
},
'voc_2012_val': {
_IM_DIR:
_DATA_DIR + '/VOC2012/JPEGImages',
_ANN_FN:
_DATA_DIR + '/VOC2012/annotations/voc_2012_val.json',
_DEVKIT_DIR:
_DATA_DIR + '/VOC2012/VOCdevkit2012'
},
'train_data': {
_IM_DIR:
_DATA_DIR + '/train_data/JPEGImages',
_ANN_FN:
_DATA_DIR + '/train_data/my_train.json',
},
'val_data': {
_IM_DIR:
_DATA_DIR + '/test_data/JPEGImages',
_ANN_FN:
_DATA_DIR + '/test_data/my_val.json',
}
5. 推送数据
把本地数据推到docker中去
在打开nvidia-docker,把本地文件与docker进行映射。
如下:
sudo nvidia-docker run -it -v $PWD:/detectron/work housebw/detectron:latest /bin/bash
这时我的当前目录的文件在,detectron/work 中。
推送数据
cp -r work/val_data detectron/datasets/data
cp -r work/train_data detectron/datasets/data
我的本地数据test_data和train_data已经到data中了。
6 训练:
注意我的预训练数据就在work/data/pkl。因为我已经进行映射了
执行如下进行训练
python tools/train_net.py --cfg work/data/our_e2e_faster_rcnn_X-101-64x4d-FPN_2x.yaml OUTPUT_DIR work/data/output
训练过程信息
7 推断
修改dummp_datasets.py和infer_simple.py
在Detectron的lib/datasets/dummy_datasets.py文件中仿照get_coco_dataset()函数添加一个get_face_dataset()函数
def get_VOC_dataset():
"""A dummy VOC dataset"""
ds = AttrDict()
classes = ['__background__', 'face']
ds.classes = {i:name for i, name in enumerate(classes)}
return ds
然后在infer_simple.py文件中将main()函数里面的
dummy_coco_dataset = dummy_datasets.get_coco_dataset()
改成dummy_coco_dataset = dummy_datasets.get_voc_dataset()
python tools/test_net.py --cfg work/data/our_e2e_faster_rcnn_X-101-64x4d-FPN_2x.yaml TEST.WEIGHTS work/data/output/train/train_data/generalized_rcnn/model_final.pkl
work/testdata NUM_GPUS 1
8 评估
python2 tools/test_net.py \
--cfg work/data/our_e2e_faster_rcnn_X-101-64x4d-FPN_2x.yaml \
TEST.WEIGHTS /work/data/output/train/train_data/generalizer_rnn/model_final.pkl \
NUM_GPUS 1
多GPU运行
看这里
https://github.com/facebookresearch/Detectron/issues/551
未完待续....................
参考:
将voc格式的数据转为json格式用于detectron的训练
使用自己的数据集(voc2007格式)训练Detectron
Detectron研读和实践三
Detectron系列文章