4

I saw an example of training cifar10 data using tensorflow: https://github.com/tensorflow/models/tree/master/tutorials/image/cifar10

the code generate a batch of images from several single image using tf.train.batch and create a queue of batchs using prefetch_queue. I understand it is necessary to use queues to pre-fetch data when training data is large. I guess tf.train.batch maintains a queue internally (because it has a capacity parameter). Since a queue of batches is already maintained in tf.train.battch, is it necessary to create another queue with tf.contrib.slim.prefetch_queue? what does tf.contrib.slim.prefetch_queue do exactly?

the key parts of the cifar-10 example code is shown below:

import tensorflow as tf

images, labels = tf.train.batch(
    [image, label],
    batch_size=...,
    num_threads=...,
    capacity=...,
    min_after_dequeue=...)

batch_queue = tf.contrib.slim.prefetch_queue.prefetch_queue(
    [images, labels], 
    capacity=...)
Ao Li
  • 101
  • 5

2 Answers2

4

After a few hours of research, I think I can answer my own question.

tf.train.batch maintains a pool of single-frame images. When a new batch (with batch size n for example)is needed, tf.train.batch fetch n items from the pool and create a new batch.

prefetch_queue internally maintains a queue. It receives the batch created from tf.train.batch and put it in the queue.

The implemntation of prefetch_queue and tf.train.batch can be visualized via tensorboard.

Ao Li
  • 101
  • 5
0

I am not sure if you are right. According to the document here, tf.train.batch itself uses a QueueRunner to hold the data, that is to say, tf.train.batch could enqueue asynchronously, you could fetch your data from its queue directly without delay when you need, so why a prefetch_queue is essential if it only maintains a queue?

I am not very sure about what I said, just little advice, I am also study its mechanism these days. BTW, new API of tf.data.dataset may be a better choice with which we do not need to worry about its implementation.

cheerss
  • 86
  • 1
  • 6