用Python将文件夹中的图片拆分为训练集、验证集、测试集
时间: 2023-12-18 13:03:17 浏览: 99
基于python的将labelme数据标注格式转换为YoloV8语义分割数据集,并可自动划分训练集和验证集
5星 · 资源好评率100%
好的,可以使用Python中的os和shutil模块来实现将文件夹中的图片拆分为训练集、验证集、测试集的功能。可以按照以下步骤进行操作:
1. 导入必要的模块:
```python
import os
import random
import shutil
```
2. 设置文件夹路径以及需要拆分的图片数量:
```python
folder_path = "your_folder_path"
train_path = "your_train_folder_path"
valid_path = "your_valid_folder_path"
test_path = "your_test_folder_path"
split_ratio = [0.7, 0.2, 0.1] # 分割比例,训练集占70%,验证集占20%,测试集占10%
```
3. 获取文件夹中的所有图片路径并打乱顺序:
```python
file_names = os.listdir(folder_path)
random.shuffle(file_names)
```
4. 根据分割比例计算出各自需要的图片数量:
```python
num_files = len(file_names)
train_split = int(split_ratio[0] * num_files)
valid_split = int(split_ratio[1] * num_files)
test_split = int(split_ratio[2] * num_files)
```
5. 将图片分别复制到训练集、验证集、测试集对应的文件夹中:
```python
for i, file_name in enumerate(file_names):
if i < train_split:
shutil.copy(os.path.join(folder_path, file_name), train_path)
elif i < train_split + valid_split:
shutil.copy(os.path.join(folder_path, file_name), valid_path)
else:
shutil.copy(os.path.join(folder_path, file_name), test_path)
```
完整代码如下:
```python
import os
import random
import shutil
folder_path = "your_folder_path"
train_path = "your_train_folder_path"
valid_path = "your_valid_folder_path"
test_path = "your_test_folder_path"
split_ratio = [0.7, 0.2, 0.1]
file_names = os.listdir(folder_path)
random.shuffle(file_names)
num_files = len(file_names)
train_split = int(split_ratio[0] * num_files)
valid_split = int(split_ratio[1] * num_files)
test_split = int(split_ratio[2] * num_files)
for i, file_name in enumerate(file_names):
if i < train_split:
shutil.copy(os.path.join(folder_path, file_name), train_path)
elif i < train_split + valid_split:
shutil.copy(os.path.join(folder_path, file_name), valid_path)
else:
shutil.copy(os.path.join(folder_path, file_name), test_path)
```
希望这个代码能够帮助到你!
阅读全文