【python代码】生成训练集和测试集路径, 写入train.txt和test.txt文件

import glob, os

# 数据集的位置
imgs_dir = '/img'
print(imgs_dir)

#用作 test 的图片数据的比例
percentage_test = 10;

#创建训练数据集和测试数据集:train.txt 和 test.txt
file_train = open('/train.txt', 'w')
file_test = open('/test.txt', 'w')
counter = 1
index_test = round(100 / percentage_test)
for pathAndFilename in glob.iglob(os.path.join(imgs_dir, "*.jpg")):
    title, ext = os.path.splitext(os.path.basename(pathAndFilename))

    if counter == index_test:
        counter = 1
        file_test.write('/dataset/sar' + "/" + title + '.jpg' + "\n")
    else:
        file_train.write('/dataset/sar' + "/" + title + '.jpg' + "\n")
        counter = counter + 1

你可能感兴趣的:(python,深度学习)