tensorflow data input

Two sessions in May | NVIDIA DLI Deep Learning Introductory Course

May 19/May 26 One- 640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1 day intensive study to get you started quickly Read more >


The text has a total of 3212 words, 1 picture, and the estimated reading time is 6 minutes.


tensorflow has two data input methods. The simpler one is to use feed_dict. This method uses placeholder to stand when drawing the graph, and passes the real input through the feed dictionary when it is actually running. It is relatively simple and will not be introduced.


What is more annoying is the second method, which reads data directly from the file (in fact, the first method can also be passed in using feed_dict after we read it from the file, but the second method tf provides a complete set of classes and functions to form a read line like a pipeline):


1. Use the tf.train.string_input_producer function to package all the files we need into a queue type inside tf, and then tf opens the file and takes the directory from this queue. It should be noted that the shuffle parameter of this function is True by default , that is, the file order you passed to him is 1234, but it is not necessarily the case when reading it. At the beginning, the samples for the first iteration of each run and training were different, and I wondered for a long time. This is the reason.


files_in = ["./data/data_batch%d.bin" % i for i in range(1, 6)]

files = tf.train.string_input_producer(files_in)


2. Build a reader. Different readers correspond to different file structures. For example, the bin file tf.FixedLengthRecordReader is better, because it reads a piece of data of equal length each time. If you want to read other structures, there are corresponding readers.


reader = tf.FixedLengthRecordReader(record_bytes=1+32*32*3)


3. Use the read method of the reader. This method requires an IO type parameter, which is the queue output by string_input_producer above. The reader takes a file directory from this queue, then opens it and reads it once. The return of the reader is A tensor (this is very important, the reading code we are writing now is not really reading data, or drawing a graph, which is the same as defining a neural network. At this time, the operation will not be executed before run, this return The tensor also has no value, it only represents a node in the graph).


key, value = reader.read(files)


4. Do some data and processing on this tensor. For example, the label and image data in CIFAR1-10 are mixed together. Here, slice them are used to cut them into two tensors (note that these two tensors are corresponding, one The image is paired with a label, and the training is over after the pair is crossed), and then data augmentation is performed on the tensor of the image.


data = tf.decode_raw(value, tf.uint8)
label = tf.cast(tf.slice(data, [0], [1]), tf.int64)
raw_image = tf.reshape(tf.slice(data, [1], [32*32*3]), [3, 32, 32])
image = tf.cast(tf.transpose(raw_image, [1, 2, 0]), tf.float32)

lr_image = tf.image.random_flip_left_right(image)
br_image = tf.image.random_brightness(lr_image, max_delta=63)
rc_image = tf.image.random_contrast(br_image, lower=0.2, upper=1.8)

std_image = tf.image.per_image_standardization(rc_image)


5. At this time, it can be found that this tensor represents a sample ([height and width pipeline]), but the input when training the network is generally a push sample ([sample number height and width * pipeline]), we will use The tf.train.batch or tf.train.shuffle_batch function packs a small sample of tensors into a high-dimensional sample batch. The input of these functions is a single sample, and the output is a 4D sample batch. The internal principle seems to be It creates a queue, and then continuously calls your single-sample tensor to obtain samples until there are enough samples in the queue, and then returns a bunch of samples at a time to form a sample batch.


images, labels = tf.train.batch([std_image, label],
                          batch_size=100,
                          num_threads=16,

                          capacity=int(50000* 0.4 + 3 * batch_size))


5.事实上一直到上一部的images这个tensor,都还没有真实的数据在里边,我们必须用Session run一下这个4D的tensor,才会真的有数据出来。这个原理就和我们定义好的神经网络run一下出结果一样,你一run这个4D tensor,他就会顺着自己的operator找自己依赖的其他tensor,一路最后找到最开始reader那里。


除了上边讲的原理,其中还要注意几点:


1.tf.train.start_queue_runners(sess=sess)这一步一定要运行,且其位置要在定义好读取graph之后,在真正run之前,其作用是把queue里边的内容初始化,不跑这句一开始string_input_producer那里就没用,整个读取流水线都没用了。


training_images = tf.train.batch(XXXXXXXXXXXXXXX)
tf.train.start_queue_runners(sess=self.sess)

real_images = sess.run(training_images)


2.image和label一定要一起run,要记清楚我们的image和label是在一张graph里边的,跑一次那个graph,这两个tensor都会出结果,且同一次跑出来的image和label才是对应的,如果你run两次,第一次为了拿image第二次为了拿label,那整个就叉了,因为第一次跑出来第0到100号image和0到100号label,第二次跑出来第100到200的image和第100到200的label,你拿到了0100的image和100200的label,整个样本分类全不对,最后网络肯定跑不出结果。


training_images, training_labels = read_image()
tf.train.start_queue_runners(sess=self.sess)

real_images = sess.run(training_images) # 读出来是真的图片,但是和label对不上

real_labels = sess.run(training_labels) # 读出来是真的label,但是和image对不上

# 正确调用方法,通过跑一次graph,将成套的label和image读出来

real_images, real_labels = sess.run([training_images, training_labels])


因为不懂这个道理的up主跑了一下午正确率还是10%。。。。(10类别分类10%正确率不就是乱猜吗)


原文:【tensorflow的数据输入】(https://goo.gl/Ls2N7s)


原文链接:https://www.jianshu.com/p/7e537cd96c6f


查阅更为简洁方便的分类文章以及最新的课程、产品信息,请移步至全新呈现的“LeadAI学院官网”:

www.leadai.org


请关注人工智能LeadAI公众号,查看更多专业文章

640?wx_fmt=jpeg

大家都在看

640.png?

LSTM模型在问答系统中的应用

TensorFlow-based neural network solves the problem of user churn overview

The Most Common Algorithm Engineer Interview Questions (1)

Sorting out the most common algorithm engineer interview questions (2)

TensorFlow from 1 to 2 | Chapter 3 The Beginning of the Deep Learning Revolution: Convolutional Neural Networks

Decorators | Advanced Programming in Python

Why don't you review the basics of Python today?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326031579&siteId=291194637