制作和读取tfrecord

1.制作tfrecord文件
#tf_filename:需提前新建一个tfrecord空白文件。然后使用tf.python_io.TFRecordWriter() 建立一个writer
with tf.python_io.TFRecordWriter(tf_filename) as tfrecord_writer:
	#dataset为字典格式
	for i,image_example in enumerate(dataset):
	#小技巧,在制作dataset的时候,只是把图片的路径放到里面,这样可以节省内存。
	filename = image_example['filename']
	image = cv2.imread(filename)
	image_data = image.tostring()
	
	class_label = image_example['balel']
	
	#创建example
	example = tf.train.Example(features=tf.train.Features(feature={
	'image':tf.train.Feature(bytes_list=tf.train.BytesList(value=image_data)),
	'label':tf.train.Feature(int64_list=tf.train.Int64List(value=class_label))}))
	
	#把example写入writer
	tfrecord_writer.write(example.SerializeToString())
2.读取tfrecord文件
##############读取tfrecord
def read_single_tfrecord(filename):
	filename_queue = tf.train.string_input_producer(filename,shuffle=True)
	#创建reader
	reader = tf.TFRecordReader()
	_,serizlized_example = reader.read(filename_queue)
	
	#解析
	images_features = tf.parse_single_example(
	serizlized_example,
	features={
	'image':tf.FixedLenFeature([],tf.string),
	'label':tf.FixedLenFeature([],tf.int64)
	})
	image = tf.decode_raw(images_features['image'],tf.unit8)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值