yolov4实战训练数据

1、克隆项目文件

项目Github地址:https://github.com/AlexeyAB/darknet
打开终端,克隆项目

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

无法克隆的话,把https修改为git

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

修改Makefile文件

GPU=1
CUDNN=1
OPENCV=1

编译项目

cd darknet
make 或者 make -j8(加速编译)

2、下载训练的权重文件

链接:(tiny) YOLOv4 详细训练指南(附下载链接)

3、准备数据集

可以用labelimg标注数据,然后图片放image、标注文件放xml

4、修改.data、.name、.cfg文件

根据自己训练数据的类别修改.data、.name文件
.cfg文件注意修改classes=你的类别数,并修改classes往上找的第一个filters=(classes+5)x 3

5、生成训练集和验证集(split_train_val.py)

# coding:utf-8
import os
import random
import argparse

parser = argparse.ArgumentParser()
#xml文件的地址,根据自己的数据进行修改 xml一般存放在Annotations下
parser.add_argument('--xml_path', default='Fincir\\xml', type=str, help='input xml label path')
#数据集的划分,地址选择自己数据下的ImageSets/Main,没有的话新建一个
parser.add_argument('--txt_path', default='jht_data\\main', type=str, help='output txt label path')
opt = parser.parse_args()

trainval_percent = 1.0
train_percent = 0.9
xmlfilepath = opt.xml_path
txtsavepath = opt.txt_path
total_xml = os.listdir(xmlfilepath)
if not os.path.exists(txtsavepath):
    os.makedirs(txtsavepath)

num = len(total_xml)
list_index = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list_index, tv)
train = random.sample(trainval, tr)

file_trainval = open(txtsavepath + '/trainval.txt', 'w')
file_test = open(txtsavepath + '/test.txt', 'w')
file_train = open(txtsavepath + '/train.txt', 'w')
file_val = open(txtsavepath + '/val.txt', 'w')

for i in list_index:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:
        file_trainval.write(name)
        if i in train:
            file_train.write(name)
        else:
            file_val.write(name)
    else:
        file_test.write(name)

file_trainval.close()
file_train.close()
file_val.close()
file_test.close()

6、xml转txt文件,生成图片、txt文件路径(voc_label.py)

# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os
from os import getcwd

sets = ['train', 'val', 'test']
classes = ["0", "1", "2"]   # 改成自己的类别
# classes = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9","A","B","C","D","E","F","G","H","I","J","K","L","M","N",
#            "O","P","Q","R","S","T","U","V","W","X","Y","Z","-","/"]   # 改成自己的类别
abs_path = os.getcwd()
print(abs_path)

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('Fincir/xml/%s.xml' % (image_id), encoding='UTF-8')
    out_file = open('Fincir/image/%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
        #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))
        b1, b2, b3, b4 = b
        # 标注越界修正
        if b2 > w:
            b2 = w
        if b4 > h:
            b4 = h
        b = (b1, b2, b3, b4)
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

wd = getcwd()
for image_set in sets:
    image_ids = open('jht_data\\xml\\%s.txt' % (image_set)).read().strip().split()
    list_file = open('%s.txt' % (image_set), 'w')
    for image_id in image_ids:
        list_file.write(abs_path + '\\Fincir\\image\\%s.bmp\n' % (image_id))
        convert_annotation(image_id)
    list_file.close()

7、训练

//linux
./darknet detector train build/darknet/x64/cir_data/voc_cir.data 
build/darknet/x64/cir_data/yolov4-tiny.cfg 
build/darknet/x64/cir_data/yolov4-tiny.conv.29 -gpus -map
./darknet detector train train_data/voc.data train_data/yolov4.cfg train_data/yolov4.conv.137 -gpus -map 

./darknet detector train train_data/voc.data train_data/yolov4-tiny.cfg train_data/yolov4-tiny.conv.29 -gpus -map

8、验证权重文件

./darknet detector map train_data/voc.data train_data/yolov4-tiny.cfg backup/yolov4-tiny_best.weights

你可能感兴趣的:(python,开发语言)