Use ten images to illustrate the TensorFlow Data Reading mechanism (with code) and ten tensorflow
In the process of learning TensorFlow, many friends report that reading data is hard to understand. Indeed, this official tutorial is relatively simple, and no suitable learning materials can be found on the Internet. Today, this article explains the Data Reading mechanism of TensorFlow in the simplest language in the form of images. The actual code will be provided at the end of this Article for your reference.
Diagram of TensorFlow reading Mechanism
The first question to consider is: what is Data Reading? Taking image data as an example, the process of reading data can be used to represent:
Assume that we have an image data set 0001.jpg, 0002.jpg,0003.jpg ...... We only need to read them into the memory and then provide them to the GPU or CPU for computing. This sounds easy, but it is far from that simple. In fact, we have to read the data first before computing. If the read time is 0.1 s and the calculation time is 0.9 s, it means that every 1 s, the GPU will have s of nothing to do, this greatly reduces the computing efficiency.
How can this problem be solved? The method is to put the read data and computation in two separate threads and read the data into a queue in the memory, as shown in:
The reading thread continuously reads images from the file system into a memory queue, and is responsible for computing another thread. when data is required for computing, it can be directly retrieved from the memory queue. In this way, the GPU is idle due to IO!
In TensorFlow, a layer of so-called "file name queue" is added before the memory queue for convenient management ".
Why add this layer of file name queue? First, we need to understand a concept in Machine Learning: epoch. For a dataset, running an epoch is to compute all the images in the dataset. For example, if three images a.jpg and B .jpg are concentrated in A data set, running an epoch means that all three images A, B, and C are computed. Two epochs calculate A, B, and C each time, and then compute them all. That is to say, each image is calculated twice.
TensorFlow reads files in the form of file name queue + Memory Queue Dual queue, which can well manage epoch. The following uses an image to describe how this mechanism works. For example, if we want to run an epoch in the dataset a.jpg, B .jpg, and C.jpg, we put A, B, and C in the file name queue once, and mark the end of the queue later.
After the program runs, the memory queue first reads A (at this time, A queues from the file name Queue ):
Then read B and C in sequence:
At this point, if you try to read the data again, the system will automatically throw an exception (OutOfRange) because the system detects "end ). The program can be ended after the exception is caught externally. This is the basic mechanism for reading data in TensorFlow. If we want to run two epochs instead of one epoch, we only need to put A, B, and C in the file name queue two times and mark the end.
Functions of TensorFlow's Data Reading Mechanism
How can I create the two queues in TensorFlow?
For file name queues, we use the tf. train. string_input_producer function. This function needs to input a file name list, and the system will automatically convert it into a file name queue.
In addition, tf. train. string_input_producer has two important parameters: num_epochs, which is the number of epochs we mentioned above. The other is shuffle, which indicates whether the order of files in an epoch is disrupted. If shuffle = False is set, for example, in each epoch, the data enters the file name queue in the order of A, B, and C. This order will not change:
If shuffle is set to True, the Data Sequence in an epoch is disrupted, as shown in:
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, refer to the actual code below.
In addition to tf. train. string_input_producer, we also need to introduce an additional function: tf. train. start_queue_runners. Beginners often see this function in the code, but it is often difficult to understand its usefulness. Here, with the above preparations, we can explain the role of this function.
When we use tf. train. after string_input_producer creates a file name queue, the entire system is still in the "stuck" State. That is to say, our file names are not actually added to the queue (as shown in ). At this point, if we start computing, because there is nothing in the memory queue, the computing unit will remain waiting, resulting in the entire system being blocked.
When tf. train. start_queue_runners is used, the thread that fills the queue is started, and the system does not "stop ". After that, the computing unit can obtain and compute the data, and the entire program will run. This is the use of the function tf. train. start_queue_runners.
Actual code
Let's use a specific example to read data in TensorFlow ., Assume that we already have three images a.jpg and B .jpg in the front folder. We want to read these three images and store the read results in the read folder.
The corresponding code is as follows:
# Import TensorFlowimport TensorFlow as tf # create a new Sessionwith 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 file name queue 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 to initialize tf. local_variables_initializer (). run () # After start_queue_runners is used, the queue threads = tf is started. train. start_queue_runners (sess = sess) I = 0 while True: I + = 1 # retrieve image data and save image_data = sess. run (value) with open ('read/test_0000d.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 file name queue that will run five epochs. Reader reads an image and saves it each time.
After running the code, we can see the image in the read folder, Which is exactly five epochs in order:
If we set the shuffle = True in filename_queue = tf. train. string_input_producer (filename, shuffle = False, num_epochs = 5), the image in each epoch will be disrupted ,:
Here we only use three images as an example. In actual application, there must be more than three images in a dataset, but the principles involved are common.
Example: how to read images in tensorflow
The following describes how tensorflow reads images in jpg format. Images in png format are the same. There are two scenarios:
The first type is to read the image as an image and obtain the original data of the image before decoding. The main function used is tf. gfile. FastGFile, tf. image. decode_jpeg.
For example:
Import tensorflow as tf; image_raw_data = tf. gfile. fastGFile ('/home/penglu/Desktop/11.jpg '). read () image = tf. image. decode_jpeg (image_raw_data) # print image for image decoding. eval (session = tf. session ())
Output:
[[11 110]
[14 66 113]
[17 116]
...,
The second method is to look at the image as a file and read it in a queue.
For example:
Import tensorflow as tf; path = '/home/penglu/Desktop/11.jpg' file_queue = tf. train. string_input_producer ([path]) # create Input Queue image_reader = tf. wholeFileReader () _, image = image_reader.read (file_queue) image = tf. image. decode_jpeg (image) with tf. session () as sess: coord = tf. train. coordinator () # threads = tf. train. start_queue_runners (sess = sess, coord = coord) # Start the thread to run the print sess queue. run (image) coord. request_stop () # Stop all threads coord. join (threads)
Output:
[[11 110]
[14 66 113]
[17 116]
...,
Summary
This article describes the Data Reading mechanism of TensorFlow in detail in graphic mode, and finally provides the actual code, hoping to help you learn about TensorFlow. We also hope that you can support the customer's home.