Tensorflow 实现分批量读取数据

作者:freedom098 时间:2023-09-23 23:04:44 

之前的博客里使用tf读取数据都是每次fetch一条记录,实际上大部分时候需要fetch到一个batch的小批量数据,在tf中这一操作的明显变化就是tensor的rank发生了变化,我目前使用的人脸数据集是灰度图像,因此大小是92*112的,所以最开始fetch拿到的图像数据集经过reshape之后就是一个rank为2的tensor,大小是92*112的(如果考虑通道,也可以reshape为rank为3的,即92*112*1)。

如果加入batch,比如batch大小为5,那么拿到的tensor的rank就变成了3,大小为5*92*112。

下面规则化的写一下读取数据的一般流程,按照官网的实例,一般把读取数据拆分成两个大部分,一个是函数专门负责读取数据和解码数据,一个函数则负责生产batch。


import tensorflow as tf

def read_data(fileNameQue):

reader = tf.TFRecordReader()
key, value = reader.read(fileNameQue)
features = tf.parse_single_example(value, features={'label': tf.FixedLenFeature([], tf.int64),
             'img': tf.FixedLenFeature([], tf.string),})
img = tf.decode_raw(features["img"], tf.uint8)
img = tf.reshape(img, [92,112]) # 恢复图像原始大小
label = tf.cast(features["label"], tf.int32)

return img, label

def batch_input(filename, batchSize):

fileNameQue = tf.train.string_input_producer([filename], shuffle=True)
img, label = read_data(fileNameQue) # fetch图像和label
min_after_dequeue = 1000
capacity = min_after_dequeue+3*batchSize
# 预取图像和label并随机打乱,组成batch,此时tensor rank发生了变化,多了一个batch大小的维度
exampleBatch,labelBatch = tf.train.shuffle_batch([img, label],batch_size=batchSize, capacity=capacity,
             min_after_dequeue=min_after_dequeue)
return exampleBatch,labelBatch

if __name__ == "__main__":

init = tf.initialize_all_variables()
exampleBatch, labelBatch = batch_input("./data/faceTF.tfrecords", batchSize=10)

with tf.Session() as sess:

sess.run(init)
 coord = tf.train.Coordinator()
 threads = tf.train.start_queue_runners(coord=coord)

for i in range(100):
  example, label = sess.run([exampleBatch, labelBatch])
  print(example.shape)

coord.request_stop()
 coord.join(threads)

读取数据和解码数据与之前基本相同,针对不同格式数据集使用不同阅读器和解码器即可,后面是产生batch,核心是tf.train.shuffle_batch这个函数,它相当于一个蓄水池的功能,第一个参数代表蓄水池的入水口,也就是逐个读取到的记录,batch_size自然就是batch的大小了,capacity是蓄水池的容量,表示能容纳多少个样本,min_after_dequeue是指出队操作后还可以供随机采样出批量数据的样本池大小,显然,capacity要大于min_after_dequeue,官网推荐:min_after_dequeue + (num_threads + a small safety margin) * batch_size,还有一个参数就是num_threads,表示所用线程数目。

min_after_dequeue这个值越大,随机采样的效果越好,但是消耗的内存也越大。

来源:https://blog.csdn.net/freedom098/article/details/56013625

标签:Tensorflow,读取,数据
0
投稿

猜你喜欢

  • Python 函数简单易理解版

    2023-02-17 21:10:47
  • Go语言使用Gob传输数据

    2023-08-06 05:12:28
  • 网页栅格系统研究(3):粒度问题

    2008-10-28 19:46:00
  • 聊聊Python中关于a=[[]]*3的反思

    2021-09-08 05:12:46
  • Python工程师面试题 与Python基础语法相关

    2021-07-08 16:20:50
  • 最简单的tab切换实例代码

    2023-08-22 08:38:59
  • 压缩技术给SQL Server备份文件瘦身

    2024-01-26 05:10:23
  • Python多线程爬取豆瓣影评API接口

    2023-07-28 05:33:48
  • asp xml 缓存类

    2011-04-03 11:20:00
  • Python访问Redis的详细操作

    2022-03-13 13:22:25
  • 升级SQL Server 2008数据库引擎

    2009-03-25 12:58:00
  • python3判断url链接是否为404的方法

    2021-11-12 15:17:54
  • Python asyncio异步编程简单实现示例

    2023-09-23 15:27:52
  • 微信小程序顶部可滚动导航效果

    2024-04-29 13:55:34
  • php动态生成版权所有信息的方法

    2024-05-02 17:17:30
  • 使用pipenv管理python虚拟环境的全过程

    2021-08-26 13:05:55
  • python利用opencv实现颜色检测

    2022-05-08 14:20:58
  • Windows下MySQL 5.6安装及配置详细图解(大图版)

    2024-01-24 06:05:52
  • bootstrap手风琴制作方法详解

    2024-04-10 16:20:11
  • Python数据分析应用之Matplotlib数据可视化详情

    2023-08-28 07:15:31
  • asp之家 网络编程 m.aspxhome.com