上篇中,已经写了如何在win10系统下安装GPU版本的YOLOv2了,本文讲演示如何训练自己的数据来实现人脸检测,其实官方文档上的教程已经写得很不错了,其实就分为几个步骤。
官方地址:https://github.com/AlexeyAB/darknet
一、模仿VOC的格式
建立文件夹VOCdevkit,在里面新建VOC2017文件夹,然后VOC2017里新建下图的4个文件夹,最后在ImageSets里新建名叫Main文件夹。其中,Annotations里面放每张图片的.xml文件。ImageSets的Main里放train.txt,该文本文件里面的内容是需要用来训练图像的名字,一行写一个(无后缀无路径)。JPEGImages文件夹中放规则命名好的原始训练图像。其他的先不用管。
二、标记图像
建议用labelImage,用法见http://blog.csdn.net/jesse_mx/article/details/53606897,会生成.xml文件。我在网上找了20张包含人脸的照片,用labelImg标注了20张图片中的人脸。
三、生成相关训练文件
修改作者提供的代码中的voc_label.py,修改sets和classes,我这里只训练一个类Face,在darknet/scripts下,运行voc_label.py,在darknet/scripts会得到 2017_train.txt文件,里面是所有训练图片的绝对路径,在darknet/scripts/VOCdevkit/VOC2017目录下生成名为labels的文件夹,里面一个文件对应一张图片的标签。代码如下:
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
# sets=[('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]
sets = [('2017', 'train')]
# classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
classes = ["Face"]
def convert(size, box):
dw = 1. / (size[0])
dh = 1. / (size[1])
x = (box[0] + box[1]) / 2.0 - 1
y = (box[2] + box[3]) / 2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_annotation(year, image_id):
in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml' % (year, image_id))
out_file = open('VOCdevkit/VOC%s/labels/%s.txt' % (year, image_id), 'w')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = 100
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()
for year, image_set in sets:
if not os.path.exists('VOCdevkit/VOC%s/labels/' % (year)):
os.makedirs('VOCdevkit/VOC%s/labels/' % (year))
image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt' % (year, image_set)).read().strip().split()
list_file = open('%s_%s.txt' % (year, image_set), 'w')
for image_id in image_ids:
list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n' % (wd, year, image_id))
convert_annotation(year, image_id)
list_file.close()
四、修改YOLOv2相关配置文件
(1)在\darknet-master\data 下新建 obj.data和obj.names 两个文件。
obj.data内容如下,classes=1,train等于上文2017_train.txt文件的绝对路径。obj.names里面只写一个Face
classes= 1
train = D:/yoloV2/darknet/scripts/2017_train.txt
names = data/obj.names
backup = D:/yoloV2/darknet/result/
(2)拷贝cfg文件夹下的yolo-voc.2.0.cfg,重命名为yolo-obj.cfg,修改里面的一些内容:
五、开始训练
编写.cmd文件,下载官网的预训练模型darknet.conv.weights做初始化,训练命令为如下(注意路径):
D:\yoloV2\darknet\build\darknet\x64\darknet.exe detector train ./data/obj.data yolo-obj.cfg darknet19_448.conv.23
六、测试
在result文件中保存了权重文件,如:yolo-obj_500.weights
编写测试命令:
D:\yoloV2\darknet\build\darknet\x64\darknet.exe detector test ./data/obj.data yolo-obj.cfg ./result/yolo-obj_500.weights -i 0 -thresh 0.1 ./data/Face/1.jpg
pause