广告
返回顶部
首页 > 资讯 > 后端开发 > Python >tensorflow学习笔记之tfrecord文件的生成与读取
  • 680
分享到

tensorflow学习笔记之tfrecord文件的生成与读取

2024-04-02 19:04:59 680人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

训练模型时,我们并不是直接将图像送入模型,而是先将图像转换为tfrecord文件,再将tfrecord文件送入模型。为进一步理解tfrecord文件,本例先将6幅图像及其标签转换为t

训练模型时,我们并不是直接将图像送入模型,而是先将图像转换为tfrecord文件,再将tfrecord文件送入模型。为进一步理解tfrecord文件,本例先将6幅图像及其标签转换为tfrecord文件,然后读取tfrecord文件,重现6幅图像及其标签。
1、生成tfrecord文件


import os
import numpy as np
import Tensorflow as tf
from PIL import Image

filenames = [
'images/cat/1.jpg',
'images/cat/2.jpg',
'images/dog/1.jpg',
'images/dog/2.jpg',
'images/pig/1.jpg',
'images/pig/2.jpg',]

labels = {'cat':0, 'dog':1, 'pig':2}

def int64_feature(values):
	if not isinstance(values, (tuple, list)):
		values = [values]
	return tf.train.Feature(int64_list=tf.train.Int64List(value=values))

def bytes_feature(values):
	return tf.train.Feature(bytes_list=tf.train.BytesList(value=[values]))

with tf.Session() as sess:
	output_filename = os.path.join('images/train.tfrecords')
	with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer:
		for filename in filenames:
			#读取图像
			image_data = Image.open(filename)
			#图像灰度化
			image_data = np.array(image_data.convert('L'))
			#将图像转化为bytes
			image_data = image_data.tobytes()
			#读取label
			label = labels[filename.split('/')[-2]]
			#生成protocol数据类型
			example = tf.train.Example(features=tf.train.Features(feature={'image': bytes_feature(image_data),
																			'label': int64_feature(label)}))
			tfrecord_writer.write(example.SerializeToString())

2、读取tfrecord文件


import tensorflow as tf
import matplotlib.pyplot as plt
from PIL import Image

# 根据文件名生成一个队列
filename_queue = tf.train.string_input_producer(['images/train.tfrecords'])
reader = tf.TFRecordReader()
# 返回文件名和文件
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example, 
									features={'image': tf.FixedLenFeature([], tf.string), 
												'label': tf.FixedLenFeature([], tf.int64)})
# 获取图像数据
image = tf.decode_raw(features['image'], tf.uint8)
# 恢复图像原始尺寸[高,宽]
image = tf.reshape(image, [60, 160])
# 获取label
label = tf.cast(features['label'], tf.int32)

with tf.Session() as sess:
	# 创建一个协调器,管理线程
	coord = tf.train.Coordinator()
	# 启动QueueRunner, 此时文件名队列已经进队
	threads = tf.train.start_queue_runners(sess=sess, coord=coord)

	for i in range(6):
		image_b, label_b = sess.run([image, label])
		img = Image.fromarray(image_b, 'L')
		plt.imshow(img)
		plt.axis('off')
		plt.show()
		print(label_b)

	# 通知其他线程关闭
	coord.request_stop()
	# 其他所有线程关闭之后,这一函数才能返回
	coord.join(threads)

到此这篇关于tensorflow学习笔记之tfrecord文件的生成与读取的文章就介绍到这了,更多相关tfrecord文件的生成与读取内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: tensorflow学习笔记之tfrecord文件的生成与读取

本文链接: https://www.lsjlt.com/news/122634.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • tensorflow学习笔记之tfrecord文件的生成与读取
    训练模型时,我们并不是直接将图像送入模型,而是先将图像转换为tfrecord文件,再将tfrecord文件送入模型。为进一步理解tfrecord文件,本例先将6幅图像及其标签转换为t...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作