文件组织形式:
代码和数据集位于同一级目录
以下需要修改的地方已经标注:
import os
import random
#------------基本参数(修改下面4个)----------------#
trainval_percent = 0.8#用于训练&评估的比例
train_percent = 0.75#用于训练的比例
xmlfilepath = 'JPEGImages'#标签文件存储位置
txtsavepath = 'Segmentation'#txt存放的文件夹
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
#------------存放标签的txt文件所在位置,指定到文件本身----------------#
ftrainval = open('Segmentation/trainval.txt', 'w')
ftest = open('Segmentation/test.txt', 'w')
ftrain = open('Segmentation/train.txt', 'w')
fval = open('Segmentation/val.txt', 'w')
#------------存放标签的txt文件所在位置,指定到文件本身----------------#
for i in list:
name = total_xml[i][:-4] + '\n' #如果是.jpg就是-4,如果是.json就是-5
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()