随机分配训练集,验证集

x33g5p2x  于2022-05-05 转载在 其他  
字(1.3k)|赞(0)|评价(0)|浏览(409)

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)

相关文章