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)文章
如何通過Python收集MySQL MHA 部署及運行狀態(tài)信息的功能
本篇幅主要介紹如何通過Python實現(xiàn)收集MHA 集群 節(jié)點信息 和 運行狀態(tài)的功能。這些信息將是CMDB信息的重要組成部分,感興趣的朋友一起看看吧2021-10-10
Python實戰(zhàn)之Elasticsearch的高級實現(xiàn)詳解
Elasticsearch是一個功能強大的開源搜索引擎,廣泛應用于各種場景,本文將深入探討如何使用Python與Elasticsearch進行高級實現(xiàn),需要的可以參考下2024-04-04
打印出python 當前全局變量和入口參數(shù)的所有屬性
打印出python 當前全局變量和入口參數(shù)的所有屬性的實現(xiàn)代碼。2009-07-07
Python讀取配置文件-ConfigParser的二次封裝方法
這篇文章主要介紹了Python讀取配置文件-ConfigParser的二次封裝方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Python從函數(shù)參數(shù)類型引出元組實例分析
這篇文章主要介紹了Python從函數(shù)參數(shù)類型引出元組,結(jié)合實例形式分析了Python函數(shù)定義與使用中常見的三種參數(shù)類型,并簡單分析了元組類型參數(shù)的使用,需要的朋友可以參考下2019-05-05

