YOLOV5学习笔记(七)——训练自己数据集

目录

一、数据集介绍

二、数据集转化

2.1 xml转txt

2.2 制作VOC数据集

三、yaml文件修改

3.1 数据集yaml

3.2 模型yaml

四、训练评估

4.1  训练

4.2 评估


一、数据集介绍

根据YOLOV5学习笔记六所设计的轻量化小目标检测网络,本节将用tibnet制作的数据集进行训练测试,该数据集是用来检测空中无人机的,可以看到无人机十分的小。该数据集的labels文件是用labelme软件进行标注的xml形式。


	0829_5JPEGImages
	0829_5092.jpg
	C:\Users\lsq\Desktop\图片\0829_5JPEGImages\0829_5092.jpg
	
		Unknown
	
	
		960
		540
		3
	
	0
	
		uav
		Unspecified
		0
		0
		
			477
			259
			499
			279
		
	

二、数据集转化

2.1 xml转txt

xml文件的标注格式是一个框的四个点的x,y范围,而yolov5使用的格式是框的中心点加上宽高,所以需要进行格式的转化,将xml文件转化为txt文件,代码如下。

import xml.etree.ElementTree as ET
 
import pickle
import os
from os import listdir , getcwd
from os.path import join
import glob
 
classes = ["uav"]
 
def convert(size, box):
 
    dw = 1.0/size[0]
    dh = 1.0/size[1]
    x = (box[0]+box[1])/2.0
    y = (box[2]+box[3])/2.0
    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_name):
    in_file = open('./Annotations/'+image_name[:-3]+'xml') #xml文件路径
    out_file = open('./labels/'+image_name[:-3]+'txt', 'w') #转换后的txt文件存放路径
    f = open('./Annotations/'+image_name[:-3]+'xml')
    xml_text = f.read()
    root = ET.fromstring(xml_text)
    f.close()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)
    for obj in root.iter('object'):
        cls = obj.find('name').text
        if cls not in classes:
            print(cls)
            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()
 
if __name__ == '__main__':
 
    for image_path in glob.glob("./JPEGImages/*.jpg"): #每一张图片都对应一个xml文件这里写xml对应的图片的路径
        image_name = image_path.split('/')[-1]
        convert_annotation(image_name)

转化后的格式如下,第一个0代表类别,之后是框的中心点坐标和宽高

转化完后一定要检查一下txt中是否有值,不知道什么原因,有时会转化为空值

0 0.47890625 0.3597222222222222 0.0296875 0.05277777777777778

2.2 制作VOC数据集

YOLOV5学习笔记(七)——训练自己数据集_第1张图片

 选取三分之二的数据作为train,剩下的三分之一作为val,数据集的目录如上图

三、yaml文件修改

3.1 数据集yaml

# YOLOv5  by Ultralytics, GPL-3.0 license
# PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC by University of Oxford
# Example usage: python train.py --data VOC.yaml
# parent
# ├── yolov5
# └── datasets
#     └── VOC  ← downloads here


# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
train: /home/cxl/ros_yolov5/src/yolov5/data/VOCdevkit/images/train/
val: /home/cxl/ros_yolov5/src/yolov5/data/VOCdevkit/images/val/

# Classes
nc: 1  # number of classes
names: ['uav']  # class names

3.2 模型yaml

 主要修改类别,因为就无人机一类,所以nc改为1

# Parameters
nc: 1  # number of classes
depth_multiple: 1.0  # model depth multiple
width_multiple: 1.0  # layer channel multiple
anchors:

  - [2,2, 6,8, 10,14]  #4
  - [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32

四、训练评估

4.1  训练

python train.py --data data/VOC.yaml --cfg models/yolov5s-tiny.yaml --weights weights/yolov5stiny.pt --batch-size 16 --epochs 100

YOLOV5学习笔记(七)——训练自己数据集_第2张图片

 查看训练过程

tensorboard --logdir=./runs

4.2 评估

YOLOV5学习笔记(七)——训练自己数据集_第3张图片

 可以看到效果不错,map0.5达到了0.94,loss接近于0

 将训练好的权重保存为yolov5suav.pt,随后进行测试

 测试

python detect.py --source ./data/images/ --weights weights/yolov5suav.pt --conf 0.4
detect: weights=['weights/yolov5suav.pt

YOLOV5学习笔记(七)——训练自己数据集_第4张图片

 PS : yolov5  txt转txt文件

1、数据集划分

# coding:utf-8

import os
import random
import argparse

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

trainval_percent = 0.7
train_percent = 1
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 + '/val.txt', 'w')
file_train = open(txtsavepath + '/train.txt', 'w')
file_val = open(txtsavepath + '/test.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()

2、生成数据集txt列表文件

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

sets = ['train', 'test', 'val']
abs_path = os.getcwd()
print(abs_path)



wd = getcwd()
for image_set in sets:
    image_ids = open('./ImageSets/Main/%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 + '/images/%s.jpg\n' % (image_id))
        #convert_annotation(image_id)
    list_file.close()

val.txt    test.txt   train.txt    images   labels 在同一目录下

3、改yaml文件

# YOLOv5  by Ultralytics, GPL-3.0 license
# PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC by University of Oxford
# Example usage: python train.py --data VOC.yaml
# parent
# ├── yolov5
# └── datasets
#     └── VOC  ← downloads here


# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
train: data/train.txt
val: data/val.txt
test: data/test.txt
# Classes
nc: 7  # number of classes
names: ['pedestrian','cyclist','car','bus','truck','traffic_light','traffic_sign']  # class names

你可能感兴趣的:(计算机视觉,学习,目标检测,深度学习)