随机分配训练集,验证集
【摘要】
目录
coco json格式分配训练集,验证集
单个文件分配训练集,验证集
coco json格式分配训练集,验证集
import globimport os.pathimport randomimport shutil if __name__ == '__main__': train_img=r'D:\wor...
目录
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,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/124563662
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)