TensorFlow CIFAR

编程入门 行业动态 更新时间:2024-10-28 04:24:34
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入门指南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变量仅包含一个带有图像及其高度,宽度和标签名称的记录。

然后该示例对读取的图像/记录应用一些变形,然后将其传递给_generate_image_and_label_batch()函数。

这个函数然后返回形状为[batch_size, 32, 32, 3] batch_size = 128 [batch_size, 32, 32, 3]的4D张量,其中batch_size = 128 。

上述函数在返回批处理时使用tf.train.shuffle_batch()函数。

我的问题是额外的记录来自tf.train.shuffle_batch()函数的位置? 我们没有传递任何文件名或读者对象。

有人可以从我们如何从1条记录转到128条记录? 我看了看文档,但不明白。

I was going through the CIFAR-10 example at TensorFlow getting started guide for CNN

Now in the train function in cifar10_train.py we get images as

images,labels = cifar10.distorted_inputs()
 

In the distorted_inputs() function we generate the filenames in a queue and then read a single record as

 # 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)
 

When I add debugging code, the read_input variable contains only 1 record with an image and its height, width, and label name.

The example then applies some distortion to the read image/record and then passes it to the _generate_image_and_label_batch() function.

This function then returns a 4D Tensor of shape [batch_size, 32, 32, 3] where batch_size = 128.

The above function utilizes the tf.train.shuffle_batch() function when returns the batch.

My question is where do the extra records come from in the tf.train.shuffle_batch() function? We are not passing it any filename or reader object.

Can someone shed some light on how we go from 1 record to 128 records? I looked into the documentation but didn't understand.

最满意答案

tf.train.shuffle_batch()函数可用于生成包含一批输入的(一个或多个)张量。 在内部, tf.train.shuffle_batch()创建一个tf.RandomShuffleQueue ,它在图像和标签张量上调用q.enqueue()来排队单个元素(图像 - 标签对)。 然后它返回q.dequeue_many(batch_size)的结果,它将batch_size随机选择的元素(图像标签对)连接成一批图像和一批标签。

请注意,尽管它看起来像read_input和filename_queue这样的代码有函数关系,但还有一个额外的read_input 。 简单评估tf.train.shuffle_batch()的结果将永远阻塞,因为没有元素被添加到内部队列中。 为了简化这一点,当您调用tf.train.shuffle_batch() ,TensorFlow会将QueueRunner添加到图中的内部集合中。 稍后调用tf.train.start_queue_runners() (例如,在cifar10_train.py )将启动一个向队列中添加元素的线程,并允许继续进行训练。 线程和队列HOWTO有更多关于它如何工作的信息。

The 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), which concatenates batch_size randomly selected elements (image-label pairs) into a batch of images and a batch of labels.

Note that, although it looks from the code like read_input and filename_queue have a functional relationship, there is an additional wrinkle. Simply evaluating the result of tf.train.shuffle_batch() will block forever, because no elements have been added to the internal queue. To simplify this, when you call tf.train.shuffle_batch(), TensorFlow will add a 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 has more information on how this works.

更多推荐

本文发布于:2023-04-29 10:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TensorFlow   CIFAR

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!