使用python对数据集进行批量转移
将数据集中的图像转移到train和test文件夹中
import os
import shutil
#数据路径
sour_path='./'# 原数据集,还未拆分为train和test两个子集,填写相对路径
train_path='./train/'
test_path='./test/'
# 训练集和测试集需要多少子文件夹,自行设置。这里是200个子文件夹
for i in range(200):
os.mkdir(train_path+'('+str(i+1)+')')
os.mkdir(test_path+'('+str(i+1)+')')
# 接下来开始批量转移文件到train和test中。需要将上面的for循环注释掉。
for file in os.listdir(sour_path):
folder = os.path.join(sour_path, file)
if os.path.isdir(folder):
for i in range(3):
# 将前3张放在train文件夹下
img = folder + '/(' + str(i+1) + ')'+'.bmp'
if os.path.exists(img):
shutil.copy(img, train_path + file + '/(' + str(i+1) + ')' + '.bmp')
for j in range(3):
# 将后3张放在valid文件夹下
img = folder + '/(' + str(j+4) + ')' + '.bmp'
if os.path.exists(img):
shutil.copy(img, test_path + file + '/(' + str(j+4) + ')' + '.bmp')