Darknet YoloV4 Windows10下数据训练及测试(二)darknet训练自己的数据

环境:windows10+ cundn10.0+ cudnn7.6 vs2015编译 opencv3.4.6 显卡RTX2080 8G显存

据测试cundn10.0+ cudnn7.6和cudn10.0+ cudnn7.3都可以

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

2、darknetV4训练自己的数据

yolo4训练的方法与yolo3基本相同。

yolov4.conv.137为yoloV4的预训练模型:https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137

a)、darknet数据集制作

国内交通标志数据集:共包含1465张640*640的交通标志图片,一共标注了6类交通标志,分别是:直行、左转、右转、禁行、禁鸣、人行横道,("straight", "left","right", "stop ", "nohonk", "crosswalk")。标注信息均保存在xml文件下载地址:https://www.kesci.com/home/dataset/5ea92354366f4d002d730fca 下载解压后的数据集格式:一个是xml何对应的图片

写脚本xml2voc2007转换为voc2007格式

import os
import numpy as np
import codecs
import json
from glob import glob
import cv2
import shutil
from sklearn.model_selection import train_test_split

saved_path = "C:/Users/nyy/Desktop/traffic_datasets/voc/"  # 保存路径

创建要求文件夹
if not os.path.exists(saved_path + "Annotations"):
    os.makedirs(saved_path + "Annotations")
if not os.path.exists(saved_path + "JPEGImages/"):
    os.makedirs(saved_path + "JPEGImages/")
if not os.path.exists(saved_path + "ImageSets/Main/"):
    os.makedirs(saved_path + "ImageSets/Main/")


# 6.split files for txt
txtsavepath = saved_path + "ImageSets/Main/"
ftrainval = open(txtsavepath + '/trainval.txt', 'w')
ftest = open(txtsavepath + '/test.txt', 'w')
ftrain = open(txtsavepath + '/train.txt', 'w')
fval = open(txtsavepath + '/val.txt', 'w')
total_files = glob("C:/Users/nyy/Desktop/traffic_datasets/Annotations/*.xml")
total_files = [i.split("/")[-1].split(".xml")[0] for i in total_files]
print(total_files);
test_filepath = ""
for file in total_files:
    ftrainval.write(file + "\n")
# test
# for file in os.listdir(test_filepath):
#    ftest.write(file.split(".jpg")[0] + "\n")
# split
train_files, val_files = train_test_split(total_files, test_size=0.15, random_state=42)
# train
for file in train_files:
    ftrain.write(file + "\n")
# val
for file in val_files:
    fval.write(file + "\n")

ftrainval.close()
ftrain.close()
fval.close()
#ftest.close()

之后采用darknet自带的voc_label.py脚本转换为darknet需要的txt格式

需按照数据格式修改两个部分

sets=[('2007', 'train'), ('2007', 'val'), ('2007', 'test')]
classes = ["straight", "left","right", "stop ", "nohonk", "crosswalk"]

Darknet YoloV4 Windows10下数据训练及测试(二)darknet训练自己的数据_第1张图片

b)、训练数据

darknet.exe detector train data/vocTR.data yolov4_TR.cfg yolov4.conv.137 -map

vocTR.data :classes= 80
train  = data/voc/2007_train.txt
valid  = data/voc/2007_val.txt
#difficult = data/difficult_2007_test.txt
names = data/vocTR.names
backup = backupTR/

yolov4-TR.cfg:按照显存大小进行合理修改

batch=24

subdivisions=16

width=608

height=608

channels=3

momentum=0.949

decay=0.0005

angle=0

saturation = 1.5

exposure = 1.5

hue=.1

 

learning_rate=0.0001

burn_in=200

max_batches = 6000

policy=steps

steps=4800,5400

scales=.1,.1

filters=33

classes=6

按照官网要求把filters=255改为filters=(6 + 5)x3

change [filters=255] to filters=(classes + 5)x3;

在cmd进入

输入命令行:darknet.exe detector train data/vocTR.data yolov4_TR.cfg yolov4.conv.137 -map,

 

经过10h的训练

Darknet YoloV4 Windows10下数据训练及测试(二)darknet训练自己的数据_第2张图片

c)、测试

Darknet YoloV4 Windows10下数据训练及测试(二)darknet训练自己的数据_第3张图片

sign_1245.jpg

Darknet YoloV4 Windows10下数据训练及测试(二)darknet训练自己的数据_第4张图片

sign_1155.jpg

Darknet YoloV4 Windows10下数据训练及测试(二)darknet训练自己的数据_第5张图片

sign_1415.jpg

Darknet YoloV4 Windows10下数据训练及测试(二)darknet训练自己的数据_第6张图片

本训练的数据集只有直行、左转、右转、禁行、禁鸣、人行横道,("straight", "left","right", "stop ", "nohonk", "crosswalk")这六类;要识别其他的需要相应类型的数据集。

训练模型下载地址:

yolov4_TR.cfg:https://download.csdn.net/download/niuyuanye/12386318

vocTR.data:https://download.csdn.net/download/niuyuanye/12386326

classes= 6
train  = data/voc/2007_train.txt
valid  = data/voc/2007_val.txt
difficult = data/voc/2007_test.txt
names = data/vocTR.names

darknet.exe detector test data/vocTR.data yolov4-TR.cfg backupTR/yolov4-TR_best.weights -i 0 -thresh 0.25 sign_1245.jpg -ext_output

你可能感兴趣的:(YOLOV4,Darknet)