当前位置:网站首页>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)
边栏推荐
- protobuf 中数据编码规则
- 纯纯粹粹纯纯粹粹
- 高效的组织信息共享知识库是一种宝贵的资源
- I am doing open source in Didi
- 从MatePad Pro进化看鸿蒙OS的生态势能
- To add digital wings to education, NetEase Yunxin released the overall solution of "Internet + Education"
- C专家编程 第3章 分析C语言的声明 3.2 声明是如何形成的
- Web3 安全风险令人生畏?应该如何应对?
- 数据中台“集存通用治”功能场景说明
- C专家编程 第3章 分析C语言的声明 3.9 轻松一下---驱动物理实体的软件
猜你喜欢
FinClip | 2022 年 7 月产品大事记
使用Stream多年,collect还有这些“骚操作”?
2年开发经验去面试,吊打面试官,即将面试的程序员这些笔记建议复习
leetcode:189. 轮转数组
MPLS的wpn实验
AI+BI+Visualization, Deep Analysis of Sugar BI Architecture
Not to be ignored!Features and advantages of outdoor LED display
#夏日挑战赛# HarmonyOS 实现一个绘画板
虹科分享 | 如何测试与验证复杂的FPGA设计(3)——硬件测试
MySQL相关介绍
随机推荐
C专家编程 第1章 C:穿越时空的迷雾 1.8 ANSI C标准的结构
附录A 程序员工作面试的秘密
从零开始搭建MySQL主从复制架构
使用.NET简单实现一个Redis的高性能克隆版(一)
MySQL窗口函数
罗克韦尔AB PLC RSLogix5000中创建新项目、任务、程序和例程的具体方法和步骤
uniapp隐藏导航栏和横屏显示设置
Hannah荣获第六季完美童模全球总决赛全球人气总冠军
MobileVIT实战:使用MobileVIT实现图像分类
13 and OOM simulation
[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window
node连接mongoose数据库流程
STM32 GPIO LED and buzzer implementation [Day 4]
正向代理与反向代理
MySQL窗口函数 OVER()函数介绍
vector类
leetcode SVM
MATLAB | 七夕节快到了,还不给朋友安排上这个咕呱小青蛙?
Tolstoy: There are only two misfortunes in life
leetcode-268.丢失的数字