TensorFlow------TFRecords的读取实例:

import os
import tensorflow as tf # 定义cifar的数据等命令行参数
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('cifar_dir', './data/cifar10/cifar-10-batches-bin', '文件的目录')
tf.app.flags.DEFINE_string('cifar_tfrecords', './tmp/cifar.tfrecords', '存储tfrecords的文件') class CifarRead(object):
'''
完成读取二进制文件,写进tfrecords,读取tfrecords
:param object:
:return:
''' def __init__(self, filelist):
# 文件列表
self.file_list = filelist # 定义读取的图片的一些属性
self.height = 32
self.width = 32
self.channel = 3
# 二进制文件每张图片的字节
self.label_bytes = 1
self.image_bytes = self.height * self.width * self.channel
self.bytes = self.label_bytes + self.image_bytes def read_and_decode(self):
# 1. 构建文件队列
file_queue = tf.train.string_input_producer(self.file_list) # 2. 构建二进制文件读取器,读取内容,每个样本的字节数
reader = tf.FixedLengthRecordReader(self.bytes) key, value = reader.read(file_queue) # 3. 解码内容,二进制文件内容的解码 label_image包含目标值和特征值
label_image = tf.decode_raw(value, tf.uint8)
print(label_image) # 4.分割出图片和标签数据,特征值和目标值
label = tf.slice(label_image, [0], [self.label_bytes]) image = tf.slice(label_image, [self.label_bytes], [self.image_bytes])
print('---->')
print(image) # 5. 可以对图片的特征数据进行形状的改变 [3072]-->[32,32,3]
image_reshape = tf.reshape(image, [self.height, self.width, self.channel]) print('======>')
print(label)
print('======>') # 6. 批处理数据
image_batch, label_batch = tf.train.batch([image_reshape, label], batch_size=10, num_threads=1, capacity=10) print(image_batch, label_batch) return image_batch, label_batch
# 读取并存储tfrecords文件
# def write_ro_tfrecords(self, image_batch, label_batch):
# '''
# 将图片的特征值和目标值存进tfrecords
# :param image_batch: 10张图片的特征值
# :param label_batch: 10张图片的目标值
# :return: None
# '''
# # 1.建立TFRecord存储器
# writer = tf.python_io.TFRecordWriter(FLAGS.cifar_tfrecords)
#
# # 2. 循环将所有样本写入文件,每张图片样本都要构造example协议
# for i in range(10):
# # 取出第i个图片数据的特征值和目标值
# image = image_batch[i].eval().tostring()
#
# label = int(label_batch[i].eval()[0])
#
# # 构造一个样本的example
# example = tf.train.Example(features=tf.train.Features(feature={
# 'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image])),
# 'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),
# }))
#
# # 写入单独的样本
# writer.write(example.SerializeToString())
#
# # 关闭
# writer.close()
# return None def read_from_tfrecords(self):
# 1. 构造文件队列
file_queue = tf.train.string_input_producer([FLAGS.cifar_tfrecords]) # 2. 构造文件阅读器,读取内容example,value一个样本的序列化example
reader = tf.TFRecordReader() key, value = reader.read(file_queue) # 3. 解析example
features = tf.parse_single_example(value, features={
'image': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
}) print(features['image'], features['label']) # 4. 解码内容,如果读取的内容格式是string需要解码,如果是int64,float32不需要解码
image = tf.decode_raw(features['image'], tf.uint8) # 固定图片的形状,方便与批处理
image_reshape = tf.reshape(image, [self.height, self.width, self.channel]) label = tf.cast(features['label'], tf.int32) print(image_reshape, label) # 进行批处理
image_batch,label_batch = tf.train.batch([image_reshape, label], batch_size=10, num_threads=1, capacity=10) return image_batch,label_batch if __name__ == '__main__':
# 找到文件,构建列表 路径+名字 ->列表当中
file_name = os.listdir(FLAGS.cifar_dir) # 拼接路径 重新组成列表
filelist = [os.path.join(FLAGS.cifar_dir, file) for file in file_name if file[-3:] == 'bin'] # 调用函数传参
cf = CifarRead(filelist)
# image_batch,label_batch = cf.read_and_decode() image_batch, label_batch = cf.read_from_tfrecords() # 开启会话
with tf.Session() as sess:
# 定义一个线程协调器
coord = tf.train.Coordinator() # 开启读文件的线程
threads = tf.train.start_queue_runners(sess, coord=coord) # 存进tfrecords文件
# print('开始存储')
# cf.write_ro_tfrecords(image_batch,label_batch)
# print('结束存储')
# 打印读取的内容
print(sess.run([image_batch,label_batch])) # 回收子线程
coord.request_stop() coord.join(threads)

