生成tfrecords文件(29)---《深度学习》

来源:互联网 发布:js 相对路径 绝对路径 编辑:程序博客网 时间:2024/06/04 23:23

先将图片转化为tfrecords文件,然后将tfrecords文件进行批量恢复!

import os import tensorflow as tf from PIL import Image  #注意Image,后面会用到import matplotlib.pyplot as plt import numpy as npimport os.pathimport timedef read_files(dir,label_path):    filelists=os.listdir(dir)    img_lists=[]    for file in filelists:        img=Image.open(os.path.join(dir,file)).resize((224,224))        image=img.tobytes()        img_lists.append(image)    label_lists=[]    with open(label_path,'r') as f:        line=f.readline()        while line:            label_lists.append(int(line))            line=f.readline()    return img_lists,label_listsdef create_tfrecords(i_lists,l_lists):    writer=tf.python_io.TFRecordWriter("E:/hello.tfrecords")    for i,img_raw in enumerate(i_lists):        example=tf.train.Example(features=tf.train.Features(            feature={            "label":tf.train.Feature(int64_list=tf.train.Int64List(value=[l_lists[i]])),            'img_raw':tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))            }))        writer.write(example.SerializeToString())    writer.close()def read_and_decode(filename):    filename_queue=tf.train.string_input_producer([filename])    reader=tf.TFRecordReader()    _,serialized_example=reader.read(filename_queue)    features=tf.parse_single_example(serialized_example,        features={        'label':tf.FixedLenFeature([],tf.int64),        'img_raw':tf.FixedLenFeature([],tf.string)        })    image=tf.decode_raw(features['img_raw'],tf.uint8)    print("查看:"+str(image.shape))    image=tf.reshape(image,[224,224,3])    label=tf.cast(features['label'],tf.int32)    images,labels = tf.train.shuffle_batch([image, label],         batch_size=15,        num_threads=3,        capacity=30 + 3 * 15,        min_after_dequeue=30    )    return images,labelsdef recover_from_tfrecords(filename,dest_dir):    image,label=read_and_decode(filename)    with tf.Session() as sess:        init_op=tf.global_variables_initializer()        sess.run(init_op)        coord=tf.train.Coordinator()        threads=tf.train.start_queue_runners(coord=coord)        count=0        for i in range(30):            print("第"+str(i+1)+"次迭代:"+str(count))            example,lab=sess.run([image,label])            for j in range(example.shape[0]):                img=Image.fromarray(example[j],'RGB')                img=img.convert("L")                img.save(dest_dir+"\\"+str(j+count)+"_"+str(j+count)+".jpg")            count=count+15            #img=Image.fromarray(example,'RGB')            #img=img.convert("L")            #img.save(dest_dir+"\\"+str(i)+"_"+str(lab)+".jpg")            #print(example,lab)        coord.request_stop()        coord.join(threads)if __name__=="__main__":    dir="E:/test"    label_path="E:/hello_world.txt"    filename="E:/hello.tfrecords"    #img_lists,label_lists=read_files(dir,label_path)    #create_tfrecords(img_lists,label_lists)    #image,label=read_and_decode(filename)    time0=time.time()    recover_from_tfrecords(filename,"E:/hello")    time_=time.time()-time0    print("总共的时间长度:"+str(time_))

针对某些数据,我们生成了其tfrecords文件,然后我们可以查看tfrecords中的东东,值的注意的是,生成tfrecords文件和原文件相比小了很多,所以可以有效节省空间,然后将其进行恢复,我针对图片进行了批量恢复,也可以进行单张恢复,只需要把read_and_decode和recover_from_tfrecords函数稍作修改就可以得到,开心玩起来哈!

原图:

这里写图片描述

利用tfrecords文件生成的图片(灰度图)

这里写图片描述
参考:

TensorFlow 学习(二)
制作自己的TFRecord数据集,读取,显示及代码详解

TensorFlow高效读取数据