如何从示例队列中将数据读入TensorFlow批处理?

Joh*_*len 18 python numpy classification tensorflow

如何将TensorFlow示例队列分成适当的批次进行培训?

我有一些图像和标签:

IMG_6642.JPG 1
IMG_6643.JPG 2
Run Code Online (Sandbox Code Playgroud)

(随意建议另一种标签格式;我想我可能需要另一个密集到稀疏的步骤......)

我已经阅读了不少教程,但还没有完全掌握.这就是我所拥有的,其中的注释表明了TensorFlow的阅读数据页面所需的步骤.

  1. 文件名列表(为简单起见,删除了可选步骤)
  2. 文件名队列
  3. 用于文件格式的Reader
  4. 用于读取器读取的记录的解码器
  5. 示例队列

在示例队列之后,我需要将此队列分批进行培训; 那就是我被困的地方......

1.文件名列表

files = tf.train.match_filenames_once('*.JPG')

4.文件名队列

filename_queue = tf.train.string_input_producer(files, num_epochs=None, shuffle=True, seed=None, shared_name=None, name=None)

读者

reader = tf.TextLineReader() key, value = reader.read(filename_queue)

6.解码器

record_defaults = [[""], [1]] col1, col2 = tf.decode_csv(value, record_defaults=record_defaults) (我认为我不需要下面的这一步,因为我已经将我的标签放在一个张量中,但我仍然包含它)

features = tf.pack([col2])

文档页面有一个运行一个图像的示例,而不是将图像和标签分成批次:

for i in range(1200): # Retrieve a single instance: example, label = sess.run([features, col5])

然后它下面有一个批处理部分:

def read_my_file_format(filename_queue):
  reader = tf.SomeReader()
  key, record_string = reader.read(filename_queue)
  example, label = tf.some_decoder(record_string)
  processed_example = some_processing(example)
  return processed_example, label

def input_pipeline(filenames, batch_size, num_epochs=None):
  filename_queue = tf.train.string_input_producer(
  filenames, num_epochs=num_epochs, shuffle=True)
  example, label = read_my_file_format(filename_queue)
  # min_after_dequeue defines how big a buffer we will randomly sample
  #   from -- bigger means better shuffling but slower start up and more
  #   memory used.
  # capacity must be larger than min_after_dequeue and the amount larger
  #   determines the maximum we will prefetch.  Recommendation:
  #   min_after_dequeue + (num_threads + a small safety margin) *              batch_size
  min_after_dequeue = 10000
  capacity = min_after_dequeue + 3 * batch_size
  example_batch, label_batch = tf.train.shuffle_batch(
  [example, label], batch_size=batch_size, capacity=capacity,
  min_after_dequeue=min_after_dequeue)
  return example_batch, label_batch
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何将上面的示例代码与上面的代码一起使用? 我需要批量处理,大多数教程已经有mnist批次.

with tf.Session() as sess:
  sess.run(init)

  # Training cycle
for epoch in range(training_epochs):
    total_batch = int(mnist.train.num_examples/batch_size)
    # Loop over all batches
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
Run Code Online (Sandbox Code Playgroud)

小智 14

如果您希望使此输入管道正常工作,则需要添加一个生成批量示例的异步队列机制.这是通过创建tf.RandomShuffleQueuetf.FIFOQueue插入已经读取,解码和预处理的JPEG图像来执行的.

您可以使用将生成队列的方便构造以及用于通过tf.train.shuffle_batch_join或运行队列的相应线程tf.train.batch_join.这是一个简化的例子.请注意,此代码未经测试:

# Let's assume there is a Queue that maintains a list of all filenames
# called 'filename_queue'
_, file_buffer = reader.read(filename_queue)

# Decode the JPEG images
images = []
image = decode_jpeg(file_buffer)

# Generate batches of images of this size.
batch_size = 32

# Depends on the number of files and the training speed.
min_queue_examples = batch_size * 100
images_batch = tf.train.shuffle_batch_join(
  image,
  batch_size=batch_size,
  capacity=min_queue_examples + 3 * batch_size,
  min_after_dequeue=min_queue_examples)

# Run your network on this batch of images.
predictions = my_inference(images_batch)
Run Code Online (Sandbox Code Playgroud)

根据您需要扩展作业的方式,您可能需要运行多个独立的线程来读取/解码/预处理图像并将它们转储到示例队列中.Inception/ImageNet模型中提供了此类管道的完整示例.看看batch_inputs:

https://github.com/tensorflow/models/blob/master/inception/inception/image_processing.py#L407

最后,如果您使用> O(1000)JPEG图像,请记住单独准备1000个小文件效率极低.这会使你的训练减慢很多.

一种更强大,更快速的解决方案,可将图像数据集转换为分片TFRecordExample原型.这是一个完整的脚本,用于将ImageNet数据集转换为这种格式.这里有一组指令,用于在包含JPEG图像的任意目录上运行此预处理脚本的通用版本.