将图片划分为训练集、验证集和测试集三个文件,并生成txt文件
时间: 2024-02-03 12:04:09 浏览: 82
您可以使用以下Python代码来将图片分为训练集、验证集和测试集,并生成txt文件:
```python
import os
import random
# 设置图片文件夹路径
data_folder = "/path/to/data/folder/"
# 设置训练集、验证集和测试集的比例(总比例要等于1)
train_ratio = 0.7
val_ratio = 0.2
test_ratio = 0.1
# 获取所有图片的文件名
all_files = os.listdir(data_folder)
# 随机打乱图片文件名的顺序
random.shuffle(all_files)
# 计算每个集合的大小
num_train = int(len(all_files) * train_ratio)
num_val = int(len(all_files) * val_ratio)
num_test = int(len(all_files) * test_ratio)
# 将图片文件名分为训练集、验证集和测试集
train_files = all_files[:num_train]
val_files = all_files[num_train:num_train+num_val]
test_files = all_files[num_train+num_val:]
# 创建训练集、验证集和测试集文件夹
os.makedirs(os.path.join(data_folder, "train"), exist_ok=True)
os.makedirs(os.path.join(data_folder, "val"), exist_ok=True)
os.makedirs(os.path.join(data_folder, "test"), exist_ok=True)
# 将图片文件拷贝到对应的文件夹中
for filename in train_files:
src = os.path.join(data_folder, filename)
dst = os.path.join(data_folder, "train", filename)
os.rename(src, dst)
for filename in val_files:
src = os.path.join(data_folder, filename)
dst = os.path.join(data_folder, "val", filename)
os.rename(src, dst)
for filename in test_files:
src = os.path.join(data_folder, filename)
dst = os.path.join(data_folder, "test", filename)
os.rename(src, dst)
# 生成训练集、验证集和测试集的txt文件
with open(os.path.join(data_folder, "train.txt"), "w") as f:
for filename in train_files:
f.write(os.path.join("train", filename) + "\n")
with open(os.path.join(data_folder, "val.txt"), "w") as f:
for filename in val_files:
f.write(os.path.join("val", filename) + "\n")
with open(os.path.join(data_folder, "test.txt"), "w") as f:
for filename in test_files:
f.write(os.path.join("test", filename) + "\n")
```
这个代码假设您的图片文件夹包含所有要分类的图片,并且文件名以.jpg或.png结尾。您需要将"data_folder"变量设置为包含这些图片的文件夹的路径。代码将随机打乱文件名的顺序,并将其分为训练集、验证集和测试集。它还将创建包含这些文件的对应文件夹,并生成包含文件名的txt文件。
阅读全文