数据划分测试、验证集,文件转移

import os, random, shutil

# 创建目录
test_path = './test_path/'
if not os.path.exists(test_path):
    os.makedirs(test_path)
    print('test_path is Ok')
else:
    print('test_path is exit')

##深度学习过程中,需要制作训练集和验证集、测试集。

def moveFile(fileDir):
        pathDir = os.listdir(fileDir)    #取图片的原始路径
        filenumber=len(pathDir)
        rate=0.1    #自定义抽取图片的比例,比方说100张抽10张,那就是0.1
        picknumber=int(filenumber*rate) #按照rate比例从文件夹中取一定数量图片
        sample_list = random.sample(pathDir, picknumber)  #随机选取picknumber数量的样本图片
        for name in sample_list:
                shutil.move(fileDir+name, tarDir+name)
        return

if __name__ == '__main__':
    fileDir = "./train_set/"    #源图片文件夹路径
    tarDir = test_path    #移动到新的文件夹路径
    moveFile(fileDir)
发布了29 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/c2250645962/article/details/103199549