TensorFlow uses Tfrecord to build its own dataset and read
: http://blog.csdn.net/freedom098/article/details/56011858
And a video of Kevin the Great God of Youku.
Objective: 1. Store your data set in Tfrecord format.
2, read the data from the Tfrecord, and use the drawing tools to show the picture form.
Take a picture for example:
One, the picture storage tfrecod
# the attribute def _int64_feature (value) that generates an integer is: if not isinstance (value,list): Value=[value] Return tf.train.Feature (Int64_list=tf.train.int64list (Value=value)) #生成字符串型的属性 def _byte_feature (value): Return Tf.train.Feature (Bytes_ List=tf.train.byteslist (Value=[value]) #将图片存储到tfrecord中 def convert_to_tfrecord (images, labels, save_dir, name): #
Read the picture from the picture path encoding into Tfrecord ' convert all images and labels to one Tfrecord file. Args:images:list of image directories, String type labels:list of labels, int type save_dir: The directory to save Tfrecord file, e.g.: '/home/folder1/' name:the name of Tfrecord file, string type, e.g.:
' Train ' return:no return note:converting needs some time, be patient ...
' filename = (save_dir + name + '. Tfrecords ') N_samples = len (labels) #判断 the number of samples of image and whether the label is the same If Np.shape (images) [0]!= n_samples:raise valueerror (' images size%d does noT match label size%d. '% (images.shape[0], n_samples)) writer = Tf.python_io.
Tfrecordwriter (filename) print (' \ntransform start ... ') for I in range (len (images)): try:
Image_raw_data = Tf.gfile.FastGFile (Images[i], ' R '). Read () Img_data = Tf.image.decode_png (image_raw_data) label = Int (Labels[i]) example = Tf.train.Example (Features=tf.train.features (feat ure={' label ': int64_feature (label), ' Image_raw ': bytes_feature
(Image_raw)})) Writer.write (example. Serializetostring ()) except IOError as E:print (' could not read: ', images[i]) print (
' ERROR:%s '%e ' Print (' Skip it!\n ') writer.close ()
Second, read the data, and drawing
# Read the data from Tfrecoder def read_and_decode (tfrecords_file): ' "' Read and decode Tfrecord file, generate (image , label) Batches Args:tfrecords_file:the directory of Tfrecord file batch_size:number of images in each batch R eturns:image:4d tensor-[batch_size, width, height, channel] label:1d tensor-[batch_size] ' # Make a INP UT queue from the Tfrecord file Filename_queue = Tf.train.string_input_producer ([tfrecords_file]) reader = TF. Tfrecordreader () _, Serialized_example = Reader.read (filename_queue) #解析读入的样例 img_features = Tf.parse_single_example
(Serialized_example, features={ ' label ': TF. Fixedlenfeature ([], Tf.int64), ' Image_raw ': TF. Fixedlenfeature ([], tf.string),}) #将字符串解析成相应的数组 image = Tf.decode_raw (img_features[ ' Image_raw '], tf.uint8 #转化成图片的格式 image = Tf.reshapE (image, [465, 315,3]) Sess = tf. Session () Coord = Tf.train.Coordinator () threads = tf.train.start_queue_runners (sess=sess,coord=coord) image, label =
Sess.run ([Image,label]) Print image plt.imshow (image) Plt.show () Sess.close ()
Read_and_decode ('/home/tensor/desktop/tia.tfrecords ')
Iii. Results