tensorflow中的TFRecord格式文件的写入和读取

在tensorflow中,TFRecord格式的文件是可以将样本和标签放在一起,是在模型之前的一个预处理步骤,这种方式可以很大的提高效率和节约运行的内存,这种格式对数据是不进行压缩的。image.tfrecord也可以是image.tfrecords,格式写入和读取要一样就好了

1.写入

import tensorflow as tf;  

image_raw_data = tf.gfile.FastGFile('/home/penglu/Desktop/11.jpg').read()
image = tf.image.decode_jpeg(image_raw_data)

a = image.eval(session=tf.Session())
a = a.tostring()
label = 1

writer = tf.python_io.TFRecordWriter('/home/penglu/Desktop/image.tfrecord')


example = tf.train.Example(features=tf.train.Features(feature={
	'label':tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),
	'image':tf.train.Feature(bytes_list=tf.train.BytesList(value=[a]))
	}))

writer.write(example.SerializeToString())
writer.close()

2.读取

import tensorflow as tf
reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer(['/home/penglu/Desktop/image.tfrecord'])
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example, features={
	'image': tf.FixedLenFeature([], tf.string),
	'label': tf.FixedLenFeature([], tf.int64)
	})

img = tf.decode_raw(features['image'], tf.uint8)
label = tf.cast(features['label'], tf.int32)

with tf.Session() as sess:
	sess.run(tf.initialize_all_variables())
	coord = tf.train.Coordinator()
	threads = tf.train.start_queue_runners(sess=sess, coord=coord)
	print sess.run(label)
	coord.request_stop() 
	coord.join(threads) 

输出:

1


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值