使用自己的VOC数据集配置训练yolov5网络

使用自己的VOC数据集配置训练yolov5网络

1.下载yolov5网络模型

YOLOv5代码

2.制作自己的数据集

数据集制作步骤

3.将voc数据及转化为YOLO数据集

注意 :路径中不要含有中文,数据集图片的名称也不要含有中文!(如果一定要含有的话,需要在代码的os操作中加入encoding='utf-8' 例如:
open(save_dir / 'hyp.yaml', 'w')->open(save_dir / 'hyp.yaml', 'w',encoding='utf-8')`

建立文件夹: VOCdevkit
次级目录:VOC2007
次次级目录:Annotations (包含的是标注后生成的xml文件)
JPEGImages(包含的是数据集图片)
在VOCdevkit同级目录下 建立文件voc_to_yolo.py

类别:需要修改 classes
测试集训练集比例:可以修改 if(probo < 80) 【训练集占80/100】

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import random

classes=["class1","class2","class3"]   #此处要改成自己的标签类别名称


def clear_hidden_files(path):
    dir_list = os.listdir(path)
    for i in dir_list:
        abspath = os.path.join(os.path.abspath(path), i)
        if os.path.isfile(abspath):
            if i.startswith("._"):
                os.remove(abspath)
        else:
            clear_hidden_files(abspath)

def convert(size, box):
    dw = 1./size[0]
    dh = 1./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_id):
    in_file = open('VOCdevkit/VOC2007/Annotations/%s.xml' %image_id,encoding='utf-8')
    out_file = open('VOCdevkit/VOC2007/labels/%s.txt' %image_id, 'w',encoding='utf-8')
    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))
        #print("image_id = %s\n" %image_id)
        bb = convert((w,h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
    in_file.close()
    out_file.close()

wd = os.getcwd()
wd = os.getcwd()
work_sapce_dir = os.path.join(wd, "VOCdevkit/")
if not os.path.isdir(work_sapce_dir):
    os.mkdir(work_sapce_dir)
work_sapce_dir = os.path.join(work_sapce_dir, "VOC2007/")
if not os.path.isdir(work_sapce_dir):
    os.mkdir(work_sapce_dir)
annotation_dir = os.path.join(work_sapce_dir, "Annotations/")
if not os.path.isdir(annotation_dir):
        os.mkdir(annotation_dir)
clear_hidden_files(annotation_dir)
image_dir = os.path.join(work_sapce_dir, "JPEGImages/")
if not os.path.isdir(image_dir):
        os.mkdir(image_dir)
clear_hidden_files(image_dir)
VOC_file_dir = os.path.join(work_sapce_dir, "ImageSets/")
if not os.path.isdir(VOC_file_dir):
        os.mkdir(VOC_file_dir)
VOC_file_dir = os.path.join(VOC_file_dir, "Main/")
if not os.path.isdir(VOC_file_dir):
        os.mkdir(VOC_file_dir)

train_file = open(os.path.join(wd, "2007_train.txt"), 'w',encoding='utf-8')
test_file = open(os.path.join(wd, "2007_test.txt"), 'w',encoding='utf-8')
train_file.close()
test_file.close()
VOC_train_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/train.txt"), 'w',encoding='utf-8')
VOC_test_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/test.txt"), 'w',encoding='utf-8')
VOC_train_file.close()
VOC_test_file.close()
if not os.path.exists('VOCdevkit/VOC2007/labels'):
    os.makedirs('VOCdevkit/VOC2007/labels')
train_file = open(os.path.join(wd, "2007_train.txt"), 'a',encoding='utf-8')
test_file = open(os.path.join(wd, "2007_test.txt"), 'a',encoding='utf-8')
VOC_train_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/train.txt"), 'a',encoding='utf-8')
VOC_test_file = open(os.path.join(work_sapce_dir, "ImageSets/Main/test.txt"), 'a',encoding='utf-8')
list = os.listdir(image_dir) # list image files
probo = random.randint(1, 100)
print("Probobility: %d" % probo)
for i in range(0,len(list)):
    path = os.path.join(image_dir,list[i])
    if os.path.isfile(path):
        image_path = image_dir + list[i]
        voc_path = list[i]
        (nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
        (voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))
        annotation_name = nameWithoutExtention + '.xml'
        annotation_path = os.path.join(annotation_dir, annotation_name)
    probo = random.randint(1, 100)
    print("Probobility: %d" % probo)
    if(probo < 80)://在这里修改训练集测试集比例
        if os.path.exists(annotation_path):
            train_file.write(image_path + '\n')
            VOC_train_file.write(voc_nameWithoutExtention + '\n')
            convert_annotation(nameWithoutExtention)
    else:
        if os.path.exists(annotation_path):
            test_file.write(image_path + '\n')
            VOC_test_file.write(voc_nameWithoutExtention + '\n')
            convert_annotation(nameWithoutExtention)
train_file.close()
test_file.close()
VOC_train_file.close()
VOC_test_file.close()

运行后生成 label 文件夹,将其中的.txt文件全部复制进JEPGImages中
使用自己的VOC数据集配置训练yolov5网络

4.下载与训练权重

下载地址:百度网盘:预训练权重 密码:dv17

5.修改文件参数

yolov5- model文件夹下使用自己的VOC数据集配置训练yolov5网络使用自己的VOC数据集配置训练yolov5网络
修改.yaml文件中的 nc:(自己的类别数量)
使用自己的VOC数据集配置训练yolov5网络
同理data文件夹下,建立一个mine.yaml文件

train: D:/2007_train.txt # 在第3步运行下生成的和VOCdevkit同级路径下的文件
val: D:/2007_test.txt  # 同上
 
# number of classes
nc: 3#分类数量
 
# class names
names: ["class1","class2","class3"]

6.运行

终端:

python train.py --data (coco.yaml文件所在的路径) --cfg (yolov5s.yaml所在的路径) --weights '(.pt权重文件所在路径,在yolov5的下级目录中)' --batch-size 64(数字大小根据自己的电脑性能设定)
上一篇:批量修改VOC格式xml中box的大小


下一篇:学习杂记(1)