10 pictures to help you get the TensorFlow data reading mechanism

In the process of learning tensorflow, many friends reported that reading data is difficult to understand. It is true that the official tutorial for this piece is relatively simple, and there are no suitable learning materials on the Internet. Today's article will explain tensorflow's data reading mechanism in the simplest language in the form of pictures. At the end of the article, the actual combat code will be given for reference.

1. Diagram of tensorflow reading mechanism

One of the first questions to think about is, what is data reading? Taking image data as an example, the process of reading data can be represented by the following figure:

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

Suppose we have a picture data set 0001.jpg, 0002.jpg, 0003.jpg in our hard disk... We only need to read them into the memory, and then provide them to the GPU or CPU for calculation. It sounds easy, but it's far from simple. In fact, we have to read the data first before we can calculate it. Assuming that it takes 0.1s to read and 0.9s to calculate, it means that every 1s, the GPU will have nothing to do for 0.1s, which greatly reduces the operational efficiency.

how to solve this problem? The method is to place the read data and calculation in two threads respectively, and read the data into a queue in the memory, as shown in the following figure:

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

The reading thread continuously reads the pictures in the file system into a memory queue, and another thread is responsible for the calculation. When the calculation needs data, it can be directly fetched from the memory queue. In this way, the problem of GPU being idle due to IO can be solved!

In tensorflow, in order to facilitate management, a so-called "file name queue" is added in front of the memory queue.

Why was this layer of filename queue added? We first have to understand a concept in machine learning: epoch. For a data set, running an epoch is to calculate all the pictures in the data set. For example, if there are three pictures A.jpg, B.jpg, and C.jpg in a data set, running one epoch means calculating all three pictures A, B, and C. Two epochs means that A, B, and C are calculated once, and then all are calculated again, that is to say, each picture is calculated twice.

Tensorflow reads files in the form of file name queue + memory queue double queue, which can manage epoch well. Below we use pictures to illustrate how this mechanism works. As shown in the figure below, we still take the data sets A.jpg, B.jpg, and C.jpg as an example. Suppose we want to run an epoch, then we put A, B, and C in the file name queue once, and then Callout queue ends.

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

After the program runs, the memory queue first reads A (at this time A is dequeued from the file name queue):

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

Then read B and C in turn:

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

At this point, if you try to read in again, the system will automatically throw an exception (OutOfRange) because it has detected "end". After the exception is caught externally, the program can be ended. This is the basic mechanism of reading data in tensorflow. If we want to run 2 epochs instead of 1 epoch, then just put A, B, and C in the file name queue twice in turn and mark the end.

2. The corresponding function of tensorflow reading data mechanism

How to create the above two queues in tensorflow?

For the filename queue, we use the tf.train.string_input_producer function. This function needs to pass in a filename list, and the system will automatically convert it into a filename queue.

In addition, tf.train.string_input_producer has two important parameters, one is num_epochs, which is the number of epochs we mentioned above. The other is shuffle, which refers to whether the order of files in an epoch is disrupted. If shuffle=False is set, as shown in the figure below, in each epoch, the data still enters the file name queue in the order of A, B, and C, and this order will not change:

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

If shuffle=True is set, then within an epoch, the order of the data will be disrupted, as shown in the following figure:

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

In tensorflow, the memory queue does not need to be created by ourselves. We only need to use the reader object to read data from the file name queue. For specific implementation, please refer to the actual combat code below.

In addition to tf.train.string_input_producer, we also introduce an additional function: tf.train.start_queue_runners. Beginners will often see this function in the code, but it is often difficult to understand its usefulness. Here, with the above foreshadowing, we can explain the function of this function.

After we use tf.train.string_input_producer to create the file name queue, the whole system is actually in a "stagnation state", that is to say, our file name has not really been added to the queue (as shown in the figure below). If we start computing at this time, because there is nothing in the memory queue, the computing unit will wait forever, causing the entire system to be blocked.

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

After using tf.train.start_queue_runners, the threads that fill the queue will be started, and the system will no longer "stagnate". After that, the calculation unit can get the data and perform calculations, and the whole program will run. This is what the function tf.train.start_queue_runners is for.

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

3. Actual code

We use a specific example to feel the data reading in tensorflow. As shown in the figure, suppose we already have three pictures A.jpg, B.jpg, and C.jpg in the current folder. We want to read these three pictures for 5 epochs and re-save the read results to the read folder. middle.

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

The corresponding code is as follows:

# import tensorflow
import tensorflow as tf 

# Create a new session
with tf.Session() as sess:
    # We want to read three images A.jpg, B.jpg, C.jpg
    filename = ['A.jpg', 'B.jpg', 'C.jpg']
    # string_input_producer will generate a queue of file names
    filename_queue = tf.train.string_input_producer(filename, shuffle=False, num_epochs=5)
    # reader reads data from the file name queue. The corresponding method is reader.read
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
    # tf.train.string_input_producer defines an epoch variable, which needs to be initialized
    tf.local_variables_initializer().run()
    # After using start_queue_runners, the queue will start to be filled
    threads = tf.train.start_queue_runners(sess=sess)
    i = 0
    while True:
        i += 1
        # Get image data and save it
        image_data = sess.run(value)
        with open('read/test_%d.jpg' % i, 'wb') as f:
            f.write(image_data)

Here we use filename_queue = tf.train.string_input_producer(filename, shuffle=False, num_epochs=5) to create a filename queue that will run 5 epochs. And use the reader to read, the reader reads and saves one picture at a time.

After running the code, we can see the pictures in the read folder, exactly 5 epochs in sequence:

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

If we set shuffle=True in filename_queue = tf.train.string_input_producer(filename, shuffle=False, num_epochs=5), then the image will be scrambled in each epoch, as shown in the figure:

10 pictures to help you get the TensorFlow data reading mechanism 10 pictures to help you get the TensorFlow data reading mechanism

Here we just use three pictures as an example. In practical applications, a data set must have more than three pictures, but the principles involved are common.

Four. Summary

This article mainly introduces the mechanism of tensorflow reading data in detail in a graphical way, and finally gives the corresponding actual combat code, hoping to bring some substantial help to everyone learning tensorflow. If you guys have any questions, please comment or private message me, thank you~

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/132245391