yolov5训练数据集路径选择其一

在这里插入图片描述

data

```python
train: /dssg/home/zhineng_qt/test_2021_3_18_fu/dataset/voc2/train.txt  # train images (relative to 'path') 118287 images
val: /dssg/home/zhineng_qt/test_2021_3_18_fu/dataset/voc2/val.txt  # val images (relative to 'path') 5000 images
test: /dssg/home/zhineng_qt/test_2021_3_18_fu/dataset/voc2/test.txt  # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794
nc: 9  # number of classes
names: ['bicycle', 'bus', 'car',  'chair', 'diningtable',
        'motorbike', 'pottedplant','sofa', 'tvmonitor']
#处理数据

```python
import os
import random 
 
xmlfilepath=r'./Annotations'
saveBasePath=r"./ImageSets/Main/"
 
trainval_percent=1
train_percent=1

temp_xml = os.listdir(xmlfilepath)
total_xml = []
for xml in temp_xml:
    if xml.endswith(".xml"):
        total_xml.append(xml)

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)  
 
print("train and val size",tv)
print("traub suze",tr)
ftrainval = open(os.path.join(saveBasePath,'trainval.txt'), 'w')  
ftest = open(os.path.join(saveBasePath,'test.txt'), 'w')  
ftrain = open(os.path.join(saveBasePath,'train.txt'), 'w')  
fval = open(os.path.join(saveBasePath,'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()

voc_yolo_label 方法

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets = ['train', 'test','val']
# classes = ['cat','person']

# xmlfilepath=r'./Annotations'
# saveBasePath=r"./ImageSets/Main/"
# ftrain = open(os.path.join(saveBasePath,'train.txt'), 'w')  
# fval = open(os.path.join(saveBasePath,'val.txt'), 'w')  
 

classes = ['bicycle', 'bus', 'car',  'chair', 'diningtable',
        'motorbike', 'pottedplant','sofa', '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('/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset/voc2/Annotations/%s.xml' % (image_id), encoding='UTF-8')
    out_file = open('/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset/voc2/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))
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()
print(wd)
for image_set in sets:
    if not os.path.exists('/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset/voc2/labels/'):
        os.makedirs('/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset/voc2/labels/')
    image_ids = open('/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset/voc2/ImageSets2/Main/%s.txt' % (image_set)).read().strip().split()
    list_file = open('/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset/voc2/%s.txt' % (image_set), 'w', encoding='UTF-8')
    for image_id in image_ids:
        
        list_file.write('/dssg/home/zhineng_qt/test_2021_3_18_fu/dataset/voc2/JPEGImages/%s.jpg\n' % (image_id))  
        convert_annotation(image_id)
    list_file.close()
python train.py --data ./data/voc10.yaml --cfg ./models/yolov5s.yaml --weights '' 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值