深度学习数据集划分Python
import os
from shutil import copy
import random
file_path = 'dataset/val_masks'#源图像位置
images = os.listdir(file_path) # iamges 列表存储了该目录下所有图像的名称
num = len(images)
print(num)
eval_index = images[:1415] #按照8:2划分训练集测试集
for img in images:
if img in eval_index:
image_path = file_path +'/'+img
copy(image_path, 'dataset/train_data/masks') # 将选中的图像复制到新路径
else:
image_path = file_path +"/"+ img
copy(image_path, 'dataset/test_data/masks')
print('done')