一见钟情 网站,昆明系统开发,办公用品网站建设可行性分析,3d动画制作设计公司一、功能介绍 这段代码实现了从给定的图像和标签文件夹中分割数据集为训练集、验证集和测试集的功能。以下是代码功能的总结#xff1a; 创建目标文件夹结构#xff1a; 在指定的根目录#xff08;dataset_root#xff09;下创建images和labels两个文件夹。在这两个文件夹下…一、功能介绍 这段代码实现了从给定的图像和标签文件夹中分割数据集为训练集、验证集和测试集的功能。以下是代码功能的总结 创建目标文件夹结构 在指定的根目录dataset_root下创建images和labels两个文件夹。在这两个文件夹下分别创建train、val和test三个子文件夹用于存放不同阶段的数据。 统计类别数量 遍历标签文件夹中的所有文本文件统计每个类别在所有标签文件中出现的总次数。 计算分割比例 根据给定的比例默认为训练集80%验证集10%测试集10%计算每个类别在训练集、验证集和测试集中应该有的数量。 随机分配数据 遍历图像文件夹中的所有图片。对于每个图片检查其对应的标签文件是否存在。读取标签文件提取其中的类别信息。根据随机数决定图片属于训练集、验证集还是测试集。将图片和对应的标签文件复制到相应的文件夹中同时更新类别数量记录。 最终结果 数据集按照指定的比例被划分为训练集、验证集和测试集。每个类别在各个数据集中的分布尽量保持均衡。
二、代码
import os
import random
import shutildef split_dataset(image_folder, label_folder, train_ratio0.8, val_ratio0.1, test_ratio0.1):将图像和标签文件按指定比例分割成训练集、验证集和测试集。参数:image_folder (str): 图像文件夹路径。label_folder (str): 标签文件夹路径。train_ratio (float): 训练集所占比例默认为0.8。val_ratio (float): 验证集所占比例默认为0.1。test_ratio (float): 测试集所占比例默认为0.1。# 创建目标文件夹dataset_root rE:\pythonProject\pythonProject\after_neuos.makedirs(dataset_root, exist_okTrue)# 创建images和labels文件夹images_folder os.path.join(dataset_root, images)labels_folder os.path.join(dataset_root, labels)os.makedirs(images_folder, exist_okTrue)os.makedirs(labels_folder, exist_okTrue)# 创建train、val和test子文件夹for split in [train, val, test]:os.makedirs(os.path.join(images_folder, split), exist_okTrue)os.makedirs(os.path.join(labels_folder, split), exist_okTrue)# 统计每个类别的图片数量category_counts {}for filename in os.listdir(label_folder):label_path os.path.join(label_folder, filename)with open(label_path, r) as label_file:lines label_file.readlines()categories [line.split()[0] for line in lines]for category in categories:category_counts[category] category_counts.get(category, 0) 1# 计算每个类别在训练集、验证集和测试集中的数量train_category_counts {}val_category_counts {}test_category_counts {}for category, count in category_counts.items():train_count int(count * train_ratio)val_count int(count * val_ratio)test_count count - train_count - val_counttrain_category_counts[category] train_countval_category_counts[category] val_counttest_category_counts[category] test_count# 遍历图片文件夹for filename in os.listdir(image_folder):image_path os.path.join(image_folder, filename)label_path os.path.join(label_folder, os.path.splitext(filename)[0] .txt)# 确保标注文件存在if not os.path.exists(label_path):continue# 读取标注文件获取类别信息with open(label_path, r) as label_file:lines label_file.readlines()categories [line.split()[0] for line in lines]# 确定将图片放入的集合rand random.random()if rand train_ratio:destination_folder traincategory_counts train_category_countselif rand train_ratio val_ratio:destination_folder valcategory_counts val_category_countselse:destination_folder testcategory_counts test_category_counts# 移动图片和标注文件到目标文件夹for category in categories:category_folder_images os.path.join(images_folder, destination_folder)category_folder_labels os.path.join(labels_folder, destination_folder)os.makedirs(category_folder_images, exist_okTrue)os.makedirs(category_folder_labels, exist_okTrue)if category_counts[category] 0:shutil.copy(image_path, os.path.join(category_folder_images, filename))shutil.copy(label_path, os.path.join(category_folder_labels, os.path.splitext(filename)[0] .txt))category_counts[category] - 1# 图片文件夹路径
image_folder rE:\pythonProject\pythonProject\NEU-DET\images# 标注文件夹路径
label_folder rE:\pythonProject\pythonProject\NEU-DET\txt# 调用函数进行数据集分割
split_dataset(image_folder, label_folder) 这个数据集划分代码相比与其他的不是随机划分考虑到每个类别的图片样张可能不均衡所以按照类别去划分数据集。需要先把xml转成yolo的txt格式然后指定图片、txt标签、保存文件夹路径即可。在NEU-DET数据集上运行结果如下