欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

tensorflow學習筆記之tfrecord文件的生成與讀取

 更新時間:2021年03月31日 09:27:15   作者:wxsy024680  
這篇文章主要介紹了tensorflow學習筆記之tfrecord文件的生成與讀取,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

訓練模型時,我們并不是直接將圖像送入模型,而是先將圖像轉(zhuǎn)換為tfrecord文件,再將tfrecord文件送入模型。為進一步理解tfrecord文件,本例先將6幅圖像及其標簽轉(zhuǎn)換為tfrecord文件,然后讀取tfrecord文件,重現(xiàn)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'))
			#將圖像轉(zhuǎn)化為bytes
			image_data = image_data.tobytes()
			#讀取label
			label = labels[filename.split('/')[-2]]
			#生成protocol數(shù)據(jù)類型
			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

# 根據(jù)文件名生成一個隊列
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)})
# 獲取圖像數(shù)據(jù)
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:
	# 創(chuàng)建一個協(xié)調(diào)器,管理線程
	coord = tf.train.Coordinator()
	# 啟動QueueRunner, 此時文件名隊列已經(jīng)進隊
	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)

	# 通知其他線程關(guān)閉
	coord.request_stop()
	# 其他所有線程關(guān)閉之后,這一函數(shù)才能返回
	coord.join(threads)

到此這篇關(guān)于tensorflow學習筆記之tfrecord文件的生成與讀取的文章就介紹到這了,更多相關(guān)tfrecord文件的生成與讀取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論