# -*- 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. Spring Cloud(一):服务注册中心Eureka

    Spring Cloud 基于 Netflix 的几个开源项目进行了封装,提供包括服务注册与发现(Eureka),智能路由(Zuul),熔断器(Hystrix),客户端负载均衡(Ribbon)等在内的 ...

  2. Longhorn入门级教程!轻松实现持久化存储!

    介 绍 在本文中你将学会如何使用k3s在Civo上运行Longhorn.如果你还没使用过Civo,可以到官网注册(https://www.civo.com/ )还可以申请免费的使用额度.首先,需要一个 ...

  3. python修改列表

    替换元素 效果图: 代码: #创建一个列表 list = ['a','b','c','d','e','f'] print('修改前:',list) #修改元素 指定索引重设其值 list[1] = ' ...

  4. vue的param和query两种传参方式及URL的显示

    路由配置: // 首页 { path: '/home', name:'home', component:Home }, // 行情 { path: '/markets', name:'market', ...

  5. Callable接口用法

    注意点:FutureTask是Runnable的实现类,它的构造器可以传Callable接口的实现类. 例子如下: class MyThread implements Callable<Inte ...

  6. Spring Boot自动装配

    前言 一些朋友问我怎么读源码,这篇文章结合我看源码时候一些思路给大家聊聊,我主要从这三个方向出发: 确定目标,这个目标要是一个具体,不要一上来我要看懂Spring,这是不可能的,目标要这么来定,比如看 ...

  7. Django 导入配置文件

    from django.conf import settings

  8. 使用Route Prefix 使用属性路由 精通ASP-NET-MVC-5-弗瑞曼

  9. Halo-个人独立博客系统

    项目地址:https://github.com/halo-dev/halo 安装指导:https://halo.run/guide/   简介: Halo 是一款现代化的个人独立博客系统,给习惯写博客 ...

  10. selenium-第一个自动化脚本

    经过上一篇的环境搭建,这一篇我们开始编写第一个自动化脚本. 一个简单的测试百度的demo #coding=utf-8 from selenium import webdriver driver = w ...