用python制作训练集和测试集的图片名列表文本
# -*- 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制作训练集和测试集的图片名列表文本的更多相关文章
- 机器学习入门06 - 训练集和测试集 (Training and Test Sets)
原文链接:https://developers.google.com/machine-learning/crash-course/training-and-test-sets 测试集是用于评估根据训练 ...
- sklearn获得某个参数的不同取值在训练集和测试集上的表现的曲线刻画
from sklearn.svm import SVC from sklearn.datasets import make_classification import numpy as np X,y ...
- 随机切分csv训练集和测试集
使用numpy切分训练集和测试集 觉得有用的话,欢迎一起讨论相互学习~Follow Me 序言 在机器学习的任务中,时常需要将一个完整的数据集切分为训练集和测试集.此处我们使用numpy完成这个任务. ...
- sklearn学习3----模型选择和评估(1)训练集和测试集的切分
来自链接:https://blog.csdn.net/zahuopuboss/article/details/54948181 1.sklearn.model_selection.train_test ...
- sklearn——train_test_split 随机划分训练集和测试集
sklearn——train_test_split 随机划分训练集和测试集 sklearn.model_selection.train_test_split随机划分训练集和测试集 官网文档:http: ...
- Sklearn-train_test_split随机划分训练集和测试集
klearn.model_selection.train_test_split随机划分训练集和测试集 官网文档:http://scikit-learn.org/stable/modules/gener ...
- 将dataframe分割为训练集和测试集两部分
data = pd.read_csv("./dataNN.csv",',',error_bad_lines=False)#我的数据集是两列,一列字符串,一列为0,1的labelda ...
- python 将数据随机分为训练集和测试集
# -*- coding: utf-8 -*- """ Created on Tue Jun 23 15:24:19 2015 @author: hd "&qu ...
- Python数据预处理—训练集和测试集数据划分
使用sklearn中的函数可以很方便的将数据划分为trainset 和 testset 该函数为sklearn.cross_validation.train_test_split,用法如下: > ...
随机推荐
- JS 中检测数组的四种方法
今天和大家分享一下 JS 中检测是不是数组的四种方法,虽然篇幅不长,不过方法应该算是比较全面了. 1. instanceof 方法 instanceof 用于检测一个对象是不是某个类的实例,数组也是一 ...
- Essential C++学习笔记
1.当我们调用一个函数时,会在内存中建立起一块特殊区域,称为“程序栈”,这块特殊区域提供了每个函数参数的存储空间,它也提供函数所定义的每个对象的内存空间--我们将这些对象称为局部对象.一旦函数完成,这 ...
- 【JavaWeb学习】过滤器Filter
一.简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静 ...
- 原生js面向对象编程-选项卡(点击)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 悄摸直播(三)—— 搭建rtmp服务器(smart_rtmpd - rtmp服务器搭建)
悄摸直播 -- javaCV实现本机摄像头画面远程直播 搭建rtmp服务器 一.素材 rtmp服务器:smart_rtmpd ffmpeg工具:ffmpeg.exe 二.搭建 1.下载smart_rt ...
- FWT 入门
#include <bits/stdc++.h> using namespace std; #define ll long long const ll maxn = 3e5+5; cons ...
- 关于在读取excel的文件时候,放在服务器上就报路径错误
就是指定这个路径:C:\Program Files (x86)\IIS Express 因为在上传到服务器的时候,服务器读取的是在服务器上的路径,所以正确的思路应该是 把上传的Excel存在服务器上, ...
- php-lnmp环境搭建
参考网站:http://www.liyblog.top/p/9 1.nginx和php基本安装 1.更新apt apt update 2.安装nginx apt install nginx 3.查看n ...
- WordPress使用PHPMailer发送gmail邮件
wordpress使用phpmailer发送gmail邮件 0.保证用于gmail账号已经开启imap服务,且你能正常访问到gmail的smtp服务.(需要climb over the wall) 1 ...
- redis server can not continue