TensorFlow How to make your own dataset _tensorflow

Source: Internet
Author: User

With so long a tensorflow, the example ran n many, the basic flow is clear. But to do a small example of their own independence has come all sorts of questions. Like your own picture data set how to do. To tell the truth, TensorFlow is really annoying, the management of the document tutorial old mnist and cifar_10 this good data set said, for us beginners, completely do not know how to enter the picture. Today to share my tensorflow to make the data set of the learning process. &NBSP
The process is: make the DataSet-read the dataset-join the queue  
Paste the complete code first:

#encoding =utf-8 import OS import tensorflow as TF from pil import Image CWD = OS.GETCWD () classes = {' Test ', ' test1 ', ' tes T2 '} #制作二进制数据 def Create_record (): writer = Tf.python_io.
        Tfrecordwriter ("Train.tfrecords") for index, name in enumerate (classes): Class_path = CWD + "/" + name+ "/" For Img_name in Os.listdir (class_path): Img_path = Class_path + img_name img = Image.open (img_p ATH) img = img.resize (()) Img_raw = Img.tobytes () #将图片转化为原生bytes print index,img _raw example = Tf.train.Example (Features=tf.train.features (feature={) Labe L ": Tf.train.Feature (Int64_list=tf.train.int64list (Value=[index)), ' Img_raw ': Tf.train.Feature (Bytes_ List=tf.train.byteslist (Value=[img_raw]))) Writer.write (example. Serializetostring ()) writer.close () data = Create_record () #读取二进制数据 def read_and_decode (filename): # Createfile queues, unlimited read quantity filename_queue = Tf.train.string_input_producer ([filename]) # Create a reader from file queue rea Der = tf. Tfrecordreader () # Reader reads a serialized sample from the file queue _, Serialized_example = Reader.read (filename_queue) # Get feature fr Om Serialized Example # Parse the Symbolic sample features = Tf.parse_single_example (Serialized_example, features= {' label ': TF. Fixedlenfeature ([], Tf.int64), ' Img_raw ': TF. Fixedlenfeature ([], tf.string)}) label = features[' label '] img = features[' img_raw '] img = tf.de Code_raw (IMG, tf.uint8) img = Tf.reshape (IMG, [$ 3]) img = Tf.cast (IMG, tf.float32) * (1./255)-0.5 L Abel = tf.cast (label, TF.INT32) return IMG, label if __name__ = = ' __main__ ': if 0:data = Create_record ("
        Train.tfrecords ") else:img, label = Read_and_decode (" train.tfrecords ") print" tengxing ", Img,label
        #使用shuffle_batch可以随机打乱输入 Next_batch down next to each other.# Shuffle_batch to achieve [Img,label] synchronization, that is, the feature and label synchronization, or the possible input of features and labels do not match # For example, only in this way can the IMG and label one by one correspond, each fetch an image and corresponding lab The value returned by El # Shuffle_batch is the result of Randomshufflequeue.dequeue_many () # Shuffle_batch constructs a randomshufflequeue and keeps the single
                                                    [Img,label], fed into the queue img_batch, Label_batch = Tf.train.shuffle_batch ([img, label], Batch_size=4, capacity=2000, min_after_dequeue=100 0) # initializes all op init = Tf.initialize_all_variables () with TF. Session () as Sess:sess.run (init) # start Queue threads = Tf.train.start_queue_runners (sess=s ESS) for I in range (5): Print Img_batch.shape,label_batch val, l = Sess.run ([i
 Mg_batch, Label_batch]) # L = to_categorical (L, m) print (Val.shape, L)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 4-45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62-63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 1-2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 A. 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76-77 78 79 80 production DataSet
 #制作二进制数据 def create_record (): CWD = OS.GETCWD () classes = {' 1 ', ' 2 ', ' 3 '} writer = tf.python_io.
        Tfrecordwriter ("Train.tfrecords") for index, name in enumerate (classes): Class_path = CWD + "/" + name+ "/" For Img_name in Os.listdir (class_path): Img_path = Class_path + img_name img = Image.open (img_p ATH) img = img.resize (()) Img_raw = Img.tobytes () #将图片转化为原生bytes #print Index,im
                        G_raw example = Tf.train.Example (Features=tf.train.features (feature={ "Label": Tf.train.Feature (Int64_list=tf.train.int64list (Value=[index)), ' IM
            G_raw ': Tf.train.Feature (Bytes_list=tf.train.byteslist (Value=[img_raw))}) ) Writer.write (example. Serializetostring ()) Writer.close () 
The 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1 2 3 4 5 6 7 The 8, the, the, and the 9, the-

The Tfrecords file contains the Tf.train.Example Protocol memory block (protocol buffer) (the Protocol memory block contains field Features). We can write a code to get your data, populate the example Protocol memory block (protocol buffer), serialize the protocol memory block into a string, and pass the Tf.python_io. Tfrecordwriter writes to the Tfrecords file. Reading data sets

#读取二进制数据
def read_and_decode (filename):
    # Create file queues, unlimited number of reads
    Filename_queue = Tf.train.string_input_ Producer ([filename])
    # Create a reader from file queue
    reader = tf. Tfrecordreader ()
    # Reader reads a serialized sample from the file queue
    _, Serialized_example = Reader.read (filename_queue)
    # get Feature from serialized example
    # Parse Symbolic sample
    features = Tf.parse_single_example (
        serialized_example,
        features={
            ' label ': TF. Fixedlenfeature ([], Tf.int64),
            ' Img_raw ': TF. Fixedlenfeature ([], tf.string)
        }
    )
    label = features[' label ']
    img = features[' img_raw ']
    img = Tf.decode_raw (img, tf.uint8)
    img = Tf.reshape (IMG, [$ 3])
    img = Tf.cast (IMG, tf.float32) * (1./255)- 0.5
    label = tf.cast (Label, TF.INT32) Return
    IMG, label
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18-------------19 20 21 22 23 24 1 2 3-4 5 6 7, 8 24

A example contains a dictionary of the Features,features containing feature (there is no s). Finally, the feature contains a floatlist, or bytelist, or int64list join the queue.

With TF. Session () as Sess:
            sess.run (init)
            # start queue
            threads = tf.train.start_queue_runners (sess=sess) for
            i in Range (5):
                print Img_batch.shape,label_batch
                val, l = Sess.run ([Img_batch, Label_batch])
                # l = to_ Categorical (l,)
                print (Val.shape, L)
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8-9

This can be done to and tensorflow the official binary dataset,  
Note: Boot queue that code don't forget, otherwise the card dies use when remember to use Val and L, or you will report type error: Typeerror:the value of a Feed cannot be a TF. Tensor object. Acceptable feed values include Python scalars, strings, lists, or NumPy ndarrays. Calculate Cross Entropy Time: Cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits (logits,labels) calculate cross entropy  
At the end of the evaluation, use Tf.nn.in_top_k (logits,labels,1) to select the Logits maximum number of indexes and label Comparisons cross_entropy =-tf.reduce_sum (Y_*tf.log (Y_conv)) Calculates the cross entropy, so the label must be converted to One-hot vector  

http://blog.csdn.net/u012759136/article/details/52232266 
http://www.shellsec.com/news/33788.html 
http://blog.csdn.net/tengxing007/article/details/54428262

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.