# -*- coding: utf-8 -*-
from pathlib import Path #从pathlib中导入Path
import os
import fileinput
import random
root_path='/home/tay/Videos/trash/垃圾分类项目/total/'
train = open('./trash_train.txt','a')
test = open('./trash_test.txt','a')
pwd = os.getcwd() +'/'# the val data path 训练集的路径 def gen_txt():
i =0
for file in os.listdir(root_path):
print('file is{}'.format(str(file)))
for init in os.listdir(os.path.join(root_path, file)): #子文件夹
print('init is{}'.format(str(init)))
i += 1
pathDir = os.listdir(os.path.join(root_path, file, init)) #
print('pathDir is', pathDir)
file_num = len(pathDir)
rate = 0.2
pick_num = int(file_num * rate)
sample = random.sample(pathDir, pick_num) #随机选取20%的pathDir字符串
print('sample is', sample)
for pick_name in sample:
test.write(root_path.split('total/')[-1] +file + '/' + init +'/' + pick_name + ' ' + str(i) + '\n')
# for name in pathDir: #文件夹中的图片名
# print('name is{}'.format(str(name)))
# if test
# total.write(root_path.split('total/')[-1] +file + '/' + init +'/' + name + ' ' + str(i) + '\n' )
same = [x for x in pathDir if x in sample] #列表中相同的内容
diff = [y for y in (sample + pathDir) if y not in same] #列表中不同的内容
print('different', diff)
print('same', same)
for train_name in diff:
train.write(root_path.split('total/')[-1] +file + '/' + init +'/' + train_name + ' ' + str(i) + '\n')
gen_txt()

采用了random.sample函数来随机选取特定数量的文件名作为测试集,通过比较两个列表中不同的元素来获取训练集的文件名。

总体上就是在进行字符串操作。

用python制作训练集和测试集的图片名列表文本的更多相关文章

  1. 机器学习入门06 - 训练集和测试集 (Training and Test Sets)

    原文链接:https://developers.google.com/machine-learning/crash-course/training-and-test-sets 测试集是用于评估根据训练 ...

  2. sklearn获得某个参数的不同取值在训练集和测试集上的表现的曲线刻画

    from sklearn.svm import SVC from sklearn.datasets import make_classification import numpy as np X,y ...

  3. 随机切分csv训练集和测试集

    使用numpy切分训练集和测试集 觉得有用的话,欢迎一起讨论相互学习~Follow Me 序言 在机器学习的任务中,时常需要将一个完整的数据集切分为训练集和测试集.此处我们使用numpy完成这个任务. ...

  4. sklearn学习3----模型选择和评估(1)训练集和测试集的切分

    来自链接:https://blog.csdn.net/zahuopuboss/article/details/54948181 1.sklearn.model_selection.train_test ...

  5. sklearn——train_test_split 随机划分训练集和测试集

    sklearn——train_test_split 随机划分训练集和测试集 sklearn.model_selection.train_test_split随机划分训练集和测试集 官网文档:http: ...

  6. Sklearn-train_test_split随机划分训练集和测试集

    klearn.model_selection.train_test_split随机划分训练集和测试集 官网文档:http://scikit-learn.org/stable/modules/gener ...

  7. 将dataframe分割为训练集和测试集两部分

    data = pd.read_csv("./dataNN.csv",',',error_bad_lines=False)#我的数据集是两列,一列字符串,一列为0,1的labelda ...

  8. python 将数据随机分为训练集和测试集

    # -*- coding: utf-8 -*- """ Created on Tue Jun 23 15:24:19 2015 @author: hd "&qu ...

  9. Python数据预处理—训练集和测试集数据划分

    使用sklearn中的函数可以很方便的将数据划分为trainset 和 testset 该函数为sklearn.cross_validation.train_test_split,用法如下: > ...

随机推荐

  1. 变量的取用与设定:echo,变量设定规则,unset

    1.变量的取用echo echo $variable echo ${variable} 2.变量的设定规则 3.让我设定的name=VBird应用在下个应用程序 4.进入到核心的模块目录 5.取消设定 ...

  2. 关于Mach-O类型文件那点事

    Mach-O文件简介   Mach-O是一种文件格式,是Mach Object文件格式的缩写. 它通常应用于可执行文件,目标代码,动态库,内核转储等中.   Mach-O作为大部分基于Mach核心的操 ...

  3. AspectJ——预编译方式实现AOP

  4. 被裁的第50天,我终于拿到心仪公司Offer

    今天分享的是之前分享文章中被裁的小C,可以看这篇文<寒冬之下,被cai的那些人到底去哪了?>,最近他已经找到心仪公司今日头条Offer,并且即将入职,在应我要求下,他写了篇总结文如下.下文 ...

  5. MTV

    M:模型 models.py T:模板 html C:控制 urls.py 和 views.py 与MVC类似

  6. spring cloud-config的client中/refresh的端点报错401

    post访问/refresh端口报错如下 { "timestamp": 1537865395040, "status": 401, "error&qu ...

  7. python __init__()类构造方法

    构造方法用于创建对象时使用,每当创建一个类的实例对象时,python解释器都会自动调用它. class Person: def __init__(self): print("调用构造方法&q ...

  8. Bootstrap File Input 的使用

    由于工作需要使用Bootstrap的FileInput插件,在此分享下插件的使用方法 直接上代码 fileinput.html <!DOCTYPE html> <html> & ...

  9. [bzoj2815] [洛谷P2597] [ZJOI2012] 灾难

    Description 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. 学过 ...

  10. 【Oracle】复制表结构和表数据

    1.既复制表结构也复制表数据:CREATE TABLE tab_new AS SELECT * FROM tab_old; 2.只复制表结构:CREATE TABLE tab_new AS SELEC ...