Learning Google's deep learning finally a little bit of the prospect, to share my tensorflow learning process.
TensorFlow's official Chinese document is jerky, and the dataset has been used in the Mnist binary dataset. And not much about how to build their own picture dataset Tfrecords.
First paste my conversion code to transfer the pictures under the picture folder to the Tfrecords dataset.
############################################################################################ #!/usr/bin/ python2.7 #-*-Coding:utf-8-*-#Author: Zhaoqinghui #Date: 2016.5.10 #Function: Image convert to Tfrecords #####
######################################################################################## Import TensorFlow as TF Import NumPy as NP import cv2 import OS import os.path from PIL import Image #参数设置 ###################################### ######################################################### train_file = ' train.txt ' #训练图片 name= ' train ' #生成train. Tfrec Ords output_directory= './tfrecords ' resize_height=32 #存储图片高度 resize_width=32 #存储图片宽度 ############################### ################################################################ def _int64_feature (value): return Tf.train.Feature (Int64_list=tf.train.int64list (value=[value)) def _bytes_feature (value): Return Tf.train.Feature ( Bytes_list=tf.train.byteslist (Value=[value])) def load_file (examples_list_file): lines = Np.genfromtxt (Examples_list_file, delimiter= "", dtype=[(' col1 ', ' S120 '), (' col2 ', ' i8 ')]) examples = [ ] labels = [] For example, label in Lines:examples.append (example) labels.append (label) retur n Np.asarray (examples), Np.asarray (labels), Len (lines) def extract_image (filename, resize_height, resize_width): Ima
GE = cv2.imread (filename) image = Cv2.resize (image, (Resize_height, resize_width)) B,g,r = Cv2.split (image) Rgb_image = Cv2.merge ([r,g,b]) return rgb_image def transform2tfrecord (train_file, name, Output_directory, Resize_height, Resize_width): If not os.path.exists (output_directory) or Os.path.isfile (output_directory): OS. Makedirs (output_directory) _examples, _labels, Examples_num = load_file (train_file) filename = output_directory + "/" + name + '. Tfrecords ' writer = tf.python_io.
Tfrecordwriter (filename) for I, [example, label] in enumerate (Zip (_examples, _labels)): Print (' no.%d '% (i)) image = Extract_image (example, Resize_height, resize_width) print (' shape:%d,%
D,%d, Label:%d '% (image.shape[0], image.shape[1], image.shape[2], label) Image_raw = Image.tostring ()
Example = Tf.train.Example (Features=tf.train.features (feature={' Image_raw ': _bytes_feature (Image_raw), ' Height ': _int64_feature (image.shape[0]), ' width ': _int64_feature (image.shape[1]), ' depth ': _int64_feature (image.shape[2]), ' label ': _int64_feature (label)}) Writer.write (example. Serializetostring ()) writer.close () def disp_tfrecords (tfrecord_list_file): Filename_queue = Tf.train.string_inpu T_producer ([tfrecord_list_file]) reader = TF. Tfrecordreader () _, Serialized_example = Reader.read (filename_queue) features = Tf.parse_single_example (s Erialized_example, features={' Image_raw ': TF. Fixedlenfeature ([], tf.string), ' HeiGht ': TF. Fixedlenfeature ([], Tf.int64), ' width ': tf. Fixedlenfeature ([], Tf.int64), ' depth ': TF. Fixedlenfeature ([], Tf.int64), ' label ': TF. Fixedlenfeature ([], Tf.int64)}) image = Tf.decode_raw (features[' Image_raw '], tf.uint8) #print (repr (IMA GE) height = features[' height '] width = features[' width '] depth = features[' depth '] label = Tf.cast (Featu res[' label ', tf.int32) Init_op = Tf.initialize_all_variables () resultimg=[] resultlabel=[] with TF. Session () as Sess:sess.run (init_op) coord = Tf.train.Coordinator () threads = Tf.train.start_queue _runners (sess=sess, Coord=coord) for I in range: Image_eval = Image.eval () ResultLabel
. Append (Label.eval ()) Image_eval_reshape = Image_eval.reshape ([Height.eval (), Width.eval (), Depth.eval ()]) Resultimg.append (image_eval_reshape) pilimg = Image.fromarray (Np.asarray (image_eval_rEshape)) Pilimg.show () Coord.request_stop () coord.join (threads) sess.close () retur n Resultimg,resultlabel def Read_tfrecord (filename_queuetemp): Filename_queue = tf.train.string_input_producer ([File Name_queuetemp]) reader = TF. Tfrecordreader () _, Serialized_example = Reader.read (filename_queue) features = Tf.parse_single_example (s Erialized_example, features={' Image_raw ': TF. Fixedlenfeature ([], tf.string), ' width ': tf. Fixedlenfeature ([], Tf.int64), ' depth ': TF. Fixedlenfeature ([], Tf.int64), ' label ': TF. Fixedlenfeature ([], Tf.int64)}) image = Tf.decode_raw (features[' Image_raw '], tf.uint8) # image TF. Reshape (image, [256, 256, 3]) # Normalize image = Tf.cast (image, Tf.float32) * (1./255)-0.5 # Label Lab el = tf.cast (features[' label '], Tf.int32) return image, label Def Test (): Transform2tfrecord (train_file, name, O Utput_directOry, Resize_height, Resize_width) #转化函数 img,label=disp_tfrecords (output_directory+ '/' +name+ '. Tfrecords ') #显示函数 Img,label=read_tfrecord (output_directory+ '/' +name+ '. Tfrecords ') #读取函数 print label if __name__ = ' __main__ ': tes
T ()
This allows you to get your own dataset. Tfrecords, it can be used directly for TensorFlow datasets.