当前位置:网站首页>yolov5s用自己的数据集进行训练模型
yolov5s用自己的数据集进行训练模型
2022-08-03 16:28:00 【鼾声鼾语】
https://blog.csdn.net/qq_40770527/article/details/124143214
自动化分训练集和验证集以及测试集的代码如下;
import os
import shutil
import random
# 保证随机可复现
random.seed(0)
# def mk_dir(file_path):
# if os.path.exists(file_path):
# # 如果文件夹存在,则先删除原文件夹在重新创建
# shutil.rmtree(file_path)
# os.makedirs(file_path)
def split_data(file_path, new_file_path, train_rate, val_rate, test_rate):
# yolov5训练自己数据集时 准备了images图片文件夹和txt标签文件夹;但是
# 需要分割训练集、验证集、测试集3个文件夹,每个文件夹有images和labels
# 2个文件夹;此方法可以把imags和labels总文件夹,分割成3个文件夹;
# file_path ='images 文件夹'
# xmlpath= 'txt文件夹'
# new_file_path='保存的新地址'
eachclass_image = []
for image in os.listdir(file_path):
eachclass_image.append(image)
total = len(eachclass_image)
random.shuffle(eachclass_image)
train_images = eachclass_image[0:int(train_rate * total)] # 注意左闭右开
val_images = eachclass_image[int(train_rate * total):int((train_rate + val_rate) * total)] # 注意左闭右开
test_images = eachclass_image[int((train_rate + val_rate) * total):]
#训练集
for image in train_images:
print(image)
old_path = file_path + '/' + image
new_path1 = new_file_path + '/' + 'train' + '/' + 'images'
if not os.path.exists(new_path1):
os.makedirs(new_path1)
new_path = new_path1 + '/' + image
# print(new_path)
shutil.copy(old_path, new_path)
new_name = os.listdir(new_file_path + '/' + 'train' + '/' + 'images')
# print(new_name[1][:-4])
for im in new_name:
old_xmlpath = xmlpath + '/' + im[:-3] + 'txt'
print('old',old_xmlpath)
new_xmlpath1 = new_file_path + '/' + 'train' + '/' + 'labels'
if not os.path.exists(new_xmlpath1):
os.makedirs(new_xmlpath1)
new_xmlpath = new_xmlpath1 + '/' + im[:-3] + 'txt'
print('xml name',new_xmlpath)
if not os.path.exists(f'{
old_xmlpath}'):
open(f'{
old_xmlpath}', 'w')
shutil.copy(old_xmlpath, new_xmlpath)
#验证集
for image in val_images:
old_path = file_path + '/' + image
new_path1 = new_file_path + '/' + 'val' + '/' + 'images'
if not os.path.exists(new_path1):
os.makedirs(new_path1)
new_path = new_path1 + '/' + image
shutil.copy(old_path, new_path)
new_name = os.listdir(new_file_path + '/' + 'val' + '/' + 'images')
for im in new_name:
old_xmlpath = xmlpath + '/' + im[:-3] + 'txt'
new_xmlpath1 = new_file_path + '/' + 'val' + '/' + 'labels'
if not os.path.exists(new_xmlpath1):
os.makedirs(new_xmlpath1)
new_xmlpath = new_xmlpath1 + '/' + im[:-3] + 'txt'
if not os.path.exists(f'{
old_xmlpath}'):
open(f'{
old_xmlpath}', 'w')
shutil.copy(old_xmlpath, new_xmlpath)
#测试集
for image in test_images:
old_path = file_path + '/' + image
new_path1 = new_file_path + '/' + 'test' + '/' + 'images'
if not os.path.exists(new_path1):
os.makedirs(new_path1)
new_path = new_path1 + '/' + image
shutil.copy(old_path, new_path)
new_name = os.listdir(new_file_path + '/' + 'test' + '/' + 'images')
for im in new_name:
old_xmlpath = xmlpath + '/' + im[:-3] + 'txt'
new_xmlpath1 = new_file_path + '/' + 'test' + '/' + 'labels'
if not os.path.exists(new_xmlpath1):
os.makedirs(new_xmlpath1)
new_xmlpath = new_xmlpath1 + '/' + im[:-3] + 'txt'
if not os.path.exists(f'{
old_xmlpath}'):
open(f'{
old_xmlpath}', 'w')
shutil.copy(old_xmlpath, new_xmlpath)
print('ok')
if __name__ == '__main__':
file_path = "./dropout-nosise-salt/img"
xmlpath = './dropout-nosise-salt/labels'
new_file_path = "./dropout-nosise-salt/train-val"
split_data(file_path, new_file_path, train_rate=0.7, val_rate=0.2, test_rate=0.1)
边栏推荐
- I am doing open source in Didi
- 组件通信--下拉菜单案例
- 为什么我强烈推荐使用智能化async?
- Kubernetes 笔记 / 入门 / 生产环境 / 容器运行时
- 将 Windows 事件日志错误加载到 SQL 表中
- 从MatePad Pro进化看鸿蒙OS的生态势能
- 简易网络传输方法
- To participate in sweepstakes, incoming new programmers magazine welfare!
- Not to be ignored!Features and advantages of outdoor LED display
- 【翻译】关于扩容一个百万级别用户系统的六个课程
猜你喜欢

Components of communication - the drop-down menu

Small Tools (4) integrated Seata1.5.2 distributed transactions

Introduction to the advantages of the new generation mesh network protocol T-Mesh wireless communication technology

为什么我强烈推荐使用智能化async?

详谈RDMA技术原理和三种实现方式

C专家编程 第1章 C:穿越时空的迷雾 1.10 “安静的改变”究竟有多少安静

Hannah荣获第六季完美童模全球总决赛全球人气总冠军

Windows 事件转发到 SQL 数据库

组件通信-父传子组件通信

使用 PowerShell 将 Windows 转发事件导入 SQL Server
随机推荐
leetcode:202. 快乐数
华为、联想、北汽等入选工信部“企业数字化转型和安全能力提升”首批实训基地
AI+BI+Visualization, Deep Analysis of Sugar BI Architecture
如何设计大电流九线导电滑环
请问下这个hologres维表是被缓存了么?怎么直接Finished了
CPU个数_核心数_线程数之间的关系
C专家编程 第2章 这不是Bug,而是语言特性 2.1 这关语言特性何事,在Fortran里这就是Bug呀
C专家编程 第3章 分析C语言的声明 3.9 轻松一下---驱动物理实体的软件
C语言03、数组
13 and OOM simulation
机器人开发--Universal Scene Description(USD)
C专家编程 第3章 分析C语言的声明 3.1 只有编译器才会喜欢的语法
为什么我强烈推荐使用智能化async?
C语言01、数据类型、变量常量、字符串、转义字符、注释
C专家编程 第2章 这不是Bug,而是语言特性 2.2 多做之过
[Deep Learning] Today's bug (August 2)
大佬们。使用flink-cdc-sqlserver 2.2.0 版本读取sqlserver2008R
Cookie和Session的关系
protobuf 中数据编码规则
mysql delete execution error: You can't specify target table 'doctor_info' for update in FROM clause