使用keras训练模型时,我们需要将图片分为训练集和验证集,下面是使用python实现的划分数据集的代码
import os
import random
import shutil
from shutil import copy2
dir_name = os.listdir("./data")
def mk_dir():
#mkdir()--创建一级目录 makedirs()--创建多级目录
for name in dir_name:
os.makedirs('./train/' + name)
for name in dir_name:
os.makedirs('./val/' + name)
def move_file():
for name in dir_name:
trainfiles = os.listdir('./data/' + name + '/')#图片文件夹
num_train = len(trainfiles)
print( "num_train: " + str(num_train) )
index_list = list(range(num_train))
print(index_list)
random.shuffle(index_list)
num = 0
trainDir = './train/' + name + '/'#将图片文件夹中的7份放在这个文件夹下)
validDir = './val/' + name + '/'#将图片文件夹中的3份放在这个文件夹
for i in index_list:
fileName = './data/' + name + '/' + trainfiles[i]
if num < num_train*0.7:
print(str(fileName))
copy2(fileName, trainDir)
else:
copy2(fileName, validDir)
num += 1
if __name__ == "__main__":
mk_dir()
move_file()