Yolo训练自己的数据集完整记

《Yolo训练自己的数据集完整记》

0. 前言

虽然网上教程一大把,但是作为小白,训练自己的数据集还是费了点事。记录下一些关键点,少踩一点坑。
本文假设已经拥有以下条件:

1. Linux操作系统,已经安装好CUDA和cuDNN(参考教程:https://www.jianshu.com/p/bc614f23d9eb)
2. 耐心

1. 安装Darknet

Darknet是Yolo作者自己写的一个网络框架,官方网站是:https://pjreddie.com/
这部分跟着官方教程走就可以了,主要是以下步骤:

1)下载源码

git clone https://github.com/pjreddie/darknet
cd darknet

2)修改Makefile文件,启用GPU,cuDNN,OpenCV等相关支持

xed ./Makefile
# 修改
GPU=0
CUDNN=0
OPENCV=0
# 为
GPU=1
CUDNN=1
OPENCV=1

3)编译

make

最终得到可执行文件“darknet

2. 准备数据集

整个过程难就难在数据集的准备(先无脑训练,除开网络调优)。
先看看训练的命令需要准备哪些东西

./darknet detector train prcv.data prcv-tiny.cfg

其中“./darknet detector train”为训练的固定命令,最重要的是prcv.dataprcv-tiny.cfg两个参数。这两个文件,prcv.data即与数据集相关,prcv-tiny.cfg与网络结构相关。
看看prcv.data文件都有些啥内容

classes = 1
train  = /home/user/Prcv2018/Dataset/mission/detection/train/train_set.txt
valid  = /home/user/Prcv2018/Dataset/mission/detection/train/test_set.txt
names = /home/user/Prcv2018/Dataset/mission/detection/train/prcv.names
backup = /home/user/Prcv2018/Train/backup
eval = prcv

可以看出,里面是与数据集相关的配置参数,classes是目标类别数,train、valid分别是训练集和测试集txt文件所在路径,names为目标内别名称。backup为训练中间权重文件保存目录。很明显,这个文件里指出的三个文件与数据集相关,是需要准备的。train_set.txt里是训练集图片路径,test_set.txt里是测试集图片路径。

本文描述的数据集是PRCV2018行人计数挑战赛的数据集,鉴于保密协议,不公开放出。该数据集的特点是方框标注出头和肩的位置。目标分类数量只有一类:person。

因此,这三个文件的内容分别是:

# prcv.names
person

# train_set.txt
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000158_R_2018_05_17_18_40_00_left_027091.jpg
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000158_R_2018_05_17_11_37_26_left_008230.jpg
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000158_R_2018_05_17_11_37_26_left_017198.jpg
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000158_R_2018_05_17_14_00_00_left_023063.jpg
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000051_R_2018_05_17_14_00_00_left_030518.jpg
... ...

# test_set.txt
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000051_R_2018_05_17_14_00_00_left_031430.jpg
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000158_R_2018_05_17_14_00_00_left_019522.jpg
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000051_R_2018_05_17_11_43_47_left_000923.jpg
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000052_R_2018_05_17_18_40_01_left_010092.jpg
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000051_R_2018_05_17_16_00_00_left_028425.jpg
... ...

以上描述了训练集和测试集,以及类别名称。对于训练来讲,还差什么?
对了,就是标注文件。darknet识别的标注文件是txt格式。它位于图片的上级目录labels目录下。

# 比如train_set.txt里是
/home/user/Prcv2018/Dataset/mission/detection/train/images/202018000051_R_2018_05_17_14_00_00_left_031430.jpg
# 那么对应的标注文件 
202018000051_R_2018_05_17_14_00_00_left_031430.txt
# 应该在
/home/user/Prcv2018/Dataset/mission/detection/train/lables
# 目录下

标注文件中标签的数据格式:

<object-class> <x> <y> <width> <height>
# Where x, y, width, and height are relative to the image's width and height. 

以上是官方说明,说的很简单,实际意思是:

1. x,y是中心点的坐标
2. 坐标点是相对于整张图片的相对比例
3. 宽高也是相对于整张图片的相对比例
4. 类别编号是从0开始的
# eg.
# 图片大小是320*240,目标框中心位置是(160,120),框的宽高是[32,24],类别编号是0
# 那么,对应的txt文件内容应该是
# 0 0.5 0.5 0.1 0.1

源数据集的格式是左上角坐标x,y,框的宽高w,h(像素)。生成训练集和测试集以及标签格式转换py文件如下(第一次写python,实现得可能不太优雅):

