Analysis and storage example of tensorflow------tfrecords

Source: Internet
Author: User

TensorFlow------Tfrecords Analysis and storage examples:

ImportOSImportTensorFlow as TF#defining command-line parameters such as CIFAR dataFLAGS =tf.app.flags.FLAGStf.app.flags.DEFINE_string ('Cifar_dir','./data/cifar10/cifar-10-batches-bin','Directory of Files') tf.app.flags.DEFINE_string ('Cifar_tfrecords','./tmp/cifar.tfrecords','files that store tfrecords')classCifarread (object):" "finish reading the binary file, write into Tfrecords, read Tfrecords:p Aram object:: Return:" "    def __init__(self,filelist):#file ListSelf.file_list =filelist#defines some properties of a picture that is readSelf.height = 32Self.width= 32Self.channel= 3#bytes of binary files per pictureSelf.label_bytes = 1self.image_bytes= Self.height * Self.width *Self.channel self.bytes= Self.label_bytes +self.image_bytesdefRead_and_decode (self):#1. Building the file queueFile_queue =tf.train.string_input_producer (self.file_list)#2. Build binary file Reader, read content, number of bytes per sampleReader =TF. Fixedlengthrecordreader (self.bytes) Key,value=Reader.read (file_queue)#3. Decoding content, decoding the contents of binary files Label_image contains target values and eigenvaluesLabel_image =Tf.decode_raw (value,tf.uint8)Print(label_image)#4. Segmentation of image and tag data, eigenvalues and target valuesLabel =Tf.slice (label_image,[0],[self.label_bytes]) image=Tf.slice (label_image,[self.label_bytes],[self.image_bytes])Print('---->')        Print(image)#5. You can change the shape of the image's characteristic data [3072]-->[32,32,3]Image_reshape =Tf.reshape (Image,[self.height,self.width,self.channel])Print('======>')        Print(label)Print('======>')        #6. Batch processing DataImage_batch,label_batch = Tf.train.batch ([image_reshape,label],batch_size=10,num_threads=1,capacity=10)        Print(Image_batch,label_batch)returnImage_batch,label_batchdefwrite_ro_tfrecords (self,image_batch,label_batch):" "Save the image's eigenvalues and target values into the Tfrecords:p Aram the eigenvalues of the image_batch:10 picture:p Aram Label_batch:10 The target value of the picture: Retur N:none" "        #1. Build Tfrecord Memorywriter =Tf.python_io. Tfrecordwriter (flags.cifar_tfrecords)#2. Loop writes all the samples to the file, each picture sample constructs the example protocol         forIinchRange (10):            #Remove the eigenvalues and target values for the I-picture dataImage =image_batch[i].eval (). ToString () label=Int (label_batch[i].eval () [0])#construct a example of a sampleExample = Tf.train.Example (Features=tf.train.features (feature={                'Image': Tf.train.Feature (Bytes_list=tf.train.byteslist (value=[Image]),'label': Tf.train.Feature (Int64_list=tf.train.int64list (value=[Label]),}))#Write a separate sampleWriter.write (example. Serializetostring ())#Closewriter.close ()returnNoneif __name__=='__main__':    #find file, build list path + Name listfile_name =Os.listdir (Flags.cifar_dir)#stitching paths re-forming listsFileList = [Os.path.join (flags.cifar_dir,file) forFileinchfile_nameifFile[-3:] = ='bin']    #calling a function to pass a parameterCF =Cifarread (filelist) Image_batch,label_batch=Cf.read_and_decode ()#Open SessionWith TF. Session () as Sess:#define a thread coordinatorCoord =Tf.train.Coordinator ()#to open a thread that reads a fileThreads = Tf.train.start_queue_runners (sess,coord=coord)#Save in Tfrecords file        Print('Start Storage') cf.write_ro_tfrecords (image_batch,label_batch)Print('End Storage')        #Print the Read content        #Print (Sess.run ([Image_batch,label_batch]))        #Reclaim Child Threadscoord.request_stop () coord.join (threads)

Analysis and storage example of tensorflow------tfrecords

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.