YOLOV7训练本地VOC数据集(自己数据集同理)

目录

一、VOC数据集转化为yolo系列数据集处理格式

二、编写yaml数据集配置文件

三、权值文件下载

四、修改参数配置信息

四、修改数据集加载labels标签文件代码

 五、训练yolov7

六、将训练好的pt文件做成接口调用

一、VOC数据集转化为yolo系列数据集处理格式

前提:电脑上已经下载好VOC数据集或者自己的数据集

制作自己的数据集可以使用labeling标注软件获得标注好的xml文件,然后将其参照VOC数据集存放方式xml文件放在Annotations文件夹中,原图放在JPEGImages中

 有些VOC数据集的ImageSets/Main文件夹中可能已经存在数据集的划分文件了,就是main中的四个txt文件

(1)如果没有上述四个文件,需要先执行数据集划分代码进行数据集划分,这个划分数据集的文件可以放在VOC2007目录下,与Annotations等文件夹同级

"""
function: create train.txt and test.txt in ImageSets\Main
"""
import os
import random

trainval_percent = 0.8  # 可自行进行调节(这里设置训练:验证:测试的比例是6:2:2)
train_percent = 3/4
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'
total_xml = os.listdir(xmlfilepath)

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

ftrainval = open('ImageSets/Main/trainval.txt', 'w')
ftest = open('ImageSets/Main/test.txt', 'w')
ftrain = open('ImageSets/Main/train.txt', 'w')
fval = open('ImageSets/Main/val.txt', 'w')

for i in list:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:
        ftrainval.write(name)
        if i in train:
            ftrain.write(name)
        else:
            fval.write(name)
    else:
        ftest.write(name)

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

(2)如果已经有上述四个文件了,我们只需要执行标签生成程序 xml_voc_to_yolo.py,代码如下

import os
from os import listdir, getcwd
from os.path import join
import cv2

sets = ['train', 'val']
classes = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
        'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ]

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('JILI_NB\Annotations/%s.xml' % (image_id))  # 修改路径(最好使用全路径)
    # img_file = cv2.imread('JILI_NB\images\%s.jpg' % (image_id))
    # out_file = open('JILI_NB\labels/%s.txt' % (image_id), 'w+')  # 修改路径(最好使用全路径)
    in_file = open('VOCdevkit/VOC2007/Annotations/%s.xml' % (image_id))  # 修改路径(最好使用全路径)
    img_file = cv2.imread('VOCdevkit\VOC2007\JPEGImages\%s.jpg' % (image_id))
    out_file = open('VOCdevkit/VOC2007/labels
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值