如何将TensorFlow示例队列分成适当的批次进行训练?
我有一些图像和标签:
IMG_6642.JPG 1
IMG_6643.JPG 2
(随意建议其他标签格式;我想可能需要另外一个密集的步骤来简化步骤…)
我已经阅读了很多教程,但还没有完全结合在一起。这就是我所拥有的,带有注释的注释指示TensorFlow的“读取数据”页面所需的步骤。
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)
5.读者
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
我的问题是:如何将上面的示例代码与上面的代码一起使用? 我需要使用批处理,大多数教程已经附带了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)
如果您希望使该输入管道正常工作,则需要添加一个异步队列机制来生成大量示例。这是通过创建atf.RandomShuffleQueue
或atf.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)
根据您需要扩展工作的方式,您可能需要运行多个独立的线程来读取/解码/预处理图像并将其转储到示例队列中。Inception / ImageNet模型中提供了此类管道的完整示例。看一下batch_inputs:
https://github.com/tensorflow/models/blob/master/inception/inception/image_processing.py#L407
最后,如果要使用> O(1000)JPEG图像,请记住,单独准备1000个小文件的效率极低。这会大大减慢您的训练速度。
将图像数据集转换TFRecord为Example原型碎片的更强大,更快速的解决方案。这是一个完全有效的脚本,用于将ImageNet数据集转换为这种格式。这是一组说明,用于在包含JPEG图像的任意目录上运行此预处理脚本的通用版本。