随机分配训练集,验证集

目录

coco json格式分配训练集,验证集

单个文件分配训练集,验证集


coco json格式分配训练集,验证集

import glob
import os.path
import random
import shutil

if __name__ == '__main__':



    train_img=r'D:\work\images'
    train_dir=r'D:\work\train'
    train_val=r'D:\work\val'

    os.makedirs(train_dir,exist_ok=True)
    os.makedirs(train_val,exist_ok=True)
    label_dir=r'D:\work\jsons'
    files = glob.glob(train_img + '/*.jpg')
    random.shuffle(files)
    data_len = len(files)

    train_len=int(data_len*0.7)

    for index, file in  enumerate(files):

        if index<train_len:
            shutil.copy(file,train_dir)
            shutil.copy(os.path.join(label_dir,os.path.basename(file)[:-4]+".json"),train_dir)
        else:
            shutil.copy(file, train_val)
            shutil.copy(os.path.join(label_dir, os.path.basename(file)[:-4] + ".json"), train_val)



单个文件分配训练集,验证集


import random
import os
if __name__ == '__main__':

    file = r'E:/project/icdar2015_label.txt'

    with open(file, 'r',encoding="utf-8") as f:
        datas = f.readlines()
    random.shuffle(datas)
    file_train=r'E:/project/label_train.txt'
    file_val=r'E:/project/label_val.txt'
    data_len = len(datas)
    train_len=int(data_len*0.7)
    trains=[]
    vals=[]
    for index, file in enumerate(datas):
        if index<train_len:
            trains.append(file)
        else:
            vals.append(file)
    with open(file_train, 'w',encoding="utf-8") as f:
        f.writelines(trains)
    with open(file_val, 'w',encoding="utf-8") as f:
        f.writelines(vals)

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/124563662