import os

img_with = 320      # 图片宽
img_height = 240    # 图片高

rootdir = '/home/user/Prcv2018/Dataset/mission/detection/train'


def f2s(num):
    return str(round(num, 6))


def convert_data(path, tgt_path):
    out_file = open(tgt_path, 'w')
    with open(path, 'r') as txt_file:
        while True:
            line = txt_file.readline()
            if not line:
                break
            org_x, org_y, org_w, org_h = [int(i) for i in line.split()]
            tgt_x = (org_x + org_w / 2.0) / img_with
            tgt_y = (org_y + org_h / 2.0) / img_height
            tgt_w = org_w / img_with
            tgt_h = org_h / img_height
            # 单分类,序列号只有0
            tgt_line = '0 ' + f2s(tgt_x) + ' ' + f2s(tgt_y) + ' ' + f2s(tgt_w) + ' ' + f2s(tgt_h) + '\n'
            out_file.writelines(tgt_line)
    out_file.close()


def main():
    img_dir = rootdir + '/images'
    ann_dir = rootdir + '/annotations'
    lab_dir = rootdir + '/labels'
    # 1. 分出训练集和测试集
    train_txt = open(rootdir + '/train_set.txt', 'w')
    test_txt = open(rootdir + '/test_set.txt', 'w')
    img_list = os.listdir(img_dir)
    for i in range(0, len(img_list)):
        path = os.path.join(img_dir, img_list[i])
        if i % 5 == 0:
            test_txt.writelines(path + '\n')
        else:
            train_txt.writelines(path + '\n')
    train_txt.close()
    test_txt.close()
    # 2. 处理标签数据
    target_dir = lab_dir
    # 得到目录下列表名(相当于ls命令)
    txt_list = os.listdir(ann_dir)
    for i in range(0, len(txt_list)):
        path = os.path.join(ann_dir, txt_list[i])
        tgt_path = os.path.join(target_dir, txt_list[i])
        if os.path.isfile(path):
            # 处理文件
            convert_data(path, tgt_path)


if __name__ == '__main__':
    main()

其中测试集数据量占20%。

3. 训练

前面提到的另一个重要文件prcv-tiny.cfg。先放出它的具体内容:

[net]
batch=128
subdivisions=1
width=416
height=416
channels=3
momentum=0.9
decay=0.0005
angle=0
saturation = 1.5
exposure = 1.5
hue=.1

learning_rate=0.001
max_batches = 40200
policy=steps
steps=-1,100,20000,30000
scales=.1,10,.1,.1

[convolutional]
batch_normalize=1
filters=32
size=7
stride=3
pad=1
activation=leaky

... ...

[convolutional]
batch_normalize=1
size=1
stride=1
pad=1
filters=128
activation=leaky

[convolutional]
size=1
stride=1
pad=1
filters=30           <-------------------------------------------
activation=linear

[region]
anchors = 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52
bias_match=1
classes=1
coords=4
num=5
softmax=1
jitter=.2
rescore=1

object_scale=5
noobject_scale=1
class_scale=1
coord_scale=1

absolute=1
thresh = .5
random=1

这个文件里面包含了一些参数和网络结构,每项具体的释义网上一大把,就不过多解释了。唯一需要注意的是最后一层filters数量的计算,

filters = num×(classes + coords + 1)= 5*(1+4+1) = 30

当然,width=416,height=416中的416为32的13倍。
训练目录如下

user@Phynion:~/Prcv2018/Train$ ls
backup  darknet  data  prcv.data  prcv-tiny.cfg  test.jpg

训练目录位置随意,注意prcv.data、prcv-tiny.cfg里的路径即可。data\labels目录下的图片用于显示时的框和标签。
执行

./darknet detector train prcv.data prcv-tiny.cfg

开始训练

《Yolo训练自己的数据集完整记》

可以看到,对整个CPU、GPU、内存的要求都还是蛮高的。

4. 测试

训练结束后,权重文件保存在backup文件夹内,名称为prcv-tiny_final.weights。
打开prcv-tiny.cfg文件,#备注掉

#batch=128
#subdivisions=1

运行

./darknet detector test prcv.data prcv-tiny.cfg backup/prcv-tiny_final.weights test.jpg

可以看到结果

《Yolo训练自己的数据集完整记》

嗯,还不错的样子…

— over —

    原文作者:Venyeh_Hoo
    原文地址: https://www.jianshu.com/p/8a16be38e099
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