最近看了看YOLO从v1到v3,想用widerface数据集训练yolov3去进行人脸检测。
首先需要做的就是将widerface数据集的格式转化为VOC格式。
由于编程能力不强,在网上看了看代码,发现 https://www.cnblogs.com/linyuanzhou/p/6043436.html
为了省事,便借用一下,但是由于他的代码估计是基于python2.7,实测python2.7可以在直接运行他的代码。
在我用python3.5运行时,会有版本不兼容的状况。为此我对代码进行了一些小的修改,放在这里让大家更加方便吧。
下图为目录结构:
#基于python2.7的代码(我不是原作者)
# -*- coding: utf-8 -*-
"""
Created on 17-5-27
@author: zly
"""
from skimage import io
import shutil
import random
import os
import string
headstr = """\
VOC2007
%06d.jpg
NULL
company
%d
%d
%d
0
"""
objstr = """\
"""
tailstr = '''\
'''
def all_path(filename):
return os.path.join('widerface', filename)
def writexml(idx, head, bbxes, tail):
filename = all_path("Annotations/%06d.xml" % (idx))
f = open(filename, "w")
f.write(head)
for bbx in bbxes:
f.write(objstr % ('face', bbx[0], bbx[1], bbx[0] + bbx[2], bbx[1] + bbx[3]))
f.write(tail)
f.close()
def clear_dir():
if shutil.os.path.exists(all_path('Annotations')):
shutil.rmtree(all_path('Annotations'))
if shutil.os.path.exists(all_path('ImageSets')):
shutil.rmtree(all_path('ImageSets'))
if shutil.os.path.exists(all_path('JPEGImages')):
shutil.rmtree(all_path('JPEGImages'))
shutil.os.mkdir(all_path('Annotations'))
shutil.os.makedirs(all_path('ImageSets/Main'))
shutil.os.mkdir(all_path('JPEGImages'))
def excute_datasets(idx, datatype):
f = open(all_path('ImageSets/Main/' + datatype + '.txt'), 'a')
f_bbx = open(all_path('wider_face_split/wider_face_' + datatype + '_bbx_gt.txt'), 'r')
while True:
filename = string.strip(f_bbx.readline(), '\n')
if not filename:
break
im = io.imread(all_path('WIDER_' + datatype + '/images/'+filename))
head = headstr % (idx, im.shape[1], im.shape[0], im.shape[2])
nums = string.strip(f_bbx.readline(), '\n')
bbxes = []
for ind in xrange(string.atoi(nums)):
bbx_info = string.split(string.strip(f_bbx.readline(), ' \n'), ' ')
bbx = [string.atoi(bbx_info[i]) for i in range(len(bbx_info))]
#x1, y1, w, h, blur, expression, illumination, invalid, occlusion, pose
if bbx[7]==0:
bbxes.append(bbx)
writexml(idx, head, bbxes, tailstr)
shutil.copyfile(all_path('WIDER_' + datatype + '/images/'+filename), all_path('JPEGImages/%06d.jpg' % (idx)))
f.write('%06d\n' % (idx))
idx +=1
f.close()
f_bbx.close()
return idx
# 打乱样本
def shuffle_file(filename):
f = open(filename, 'r+')
lines = f.readlines()
random.shuffle(lines)
f.seek(0)
f.truncate()
f.writelines(lines)
f.close()
if __name__ == '__main__':
clear_dir()
idx = 1
idx = excute_datasets(idx, 'train')
idx = excute_datasets(idx, 'val')
下面是我修改后的基于python3.5
# -*- coding: utf-8 -*-
"""
Created on 18-5-13
@author: CHR
"""
from skimage import io
import shutil
import random
import os
import string
headstr = """\
VOC2007
%06d.jpg
NULL
company
%d
%d
%d
0
"""
objstr = """\
"""
tailstr = '''\
'''
def all_path(filename):
return os.path.join('widerface', filename)
def writexml(idx, head, bbxes, tail):
filename = all_path("Annotations/%06d.xml" % (idx))
f = open(filename, "w")
f.write(head)
for bbx in bbxes:
f.write(objstr % ('face', bbx[0], bbx[1], bbx[0] + bbx[2], bbx[1] + bbx[3]))
f.write(tail)
f.close()
def clear_dir():
if shutil.os.path.exists(all_path('Annotations')):
shutil.rmtree(all_path('Annotations'))
if shutil.os.path.exists(all_path('ImageSets')):
shutil.rmtree(all_path('ImageSets'))
if shutil.os.path.exists(all_path('JPEGImages')):
shutil.rmtree(all_path('JPEGImages'))
shutil.os.mkdir(all_path('Annotations'))
shutil.os.makedirs(all_path('ImageSets/Main'))
shutil.os.mkdir(all_path('JPEGImages'))
def excute_datasets(idx, datatype):
f = open(all_path('ImageSets/Main/' + datatype + '.txt'), 'a')
f_bbx = open(all_path('wider_face_split/wider_face_' + datatype + '_bbx_gt.txt'), 'r')
while True:
filename = f_bbx.readline().strip('\n')
if not filename:
break
im = io.imread(all_path('WIDER_' + datatype + '/images/'+filename))
head = headstr % (idx, im.shape[1], im.shape[0], im.shape[2])
nums = f_bbx.readline().strip('\n')
bbxes = []
for ind in range(int(nums)):
bbx_info = f_bbx.readline().strip(' \n').split(' ')
bbx = [int(bbx_info[i]) for i in range(len(bbx_info))]
#x1, y1, w, h, blur, expression, illumination, invalid, occlusion, pose
if bbx[7]==0:
bbxes.append(bbx)
writexml(idx, head, bbxes, tailstr)
shutil.copyfile(all_path('WIDER_' + datatype + '/images/'+filename), all_path('JPEGImages/%06d.jpg' % (idx)))
f.write('%06d\n' % (idx))
idx +=1
f.close()
f_bbx.close()
return idx
# 打乱样本
def shuffle_file(filename):
f = open(filename, 'r+')
lines = f.readlines()
random.shuffle(lines)
f.seek(0)
f.truncate()
f.writelines(lines)
f.close()
if __name__ == '__main__':
clear_dir()
idx = 1
idx = excute_datasets(idx, 'train')
idx = excute_datasets(idx, 'val')
这里主要是记录我的学习过程,并分享,如有错误,望大家指出。