下载17flowers数据集,并生成train.txt和validation.txt两个用来训练的文件

下载数据集:

# 在目标路径里type如下代码运行即可自动下载好数据集并自动分好类别
import tflearn.datasets.oxflower17 as oxflower17
X, Y = oxflower17.load_data(one_hot=True)

生成训练文件:

import os
import random
path = r'/home/yuan/PycharmProjects/untitled/17flowers/jpg/'

def generate_txt(path):
    dir_list = os.listdir(path)
    print("======start=====")
    trainList = open(path + '/' + 'train.txt', 'w')
    valList = open(path + '/' + 'validation.txt', 'w')
    for i in range(len(dir_list)):
        label_path = os.path.join(path,dir_list[i])
        if os.path.isdir(label_path):
            label = dir_list[i]
            files = os.listdir(label_path)
            random.shuffle(files)
            for num,file in enumerate(files):
                fileType = os.path.split(file)
                if fileType[1] == '.txt':
                    continue
                name = label_path+ '/' + file + ' ' + label + '\n'
                if num <= int(len(files)*0.7):
                    trainList.write(name)
                else:
                    valList.write(name)
    trainList.close()
    valList.close()
    print("======endall=======")

if __name__ == '__main__':
    generate_txt(path)






参考博客:https://www.cnblogs.com/ilym/p/8666148.html

你可能感兴趣的:(下载17flowers数据集,并生成train.txt和validation.txt两个用来训练的文件)