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企業(yè)編碼生成系統(tǒng)之主程序模塊設(shè)計詳解
這篇文章主要介紹了Python企業(yè)編碼生成系統(tǒng)之主程序模塊設(shè)計,包括初始化、界面與邏輯實現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2019-07-07Python實現(xiàn)修改Excel文件的元數(shù)據(jù)
這篇文章將通過使用Python、Openpyxl模塊以及wxPython庫,實現(xiàn)創(chuàng)建一個GUI界面來輸入元數(shù)據(jù),然后將這些元數(shù)據(jù)與Excel文件一起保存,感興趣的可以了解一下2023-04-04jupyter 實現(xiàn)notebook中顯示完整的行和列
這篇文章主要介紹了jupyter 實現(xiàn)notebook中顯示完整的行和列,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04詳解model.train()和model.eval()兩種模式的原理與用法
這篇文章主要介紹了詳解model.train()和model.eval()兩種模式的原理與用法,相信很多沒有經(jīng)驗的人對此束手無策,那么看完這篇文章一定會對你有所幫助2023-03-03