数据读取之TFRecords
来源:互联网 发布:继承 多态 java 编辑:程序博客网 时间:2024/06/09 22:42
转载自http://blog.csdn.net/u012759136/article/details/52232266 对部分代码做了一些修改
import osimport tensorflow as tffrom PIL import Imagecwd = os.getcwd()
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 create_record(): ''' 此处我加载的数据目录如下: 0 -- img1.jpg img2.jpg img3.jpg ... 1 -- img1.jpg img2.jpg ... 2 -- ... ... ''' writer = tf.python_io.TFRecordWriter("train.tfrecords") for index, name in enumerate(classes): class_path = cwd + name + "/" for img_name in os.listdir(class_path): img_path = class_path + img_name img = Image.open(img_path) img = img.resize((224, 224)) img_raw = img.tobytes() #将图片转化为原生bytes example = tf.train.Example(features=tf.train.Features(feature={ "label": _int64_feature(index), 'img_raw': _bytes_feature(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), }) img = tf.decode_raw(features['img_raw'], tf.uint8) img = tf.reshape(img, [224, 224, 3]) img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 label = tf.cast(features['label'], tf.int32) return img, labelif __name__ == '__main__': img, label = read_and_decode("train.tfrecords") img_batch, label_batch = tf.train.shuffle_batch([img, label], batch_size=30, capacity=2000, min_after_dequeue=1000,enqueue_many=True) #初始化所有的op init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init)#启动队列
coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess,coord=coord) for i in range(3): val, l= sess.run([img_batch, label_batch]) print(val.shape, l)
coord.request_stop()coord.join(threads)sess.close()
在源代码上做了一些修改
1.将Feature函数单独抽象出来
2.tf.train.shuffle_batch中的参数enqueue_many修改成了True,默认是False,区别在于输入[x,y,z]如果enqueue_many默认为False,则输出为[batch_size,x,y,z],如果设置为True,那么输出就是[batch_size,y,z]
3.将变量初始化修改成了新版的初始化方法(貌似这里用不到)
4.加入了tf.train.Coordinator()
0 0
- 数据读取之TFRecords
- Tensorlfow 数据读取之TFRecords
- tensorflow数据读取之tfrecords
- TensorFlow读取tfrecords数据
- 由浅入深之Tensorflow(3)----数据读取之TFRecords
- 由浅入深之Tensorflow(3)----数据读取之TFRecords
- 机器学习: TensorFlow 的数据读取与TFRecords 格式
- tensorflow读取SVHN数据集转为TFrecords格式
- Tensorflow之构建自己的图片数据集TFrecords
- Tensorflow之构建自己的图片数据集TFrecords(精)
- Tensorflow之构建自己的图片数据集TFrecords
- 制作TFrecords格式数据
- Tensorflow分批量读取tfrecords
- Tensorflow中使用TFRecords高效读取数据--结合NLP数据实践
- Tensorflow中使用TFRecords高效读取数据--结合NLP数据实践
- [TFRecord格式数据]利用TFRecords存储与读取带标签的图片
- 读取.tfrecords格式数据集,进行geture的cnn构建、训练、模型保存
- TFRecords 文件的生成和读取
- 排序算法(4)--插入排序
- MySql查询优化,高并发控制
- mysql导入大型sql文件时注意事项
- 嵌入式编程感想:会用C语言的是真男人!
- 关于结构体运用中的一些细节问题(输入的数据与打印出来的对不齐)
- 数据读取之TFRecords
- POJ
- 深度学习与计算机视觉梳理思考
- 关于properties文件中文乱码问题
- if 1 endif if 0
- 使用ajax上传文件
- 网络下载文件
- shell 批量导入数据
- invalid constant type: 15问题解决