train.txt---val.txt---trainval.txt---test.txt 生成

import os
import random

def writeTxt(content,fileName):
    with open(fileName,'w') as f:
        for i in range(len(content)):
            f.writelines(content[i]+'\n')

root = r'C:\Users\Lenovo\Desktop\data_5\Step3_caiJian'

imgNames = os.listdir(root)
print(imgNames)
imgNames = [imgName[:-4] for imgName in imgNames]
print(imgNames)
random.shuffle(imgNames)
print(imgNames)
p1 = 0.8
p2 = 0.1
trainTxt = r'C:\Users\Lenovo\Desktop\data_5\train.txt'
valTxt = r'C:\Users\Lenovo\Desktop\data_5\val.txt'
testTxt = r'C:\Users\Lenovo\Desktop\data_5\test.txt'
trainValTxt = r'C:\Users\Lenovo\Desktop\data_5\trainVal.txt'
trainImgNames = imgNames[:int(len(imgNames)*p1)]
valImgNames = imgNames[int(len(imgNames)*p1):int(len(imgNames)*(p1+p2))]
testImgNames = imgNames[int(len(imgNames)*(p1+p2)):]
trainValImgNames = imgNames[:int(len(imgNames)*(p1+p2))]
print(len(trainImgNames),len(valImgNames),len(trainValImgNames),len(testImgNames))
writeTxt(trainImgNames,trainTxt)
writeTxt(valImgNames,valTxt)
writeTxt(trainValImgNames,trainValTxt)
writeTxt(testImgNames,testTxt)

 

发布了31 篇原创文章 · 获赞 14 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览