zoukankan      html  css  js  c++  java
  • darknet版本yolov4模型训练与测试

    目录

    • 代码下载

    • 更改配置及编译

    • 测试初始权重

    • 准备自己的数据集

    • 划分测试集和训练集

    • 将voc文件格式转为yolo文件格式(xml->txt)

    • 训练

    • 测试

    1 代码下载

    首先把代码下载下来,可以用下面命令下载,也可以直接点击上面代码链接下载。

     
    git clone https://github.com/AlexeyAB/darknet.git

    2 更改配置及编译

    如果需要使用GPU加速,那么得打开项目里面的makefile文件修改一些参数的值。修改完成之后在直接make。
    GPU=1
    CUDNN=1
    CUDNN_HALF=1
    OPENCV=1
    OPENMP=1
    LIBSO=1
    DEBUG=1

    编译:

    make 或者 make -j8

    3 测试初始权重

    ./darknet detect cfg/yolov4.cfg yolov4.weights data/dog.jpg
    初始权重yolov4.weights需要下载。

    4 准备自己的数据集

    (1) 准备JPEGImages文件夹,里面存放要训练和测试的图像集。

    (2)准备Annotations文件夹,里面存放标注好的voc格式的xml文件。

    5 划分测试集和训练集

    运行makeTxt.py脚本文件,在ImageSets文件夹里生成train.txt和test.txt文件,里面为去除后缀的图像名。

    import os
    import random
    
    train_percent = 0.8
    xmlfilepath = 'Annotations'
    txtsavepath = 'ImageSets'
    total_xml = os.listdir(xmlfilepath)
    
    num = len(total_xml)
    list = range(num)
    tr = int(num * train_percent)
    #trainval = random.sample(list, tv)
    train = random.sample(list, tr)
    
    if not os.path.exists('ImageSets/'):
        os.makedirs('ImageSets/')
    
    ftest = open('ImageSets/test.txt', 'w')
    ftrain = open('ImageSets/train.txt', 'w')
    
    for i in list:
        name = total_xml[i][:-4] + '
    '
        if i in train:
             ftrain.write(name)
        else:
            ftest.write(name)
    
    ftrain.close()
    ftest.close()

    6 将VOC文件格式转为yolo文件格式

    运行xml2txt.py文件,在当前目录生成tp_train.txt和tp_test.txt文件,里面存放训练和测试图像的绝对路径。且在labels文件夹下生成xml转txt格式的文件。

    import xml.etree.ElementTree as ET
    import pickle
    import os
    from os import listdir, getcwd
    from os.path import join
    sets=[ 'train', 'test']  #替换为自己的数据集
    classes = ["person", "car", "cyclist"]     #修改为自己的类别
    #classes = ["eye", "nose"]
    
    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(image_id):
        in_file = open('Annotations/%s.xml'%image_id)  #将数据集放于当前目录下
        out_file = open('labels/%s.txt'%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 = obj.find('difficult').text
            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]) + '
    ')
    
    
    wd = getcwd()#获取当前目录
    train_images_dir = os.path.join(wd, 'JPEGImages/')
    
    for image_set in sets:
        if not os.path.exists('labels/'):
            os.makedirs('labels/')
        image_ids = open('ImageSets/%s.txt'%image_set).read().strip().split()
        list_file = open('%s_%s.txt'%("tp", image_set), 'w') #reate new file store image path
        for image_id in image_ids:
            list_file.write(train_images_dir+'%s.jpg
    '%image_id)
            convert_annotation(image_id)
        list_file.close()

    7 训练

    7.1 修改配置文件yolov4.cfg

    复制cfg/yolov4-custom.cfg,并且重命名为yolo-obj.cfg,同时修改一下内容

    上图中修改width和height为416,修改最大batch迭代多少个数max_batches = 6000,修改steps多久学习率下降一次,一般设置为batch个数的80%和90%。 然后三个classes的地方和三个filters=255的地方要修改成自己的。

    7.2 编辑adas.data 和 adas.names文件

    (1)adas.data

    classes :训练集类别总数

    train:训练集路径

    valid:验证集路径

    names: adas.names 文件

    backup: 备份文件夹,训练后的权重信息或者断点保存信息在此文件夹下。

    (记得创建目录 backup)

     (2)adas.name

     根据自己要分类的名称编写,一行一个名字。

    7.3 编写训练sh脚本

    vim tp_train.sh

    注:yolov4.conv.137预训练权重文件需要下载

    执行 sh tp_train.sh 开始训练

    8 测试

    ./darknet detector test cfg/obj.data cfg/yolo-obj.cfg yolo-obj_xxxx.weights 
     
  • 相关阅读:
    Python自动化开发
    Python自动化开发
    Python自动化开发
    前端开发
    keepalived和zookeeper对比
    网页静态化—redis | freemarker
    redis学习笔记
    消息队列—ActiveMQ
    Zookeeper原理架构
    Zookeeper集群搭建
  • 原文地址:https://www.cnblogs.com/tianpeng-blog/p/14776481.html
Copyright © 2011-2022 走看看