欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

关于tf.TFRecordReader()函数的用法解析

时间:2021-04-23 09:28:39|栏目:Python代码|点击:

读取tfrecord数据

从TFRecords文件中读取数据, 首先需要用tf.train.string_input_producer生成一个解析队列。之后调用tf.TFRecordReader的tf.parse_single_example解析器。

如下图:

解析器首先读取解析队列,返回serialized_example对象,之后调用tf.parse_single_example操作将Example协议缓冲区(protocol buffer)解析为张量。

简单来说,一旦生成了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.int32)

  return img, label

举例:

下面代码是我的程序中利用TFRecord读取格式时的代码:

这部分只要使用对应的代码就可以,主要是知道咋回事。

上一篇:新手常见Python错误及异常解决处理方案

栏    目:Python代码

下一篇:python中报错"json.decoder.JSONDecodeError: Expecting value:"的解决

本文标题:关于tf.TFRecordReader()函数的用法解析

本文地址:http://www.codeinn.net/misctech/107032.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有