美呗贤果 发表于 4 天前

看看使用 Python 脚本将标注数据集划分为训练集和验证集

前言
在深度学习项目中,将数据集划分为训练集和验证集是非常重要的一步。本文将介绍如何使用Python脚本将通过LabelImg标注的图片和对应的标签文件分为训练集、验证集和测试集,并提供详细的步骤和示例代码。

1项目背景
在使用YOLO等深度学习模型进行图像识别时,训练集和验证集的划分是确保模型性能的重要步骤。本文提供一个自动化的Python脚本,帮助用户将标注的数据集按照指定比例划分为训练集和验证集。代码将会读取指定的图片和标签文件目录,随机打乱数据顺序,然后将数据分配到对应的文件夹中。

2准备工作
在开始之前,请确保你已经安装了Python环境,并将待处理的图片和标签文件放在指定目录下。


图片目录:C:\Users\Administrator\Desktop\allData\images

如图:图片文件都被放置于allData目录下的images中




标签目录:C:\Users\Administrator\Desktop\allData\labels

如图:图片所对应的标签文件都放置于allData目录下的labels中




期望的输出目录:C:\Users\Administrator\Desktop\SplitData

3代码现
以下是将数据集划分为训练集和验证集的Python代码:

因为我这里不需要测试test集,就将test的占比设为了0

importos
importshutil
importrandom

#设置随机种子
randomseed(0)

defsplit_data(file_path,xml_path,new_file_path,train_rate,val_rate,test_rate):
'''====1将数据集打乱===='''
each_class_image=
each_class_label=
forimageinoslistdir(file_path):
each_class_image脚本end(image)
forlabelinoslistdir(xml_path):
each_class_label脚本end(label)
#将两个文件通过zip()函数绑定
data=list(zip(each_class_image,each_class_label))
#计算总长度
total=len(each_class_image)
#randomshuffle()函数打乱顺序
randomshuffle(data)
#再将两个列表解绑
each_class_image,each_class_label=zip(*data)

'''====2分别获取train、val、test这个文件夹对应的图片和标签===='''
train_images=each_class_image
val_images=each_class_image
test_images=each_class_image
train_labels=each_class_label
val_labels=each_class_label
test_labels=each_class_label

'''====3设置相应的路径保存格式,将图片和标签对应保存下来===='''
#train
forimageintrain_images:
old_path=file_path+''+image
new_path1=new_file_path+''+'train'+''+'images'
osmakedirs(new_path1,exist_ok=True)
new_path=new_path1+''+image
shutilcopy(old_path,new_path)

forlabelintrain_labels:
old_path=xml_path+''+label
new_path1=new_file_path+''+'train'+''+'labels'
osmakedirs(new_path1,exist_ok=True)
new_path=new_path1+''+label
shutilcopy(old_path,new_path)

#val
forimageinval_images:
old_path=file_path+''+image
new_path1=new_file_path+''+'val'+''+'images'
osmakedirs(new_path1,exist_ok=True)
new_path=new_path1+''+image
shutilcopy(old_path,new_path)

forlabelinval_labels:
old_path=xml_path+''+label
new_path1=new_file_path+''+'val'+''+'labels'
osmakedirs(new_path1,exist_ok=True)
new_path=new_path1+''+label
shutilcopy(old_path,new_path)

#test
forimageintest_images:
old_path=file_path+''+image
new_path1=new_file_path+''+'test'+''+'images'
osmakedirs(new_path1,exist_ok=True)
new_path=new_path1+''+image
shutilcopy(old_path,new_path)

forlabelintest_labels:
old_path=xml_path+''+label
new_path1=new_file_path+''+'test'+''+'labels'
osmakedirs(new_path1,exist_ok=True)
new_path=new_path1+''+label
shutilcopy(old_path,new_path)

if__name__=='__main__':
file_path=r"C:\Users\Administrator\Desktop\allData\images"#原图片路径
txt_path=r"C:\Users\Administrator\Desktop\allData\labels"#原标签路径
new_file_path=r"C:\Users\Administrator\Desktop\SplitData"#划分后存放目录
#设置划分比例
split_data(file_path,txt_path,new_file_path,train_rate=085,val_rate=015,test_rate=0)
4运行结果
运行后,可以看到数据集已经按照代码中train:val=085:015的比例划分了



以上是将标注数据集划分为训练集和验证集的解决方案,欢迎在幽络源上获取更多源码和技术教程!

?



在同行眼中,源码网有着让人觊觎的好品质,也有着很多让人嫉妒的忠实粉丝。提供经过严格测试的免费源码、各种线上兼职和网络兼职的网创教程、编程及网络相关的技术教程分享,助您轻松获取资源和技术支持。https://www.youluoyuan.com/https://www.youluoyuan.com/wp-content/uploads/2025/04/cd76b3ef-2f58-45ae-9eca-94428de13424.png
页: [1]
查看完整版本: 看看使用 Python 脚本将标注数据集划分为训练集和验证集