生成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高效读取数据
阅读全文
0 0
- 生成tfrecords文件(29)---《深度学习》
- 生成TFRecords文件代码(最终版,亲测可用)
- TFRecords 文件的生成和读取
- TensorFlow .tfrecords训练文件生成、使用
- 7.2 TensorFlow笔记(基础篇): 生成TFRecords文件
- Tensorflow 生成tfrecords
- tensorflow将CSV文件转为TFrecords文件
- 深度学习之深度生成模型
- tensorflow生成数据集(24)---《深度学习》
- tensorflow中tfrecords文件的save和read
- python小工具--tfrecords文件的制作工具
- 读tfrecords文件,一个一个读/按批次读
- 机器学习: TensorFlow 的数据读取与TFRecords 格式
- 深度学习与生成式模型
- 深度学习之生成对抗网络GAN
- 深度学习样本生成data augmentation
- 深度学习之生成对抗网络GAN
- 深度学习之文本摘要自动生成
- Node.js学习-web框架-2-中间件
- 2.1 性能测试的分类
- L版本上添加关机振动,定时开机振动会失效
- iOS 添加到购物车 小动画
- Vue.js学习---vue概念理解No.1
- 生成tfrecords文件(29)---《深度学习》
- 不使用vector<bool>的原因和替代方法
- 自定义类型:结构体、枚举、联合
- 关于git的撤销
- 自定义圆形QLabel控件
- windbg crashdump调试,自定义切换栈帧
- oracle静态注册,动态注册,共享服务器配置
- a
- user版本如何打开root权限