TensorFlow CIFAR-10 示例教程中的 distorted_inputs() 函数如何每批获取 128 张图像?
How does the distorted_inputs() function in the TensorFlow CIFAR-10 example tutorial get 128 images per batch?
我正在 TensorFlow getting started guide for CNN
查看 CIFAR-10 示例
现在在 cifar10_train.py 的训练函数中,我们得到图像
images,labels = cifar10.distorted_inputs()
在 distorted_inputs()
函数中,我们在队列中生成文件名,然后读取单个记录作为
# Create a queue that produces the filenames to read.
filename_queue = tf.train.string_input_producer(filenames)
# Read examples from files in the filename queue.
read_input = cifar10_input.read_cifar10(filename_queue)
reshaped_image = tf.cast(read_input.uint8image, tf.float32)
当我添加调试代码时,read_input
变量仅包含 1 条包含图像及其高度、宽度和标签名称的记录。
该示例然后对读取 image/record 应用了一些失真,然后将其传递给 _generate_image_and_label_batch()
函数。
此函数然后 returns 形状为 [batch_size, 32, 32, 3]
的 4D 张量,其中 batch_size = 128
。
以上函数在returns批处理时利用tf.train.shuffle_batch()
函数。
我的问题是 tf.train.shuffle_batch()
函数中的额外记录来自哪里?我们没有向它传递任何文件名或 reader 对象。
有人可以解释一下我们是如何从 1 条记录变成 128 条记录的吗?我查看了文档,但没看懂。
tf.train.shuffle_batch()
function can be used to produce (one or more) tensors containing a batch of inputs. Internally, tf.train.shuffle_batch()
creates a tf.RandomShuffleQueue
, on which it calls q.enqueue()
with the image and label tensors to enqueue a single element (image-label pair). It then returns the result of q.dequeue_many(batch_size)
,它将 batch_size
个随机选择的元素(image-label 对)连接成一批图像和一批标签。
请注意,虽然从代码看来 read_input
和 filename_queue
具有函数关系,但还有一个问题。简单地评估 tf.train.shuffle_batch()
的结果将永远阻塞,因为没有元素被添加到内部队列中。为简化这一点,当您调用 tf.train.shuffle_batch()
时,TensorFlow 将添加一个 QueueRunner
to an internal collection in the graph. A later call to tf.train.start_queue_runners()
(e.g. here in cifar10_train.py
) will start a thread that adds elements to the queue, and enables training to proceed. The Threading and Queues HOWTO,其中包含有关其工作原理的更多信息。
我正在 TensorFlow getting started guide for CNN
查看 CIFAR-10 示例现在在 cifar10_train.py 的训练函数中,我们得到图像
images,labels = cifar10.distorted_inputs()
在 distorted_inputs()
函数中,我们在队列中生成文件名,然后读取单个记录作为
# Create a queue that produces the filenames to read.
filename_queue = tf.train.string_input_producer(filenames)
# Read examples from files in the filename queue.
read_input = cifar10_input.read_cifar10(filename_queue)
reshaped_image = tf.cast(read_input.uint8image, tf.float32)
当我添加调试代码时,read_input
变量仅包含 1 条包含图像及其高度、宽度和标签名称的记录。
该示例然后对读取 image/record 应用了一些失真,然后将其传递给 _generate_image_and_label_batch()
函数。
此函数然后 returns 形状为 [batch_size, 32, 32, 3]
的 4D 张量,其中 batch_size = 128
。
以上函数在returns批处理时利用tf.train.shuffle_batch()
函数。
我的问题是 tf.train.shuffle_batch()
函数中的额外记录来自哪里?我们没有向它传递任何文件名或 reader 对象。
有人可以解释一下我们是如何从 1 条记录变成 128 条记录的吗?我查看了文档,但没看懂。
tf.train.shuffle_batch()
function can be used to produce (one or more) tensors containing a batch of inputs. Internally, tf.train.shuffle_batch()
creates a tf.RandomShuffleQueue
, on which it calls q.enqueue()
with the image and label tensors to enqueue a single element (image-label pair). It then returns the result of q.dequeue_many(batch_size)
,它将 batch_size
个随机选择的元素(image-label 对)连接成一批图像和一批标签。
请注意,虽然从代码看来 read_input
和 filename_queue
具有函数关系,但还有一个问题。简单地评估 tf.train.shuffle_batch()
的结果将永远阻塞,因为没有元素被添加到内部队列中。为简化这一点,当您调用 tf.train.shuffle_batch()
时,TensorFlow 将添加一个 QueueRunner
to an internal collection in the graph. A later call to tf.train.start_queue_runners()
(e.g. here in cifar10_train.py
) will start a thread that adds elements to the queue, and enables training to proceed. The Threading and Queues HOWTO,其中包含有关其工作原理的更多信息。