zoukankan      html  css  js  c++  java
  • Tensorflow读取文件到队列文件

    TensorFlow读取二进制文件数据到队列
    2016-11-03 09:30:00      0个评论    来源:diligent_321的博客  
    收藏   我要投稿

    TensorFlow是一种符号编程框架(与theano类似),先构建数据流图再输入数据进行模型训练。Tensorflow支持很多种样例输入的方式。最容易的是使用placeholder,但这需要手动传递numpy.array类型的数据。第二种方法就是使用二进制文件和输入队列的组合形式。这种方式不仅节省了代码量,避免了进行data augmentation和读文件操作,可以处理不同类型的数据, 而且也不再需要人为地划分开“预处理”和“模型计算”。在使用TensorFlow进行异步计算时,队列是一种强大的机制。

    队列使用概述

    正如TensorFlow中的其他组件一样,队列就是TensorFlow图中的节点。这是一种有状态的节点,就像变量一样:其他节点可以修改它的内容。具体来说,其他节点可以把新元素插入到队列后端(rear),也可以把队列前端(front)的元素删除。队列,如FIFOQueue和RandomShuffleQueue(A queue implementation that dequeues elements in a random order.)等对象,在TensorFlow的tensor异步计算时都非常重要。例如,一个典型的输入结构是使用一个RandomShuffleQueue来作为模型训练的输入,多个线程准备训练样本,并且把这些样本压入队列,一个训练线程执行一个训练操作,此操作会从队列中移除最小批次的样本(mini-batches),这种结构具有许多优点。

    TensorFlow的Session对象是可以支持多线程的,因此多个线程可以很方便地使用同一个会话(Session)并且并行地执行操作。然而,在Python程序实现这样的并行运算却并不容易。所有线程都必须能被同步终止,异常必须能被正确捕获并报告,会话终止的时候, 队列必须能被正确地关闭。所幸TensorFlow提供了两个类来帮助多线程的实现:tf.Coordinator和 tf.QueueRunner。从设计上这两个类必须被一起使用。Coordinator类可以用来同时停止多个工作线程并且向那个在等待所有工作线程终止的程序报告异常。QueueRunner类用来协调多个工作线程同时将多个tensor压入同一个队列中。

    (1)读二进制文件数据到队列中

    同很多其他的深度学习框架一样,TensorFlow有它自己的二进制格式。它使用了a mixture of its Records 格式和protobuf。Protobuf是一种序列化数据结构的方式,给出了关于数据的一些描述。TFRecords是tensorflow的默认数据格式,一个record就是一个包含了序列化tf.train.Example 协议缓存对象的二进制文件,可以使用python创建这种格式,然后便可以使用tensorflow提供的函数来输入给机器学习模型。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    import tensorflow as tf
     
    def read_and_decode_single_example(filename_queue):
    # 定义一个空的类对象,类似于c语言里面的结构体定义
        class Image(self):
        pass
        image = Image()
        image.height = 32
        image.width = 32
        image.depth = 3
        label_bytes = 1
         
        Bytes_to_read = label_bytes+image.heigth*image.width*3
        # A Reader that outputs fixed-length records from a file
        reader = tf.FixedLengthRecordReader(record_bytes=Bytes_to_read)
        # Returns the next record (key, value) pair produced by a reader, key 和value都是字符串类型的tensor
        # 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).
        image.key, value_str = reader.read(filename_queue)
        # Reinterpret the bytes of a string as a vector of numbers,每一个数值占用一个字节,在[0, 255]区间内,因此out_type要取uint8类型
        value = tf.decode_raw(bytes=value_str, out_type=tf.uint8)
        # Extracts a slice from a tensor, value中包含了label和feature,故要对向量类型tensor进行'parse'操作
        image.label = tf.slice(input_=value, begin=[0], size=[1])
        value = value.slice(input_=value, begin=[1], size=[-1]).reshape((image.depth, image.height, image.width))
        transposed_value = tf.transpose(value, perm=[2, 0, 1])
        image.mat = transposed_value
        return image
    接下来我们便可以调用这个函数了,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    filenames =[os.path.join(data_dir, 'test_batch.bin')]
    # Output strings (e.g. filenames) to a queue for an input pipeline
    filename_queue = tf.train.string_input_producer(string_tensor=filenames)
    # returns symbolic label and image
    img_obj = read_and_decode_single_example("filename_queue")
    Label = img_obj.label
    Image = img_obj.mat
    sess = tf.Session()
    # 初始化tensorflow图中的所有状态,如待读取的下一个记录tfrecord的位置,variables等
    init = tf.initialize_all_variables()
    sess.run(init)
    tf.train.start_queue_runners(sess=sess)
    # grab examples back.
    # first example from file
    label_val_1, image_val_1 = sess.run([label, image])
    # second example from file
    label_val_2, image_val_2 = sess.run([label, image])
    值得一提的是,TFRecordReader总是作用于文件名队列。它将会从队列中弹出文件名并使用该文件名,直到tfrecord为空时停止,此时它将从文件名队列中弹出下一个filename。然而,文件名队列又是怎么得来的呢?起初这个队列是空的,QueueRunners的概念即源于此。QueueRunners本质上就是一个线程thread,这个线程负责使用会话session并不断地调用enqueue操作。Tensorflow把这个模式封装在tf.train.QueueRunner对象里面。入队列操作99%的时间都可以被忽略掉,因为这个操作是由后台负责运行。(比如在上面的例子中,tf.train.string_input_producer创建了一个这样的线程,添加QueueRunner到数据流图中)。

    可想而知,在你运行任何训练步骤之前,我们要告知tensorflow去启动这些线程,否则这些队列会因为等待数据入队而被堵塞,导致数据流图将一直处于挂起状态。我们可以调用tf.train.start_queue_runners(sess=sess)来启动所有的QueueRunners。这个调用并不是符号化的操作,它会启动输入管道的线程,填充样本到队列中,以便出队操作可以从队列中拿到样本。另外,必须要先运行初始化操作再创建这些线程。如果这些队列未被初始化,tensorflow会抛出错误。

    (2)从二进制文件中读取mini-batchs

    在训练机器学习模型时,使用单个样例更新参数属于“online learning”,然而在线下环境下,我们通常采用基于mini-batchs 随机梯度下降法(SGD),但是在tensorflow中如何利用queuerunners返回训练块数据呢?请参见下面的程序:
    1
    2
    3
    4
    5
    image_batch, label_batch = tf.train.shuffle_batch(tensor_list=[image, label]],
                                                      batch_size=batch_size,
                                                      num_threads=24,
                                                      min_after_dequeue=min_samples_in_queue,
                                                      capacity=min_samples_in_queue+3*batch_size)
    读取batch数据需要使用新的队列queues和QueueRunners(大致流程图如下)。Shuffle_batch构建了一个RandomShuffleQueue,并不断地把单个的(image,labels)对送入队列中,这个入队操作是通过QueueRunners启动另外的线程来完成的。这个RandomShuffleQueue会顺序地压样例到队列中,直到队列中的样例个数达到了batch_size+min_after_dequeue个。它然后从队列中选择batch_size个随机的元素进行返回。事实上,shuffle_batch返回的值就是RandomShuffleQueue.dequeue_many()的结果。有了这个batches变量,就可以开始训练机器学习模型了。

    函数 tf.train.shuffle_batch(tensor_list, batch_size, capacity, min_after_dequeue, num_threads=1, seed=None, enqueue_many=False, shapes=None, shared_name=None, name=None)的使用说明:
    作用:Creates batches by randomly shuffling tensors.(从队列中随机筛选多个样例返回给image_batch和label_batch);
    参数说明:
    tensor_list: The list of tensors to enqueue.(待入队的tensor list);
    batch_size: The new batch size pulled from the queue;
    capacity: An integer. The maximum number of elements in the queue(队列长度);
    min_after_dequeue: Minimum number elements in the queue after a dequeue, used to ensure a level of mixing of elements.(随机取样的样本总体最小值,用于保证所取mini-batch的随机性);
    num_threads: The number of threads enqueuing `tensor_list`.(session会话支持多线程,这里可以设置多线程加速样本的读取)
    seed: Seed for the random shuffling within the queue.
    enqueue_many: Whether each tensor in `tensor_list` is a single example.(为False时表示tensor_list是一个样例,压入时占用队列中的一个元素;为True时表示tensor_list中的每一个元素都是一个样例,压入时占用队列中的一个元素位置,可以看作为一个batch);
    shapes: (Optional) The shapes for each example. Defaults to the inferred shapes for `tensor_list`.
    shared_name: (Optional) If set, this queue will be shared under the given name across multiple sessions.

    name: (Optional) A name for the operations.

  • 相关阅读:
    抽象工厂设计模式【看了挺多的,还是这个最清楚】
    js1:对象的学习,构造函数,继承构造函数【使用教材:JavaScript深度剖析第2版】
    如何用DW设计界面 结合 VS设计后台代码
    初识微服务架构
    Mysql分库分表方案
    真实经历,互联网大厂升职加薪那些事
    fastjson数据格式转换 SerializerFeature属性详解
    基于Spring Boot和Spring Cloud实现微服务架构学习
    MySQL中tinytext、text、mediumtext和longtext详解
    下载知乎视频
  • 原文地址:https://www.cnblogs.com/jyxbk/p/7771376.html
Copyright © 2011-2022 走看看