TensorFlow------TFRecords的读取实例的更多相关文章

  1. tensorflow二进制文件读取与tfrecords文件读取

    1.知识点 """ TFRecords介绍: TFRecords是Tensorflow设计的一种内置文件格式,是一种二进制文件,它能更好的利用内存, 更方便复制和移动,为 ...

  2. 深度学习tensorflow实战笔记(2)图像转换成tfrecords和读取

    1.准备数据 首选将自己的图像数据分类分别放在不同的文件夹下,比如新建data文件夹,data文件夹下分别存放up和low文件夹,up和low文件夹下存放对应的图像数据.也可以把up和low文件夹换成 ...

  3. Tensorflow创建和读取17flowers数据集

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

  4. (第二章第三部分)TensorFlow框架之读取二进制数据

    系列博客链接: (第二章第一部分)TensorFlow框架之文件读取流程:https://www.cnblogs.com/kongweisi/p/11050302.html (第二章第二部分)Tens ...

  5. 关于Tensorflow 的数据读取环节

    Tensorflow读取数据的一般方式有下面3种: preloaded直接创建变量:在tensorflow定义图的过程中,创建常量或变量来存储数据 feed:在运行程序时,通过feed_dict传入数 ...

  6. TensorFlow笔记-图片读取

    回到上一篇文件的读取分这么几步: # 构造队列 # 1,构造图片文件的队列 file_queue = tf.train.string_input_producer(filelist) # 构造阅读器 ...

  7. Java学习-019-Properties 文件读取实例源代码

    在这几天的学习过程中,有开发的朋友告知我,每个编程语言基本都有相应的配置文件支持类,像 Python 编程语言中支持的 ini 文件及其对应的配置文件读取类 ConfigParse,通过这个类,用户可 ...

  8. Java学习-017-EXCEL 文件读取实例源代码

    众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...

  9. Java学习-016-CSV 文件读取实例源代码

    上文(CSV文件写入)讲述了日常自动化测试过程中将测试数据写入 CSV 文件的源码,此文主要讲述如何从 CSV 文件获取测试过程中所需的参数化数据.敬请各位小主参阅,若有不足之处,敬请大神指正,不胜感 ...

随机推荐

  1. %和format的区别

    在python中字符串的格式化分为两种:%和format.那么我们在什么时候来使用它们呢?它们有什么区别呢? 举个例子:我们根据一个坐标来表示一个动作 #定义一个坐标 point = (250,250 ...

  2. redis之(十五)redis的集群中的哨兵角色

    一:redis集群的哨兵的目的是什么?. (1)监控主redis和从redis数据库是否正常运行 (2)主redis出现故障,自动将其中一台从redis升级为主redis.将原先的主redis转变成从 ...

  3. AC日记——「HNOI2017」单旋 LiBreOJ 2018

    #2018. 「HNOI2017」单旋 思路: set+线段树: 代码: #include <bits/stdc++.h> using namespace std; #define max ...

  4. 五十四 网络编程 TCP编程

    Socket是网络编程的一个抽象概念.通常我们用一个Socket表示“打开了一个网络链接”,而打开一个Socket需要知道目标计算机的IP地址和端口号,再指定协议类型即可. 客户端 大多数连接都是可靠 ...

  5. Oracle 数据库分页查询的三种方法

    一.Oracle 数据库分页查询的三种方法 1.简介 不能对 rownum 使用 >(大于或等于 1 的数值).>=(大于 1 的数值).=(不等于 1 的数值),否则无结果.所以直接用 ...

  6. 再聊语言,模式,OOD

    今天与人再次聊到这个话题,有人在为"到底该用什么模式"而烦恼,我相信,每个都经历过这个阶段一定都会感觉很熟悉这个烦恼我认为, 模式不是目的,只是工具,达到设计目标的工具,我们不会因 ...

  7. python No migrations to apply

    错误显示:  “No migrations to apply” 错误情况:python在通过model同步数据库时,提示 No migrations to apply 查看数据库,新表没有被创建,只是 ...

  8. WHERE 子句中的标量子查询

    标量子查询不仅可以用在SELECT 语句的列表中,它还可以用在WHERE 子句中,而且实际应用中子查询很多的时候都是用在WHERE子句中的. 先来看一个简单的例子,我们要检索喜欢“Story”的读者主 ...

  9. Socket读取页面

    http://www.knowsky.com/363189.html http://hi.baidu.com/myyers/item/f90fa3f57d89e1d243c36a34 http://h ...

  10. xtuoj 1233 coins(dp)

    Coins Accepted : 120   Submit : 305 Time Limit : 1000 MS   Memory Limit : 65536 KB Coins Problem Des ...