Tensorflow创建和读取17flowers数据集
http://blog.csdn.net/sinat_16823063/article/details/53946549
Tensorflow创建和读取17flowers数据集

- import os
- import tensorflow as tf
- from PIL import Image
- cwd = os.getcwd()
- classes = os.listdir(cwd+"/17flowers/jpg")
- writer = tf.python_io.TFRecordWriter("train.tfrecords")
- for index, name in enumerate(classes):
- class_path = cwd + "/17flowers/jpg/" + name + "/"
- if os.path.isdir(class_path):
- for img_name in os.listdir(class_path):
- img_path = class_path + img_name
- img = Image.open(img_path)
- img = img.resize((224, 224))
- img_raw = img.tobytes() #将图片转化为原生bytes
- example = tf.train.Example(features=tf.train.Features(feature={
- "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[int(name)])),
- 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
- }))
- writer.write(example.SerializeToString()) #序列化为字符串
- writer.close()
- print(img_name)
我们使用tf.train.Example来定义我们要填入的数据格式,其中label即为标签,也就是最外层的文件夹名字,img_raw为易经理二进制化的图片。然后使用tf.python_io.TFRecordWriter来写入。基本的,一个Example中包含Features,Features里包含Feature(这里没s)的字典。最后,Feature里包含有一个 FloatList, 或者ByteList,或者Int64List。就这样,我们把相关的信息都存到了一个文件中,所以前面才说不用单独的label文件。而且读取也很方便。
下面测试一下,已经存好的训练集是否可用:
- for serialized_example in tf.python_io.tf_record_iterator("train.tfrecords"):
- example = tf.train.Example()
- example.ParseFromString(serialized_example)
- image = example.features.feature['image'].bytes_list.value
- label = example.features.feature['label'].int64_list.value
- # 可以做一些预处理之类的
- print image, label
可以输出值,那么现在我们创建好的数据集已经存储在了统计目录下的train.tfrecords中了。接下来任务就是通过队列(queue)来读取这个训练集中的数据。
- def read_and_decode(filename):
- #根据文件名生成一个队列
- filename_queue = tf.train.string_input_producer([filename])
- reader = tf.TFRecordReader()
- _, serialized_example = reader.read(filename_queue)
- #返回文件名和文件
- features = tf.parse_single_example(serialized_example, features={
- 'label': tf.FixedLenFeature([], tf.int64), 'img_raw' : tf.FixedLenFeature([], tf.string), })
- img = tf.decode_raw(features['img_raw'], tf.uint8)
- img = tf.reshape(img, [224, 224, 3])
- img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
- label = tf.cast(features['label'], tf.int64)
- return img, label
其中的filename,即刚刚通过TFReader来生成的训练集。通过将其转化成string类型数据,再通过reader来读取队列中的文件,并通过features的名字,‘label’和‘img_raw’来得到对应的标签和图片数据。之后就是一系列的转码和reshape的工作了。
- img, label = read_and_decode("train.tfrecords")
- img_batch, label_batch = tf.train.shuffle_batch([img, label],batch_size=100, capacity=2000, min_after_dequeue=1000)
- labels = tf.one_hot(label_batch,17,1,0)
- coord = tf.train.Coordinator()
- threads = tf.train.start_queue_runners(coord=coord,sess=sess)
- for i in range(200):
- batch_xs, batch_ys = sess.run([img_batch, labels])
- print(sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5}))
- print("Loss:", sess.run(cross_entropy,feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5}))
- if i % 50 == 0:
- print(compute_accuracy(mnist.test.images, mnist.test.labels))
- coord.request_stop()
- coord.join()
注意一点,由于这里使用了队列的方式来进行训练集的读取,所以异步方式,通过Coordinator让queue runner通过coordinator来启动这些线程,并在最后读取队列结束后终止线程。
Tensorflow创建和读取17flowers数据集的更多相关文章
- 在C#下使用TensorFlow.NET训练自己的数据集
在C#下使用TensorFlow.NET训练自己的数据集 今天,我结合代码来详细介绍如何使用 SciSharp STACK 的 TensorFlow.NET 来训练CNN模型,该模型主要实现 图像的分 ...
- (第二章第三部分)TensorFlow框架之读取二进制数据
系列博客链接: (第二章第一部分)TensorFlow框架之文件读取流程:https://www.cnblogs.com/kongweisi/p/11050302.html (第二章第二部分)Tens ...
- tensorflow之数据读取探究(2)
tensorflow之tfrecord数据读取 Tensorflow关于TFRecord格式文件的处理.模型的训练的架构为: 1.获取文件列表.创建文件队列:http://blog.csdn.net/ ...
- 【猫狗数据集】谷歌colab之使用pytorch读取自己数据集(猫狗数据集)
之前在:https://www.cnblogs.com/xiximayou/p/12398285.html创建好了数据集,将它上传到谷歌colab 在colab上的目录如下: 在utils中的rdat ...
- TensorFlow从0到1之TensorFlow逻辑回归处理MNIST数据集(17)
本节基于回归学习对 MNIST 数据集进行处理,但将添加一些 TensorBoard 总结以便更好地理解 MNIST 数据集. MNIST由https://www.tensorflow.org/get ...
- TensorFlow从0到1之TensorFlow csv文件读取数据(14)
大多数人了解 Pandas 及其在处理大数据文件方面的实用性.TensorFlow 提供了读取这种文件的方法. 前面章节中,介绍了如何在 TensorFlow 中读取文件,本节将重点介绍如何从 CSV ...
- C#无限极分类树-创建-排序-读取 用Asp.Net Core+EF实现之方法二:加入缓存机制
在上一篇文章中我用递归方法实现了管理菜单,在上一节我也提到要考虑用缓存,也算是学习一下.Net Core的缓存机制. 关于.Net Core的缓存,官方有三种实现: 1.In Memory Cachi ...
- [转载]MongoDB学习 (四):创建、读取、更新、删除(CRUD)快速入门
本文介绍数据库的4个基本操作:创建.读取.更新和删除(CRUD). 接下来的数据库操作演示,我们使用MongoDB自带简洁但功能强大的JavaScript shell,MongoDB shell是一个 ...
- excel2003和excel2007文件的创建和读取
excel2003和excel2007文件的创建和读取在项目中用的很多,首先我们要了解excel的常用组件和基本操作步骤. 常用组件如下所示: HSSFWorkbook excel的文档对象 HSSF ...
随机推荐
- TCP/IP四层协议
1.数据链路层 数据链路层实现了网卡接口的网络驱动程序,处理数据在物理媒介(以太网,令牌环)上的传输,常用协议包含ARP(地址解析协议),RARP(逆地址解析协议)两个协议,他们实现了IP地址和物理地 ...
- Python 余弦相似度与皮尔逊相关系数 计算
夹角余弦(Cosine) 也可以叫余弦相似度. 几何中夹角余弦可用来衡量两个向量方向的差异,机器学习中借用这一概念来衡量样本向量之间的差异. (1)在二维空间中向量A(x1,y1)与向量B(x2,y2 ...
- 牛客练习赛33 D tokitsukaze and Inverse Number (树状数组求逆序对,结论)
链接:https://ac.nowcoder.com/acm/contest/308/D 来源:牛客网 tokitsukaze and Inverse Number 时间限制:C/C++ 1秒,其他语 ...
- DDD领域驱动设计初探(七):Web层的搭建
前言:好久没更新博客了,每天被该死的业务缠身,今天正好一个模块完成了,继续来完善我们的代码.之前的六篇完成了领域层.应用层.以及基础结构层的部分代码,这篇打算搭建下UI层的代码. DDD领域驱动设计初 ...
- “jps”命令的用处?
jps位于jdk的bin目录下,其作用是显示当前系统的java进程情况,及其id号. jps相当于Solaris进程工具ps.不像”pgrep java” 或”ps -ef grep java”,jp ...
- Python (2) 除法
/ 除法 自动转化为浮动数 // 整除 只保留整数部分 >>> 2/21.0>>> 2//21>>> 1 ...
- C++ GUI Qt4学习笔记05
C++ GUI Qt4学习笔记05 qtc++正则表达式 QIntValidator -- 只让用户输入整数 QDoubleValidator -- 只让用户输入浮 ...
- Centos7 离线安装 php7
问题:因内部管控,机器无法连接公有yum源安装php. 正常安装php7可以参考CentOS7.2 安装 PHP7.2 下面的代码也是一种方法 yum -y install libmcrypt lib ...
- dede后台系统基本参数空白怎么办?
dede后台系统基本参数空白怎么办? 如图: 解决办法:还原dede_sysconfig表即可 后台 系统-SQL命令行工具,执行如下sql delete table dede_sysconfig ...
- 求大师点化,寻求大文件(最大20G左右)上传方案
之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...