tensorFlow

Easter79
• 阅读 492

先介绍一下TensorFlow自带的数据格式:

TensorFlow自带一种数据格式叫做tfrecords。 你可以把你的输入转成专属与TensorFlow的tfrecords格式并保存在本地。

-关于输入碎碎念:输入比如图片,可以有各种格式呀首先你从网上下载到的一般是png或者jpg格式的吧, 你可以把它存成一个矩阵的形式(numpy ndarray),如果不用TensorFlow自带的tfrecords,你其实也可以存成python独有的pickle文件哈。

那么要怎样把数据存成tfrecords呢?

当然是用TensorFlow api库啦,就是下面这个Class:

tf.python_io.TFRecordWriter

__init__(self, path, options=None)
Opens file `path` and creates a `TFRecordWriter` writing to it.
Args:
path: The path to the TFRecords file.
options: (optional) A TFRecordOptions object.

在参数列表里指明你想要存放的路径。

tf.python_io.TFRecordWriter('SVHN/train.tfrecords')

虽然有一点没逻辑,但是我还是要介绍一下在处理图片数据输入需要用到的一个TensorFlow Class:

class GFile(tensorflow.python.lib.io.file_io.FileIO)

这是一个用来处理文件IO的类,它包含一个类似正则的查找匹配的函数我们可以用它来找到我们想要的文件->tf.gfile.Glob

它返回一个包含所有满足条件元素的列表。

初始化一个TFRecordWriter完成后,就等于知道了tfrecords的存放路径,接下来就要往这个文件中存数据呀!这里用到了这个类的write函数。

tf.python_io.TFRecordWriter
write(self, record)
Write a string record to the file.
Args:
record: str

当要读取tfrecords中的数据时,要做以下的事情:

首先呢需要一个pipeline,然后需要将tfrecords的存放路径作为一个str放入到一个queue中。string_input_producer这个函数负责完成这件事。

string_input_producer(string_tensor, num_epochs=None, shuffle=True, seed=None, capacity=32, shared_name=None, name=None, cancel_op=None)
Output strings (e.g. filenames) to a queue for an input pipeline.

这个函数需要传入一个文件名list,系统会自动将它转为一个文件名队列。

 tf.train.string_input_producer还有两个重要的参数,一个是num_epochs,它就epoch数。另外一个就是shuffle,shuffle是指在一个epoch内文件的顺序是否被打乱。

在tensorflow中,内存队列不需要我们自己建立,我们只需要使用reader对象从文件名队列中读取数据就可以了。

类似的TensorFlow有相对应的TFRecordReader类来读取。

class TFRecordReader(ReaderBase)
A Reader that outputs the records from a TFRecords file
__init__(self, name=None, options=None)
Create a TFRecordReader.
Args:
name: A name for the operation (optional).
options: A TFRecordOptions object (optional).

初始化一个TFRecordWriter完成后,接下来就要往这个文件中读数据呀!这里用到了这个类的read函数。

read(self, queue, name=None)
Returns the next record (key, value) pair produced by a reader.

Will dequeue a work unit from queue if necessary (e.g. when the
Reader needs to start reading from a new file since it has
finished with the previous file).

Args:
queue: A Queue or a mutable string Tensor representing a handle
to a Queue, with string work items.
name: A name for the operation (optional). Returns: A tuple of Tensors (key, value). key: A string scalar Tensor. value: A string scalar Tensor.

当要读取整个文件的时候,也可以使用WholeFileReader这个阅读器。它是TensorFlow提供的一个类。
class tf.WholeFileReader

一个阅读器,读取整个文件,返回文件名称key,以及文件中所有的内容value。

创建阅读器之后,要从文件名队列中读取文件。

read(queue, name=None) method of tensorflow.python.ops.io_ops.WholeFileReader instance
Returns the next record (key, value) pair produced by a reader.

返回下一个文件名称key和文件中所有内容的value。

Will dequeue a work unit from queue if necessary (e.g. when the
Reader needs to start reading from a new file since it has
finished with the previous file).
如果需要,会从队列中出队一个单元。读取器在完成上一个文件后,继续读取下一个文件。

Args:
queue: A Queue or a mutable string Tensor representing a handle
to a Queue, with string work items.
name: A name for the operation (optional).

Returns:
A tuple of Tensors (key, value).
key: A string scalar Tensor.
value: A string scalar Tensor.

关于TensorFlow的读取机制:

转载于https://zhuanlan.zhihu.com/p/27238630

什么是数据读取?假设我们的硬盘中有一个图片数据集0001.jpg,0002.jpg,0003.jpg……我们只需要把它们读取到内存中,然后提供给GPU或是CPU进行计算。

tensorFlow

读取线程源源不断地将文件系统中的图片读入到一个内存的队列中,而负责计算的是另一个线程,计算需要数据时,直接从内存队列中取就可以了。这样就可以解决GPU因为IO而空闲的问题!

 而在tensorflow中,为了方便管理,在内存队列前又添加了一层所谓的“文件名队列”。

为什么要添加这一层文件名队列?我们首先得了解机器学习中的一个概念:epoch。对于一个数据集来讲,运行一个epoch就是将这个数据集中的图片全部计算一遍。

tensorFlow

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
2年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Karen110 Karen110
2年前
​一篇文章总结一下Python库中关于时间的常见操作
前言本次来总结一下关于Python时间的相关操作,有一个有趣的问题。如果你的业务用不到时间相关的操作,你的业务基本上会一直用不到。但是如果你的业务一旦用到了时间操作,你就会发现,淦,到处都是时间操作。。。所以思来想去,还是总结一下吧,本次会采用类型注解方式。time包importtime时间戳从1970年1月1日00:00:00标准时区诞生到现在
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
2年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Stella981 Stella981
2年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k