写一段Python代码,划分yolo数据集8:1:1
时间: 2024-02-01 12:16:58 浏览: 71
假设你的数据集文件夹为data,其中包含了许多图片和对应的标签文件,下面是划分数据集的Python代码:
```python
import os
import random
import shutil
# 设置随机数种子,确保每次运行结果一致
random.seed(42)
# 划分比例
train_ratio = 0.8
val_ratio = 0.1
test_ratio = 0.1
# 数据集路径
data_path = "data"
# 存放划分后的数据集路径
train_path = "train"
val_path = "val"
test_path = "test"
# 获取所有图片的文件名(去掉扩展名)
image_names = [os.path.splitext(filename)[0] for filename in os.listdir(data_path) if filename.endswith(".jpg")]
# 随机打乱图片文件名
random.shuffle(image_names)
# 计算划分后的数据集大小
num_images = len(image_names)
num_train = int(num_images * train_ratio)
num_val = int(num_images * val_ratio)
num_test = num_images - num_train - num_val
# 创建存放划分后数据集的文件夹
os.makedirs(train_path, exist_ok=True)
os.makedirs(val_path, exist_ok=True)
os.makedirs(test_path, exist_ok=True)
# 将图片和标签文件复制到对应的文件夹中
for i, image_name in enumerate(image_names):
if i < num_train:
shutil.copy(os.path.join(data_path, image_name + ".jpg"), os.path.join(train_path, image_name + ".jpg"))
shutil.copy(os.path.join(data_path, image_name + ".txt"), os.path.join(train_path, image_name + ".txt"))
elif i < num_train + num_val:
shutil.copy(os.path.join(data_path, image_name + ".jpg"), os.path.join(val_path, image_name + ".jpg"))
shutil.copy(os.path.join(data_path, image_name + ".txt"), os.path.join(val_path, image_name + ".txt"))
else:
shutil.copy(os.path.join(data_path, image_name + ".jpg"), os.path.join(test_path, image_name + ".jpg"))
shutil.copy(os.path.join(data_path, image_name + ".txt"), os.path.join(test_path, image_name + ".txt"))
```
此代码将会把原始数据集按照8:1:1的比例划分为训练集、验证集和测试集,每个数据集里面包含了对应的图片文件和标签文件。
阅读全文