Tensorflow 實(shí)現(xiàn)分批量讀取數(shù)據(jù)
之前的博客里使用tf讀取數(shù)據(jù)都是每次fetch一條記錄,實(shí)際上大部分時(shí)候需要fetch到一個(gè)batch的小批量數(shù)據(jù),在tf中這一操作的明顯變化就是tensor的rank發(fā)生了變化,我目前使用的人臉數(shù)據(jù)集是灰度圖像,因此大小是92*112的,所以最開始fetch拿到的圖像數(shù)據(jù)集經(jīng)過reshape之后就是一個(gè)rank為2的tensor,大小是92*112的(如果考慮通道,也可以reshape為rank為3的,即92*112*1)。
如果加入batch,比如batch大小為5,那么拿到的tensor的rank就變成了3,大小為5*92*112。
下面規(guī)則化的寫一下讀取數(shù)據(jù)的一般流程,按照官網(wǎng)的實(shí)例,一般把讀取數(shù)據(jù)拆分成兩個(gè)大部分,一個(gè)是函數(shù)專門負(fù)責(zé)讀取數(shù)據(jù)和解碼數(shù)據(jù),一個(gè)函數(shù)則負(fù)責(zé)生產(chǎn)batch。
import tensorflow as tf
def read_data(fileNameQue):
reader = tf.TFRecordReader()
key, value = reader.read(fileNameQue)
features = tf.parse_single_example(value, features={'label': tf.FixedLenFeature([], tf.int64),
'img': tf.FixedLenFeature([], tf.string),})
img = tf.decode_raw(features["img"], tf.uint8)
img = tf.reshape(img, [92,112]) # 恢復(fù)圖像原始大小
label = tf.cast(features["label"], tf.int32)
return img, label
def batch_input(filename, batchSize):
fileNameQue = tf.train.string_input_producer([filename], shuffle=True)
img, label = read_data(fileNameQue) # fetch圖像和label
min_after_dequeue = 1000
capacity = min_after_dequeue+3*batchSize
# 預(yù)取圖像和label并隨機(jī)打亂,組成batch,此時(shí)tensor rank發(fā)生了變化,多了一個(gè)batch大小的維度
exampleBatch,labelBatch = tf.train.shuffle_batch([img, label],batch_size=batchSize, capacity=capacity,
min_after_dequeue=min_after_dequeue)
return exampleBatch,labelBatch
if __name__ == "__main__":
init = tf.initialize_all_variables()
exampleBatch, labelBatch = batch_input("./data/faceTF.tfrecords", batchSize=10)
with tf.Session() as sess:
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(100):
example, label = sess.run([exampleBatch, labelBatch])
print(example.shape)
coord.request_stop()
coord.join(threads)
讀取數(shù)據(jù)和解碼數(shù)據(jù)與之前基本相同,針對(duì)不同格式數(shù)據(jù)集使用不同閱讀器和解碼器即可,后面是產(chǎn)生batch,核心是tf.train.shuffle_batch這個(gè)函數(shù),它相當(dāng)于一個(gè)蓄水池的功能,第一個(gè)參數(shù)代表蓄水池的入水口,也就是逐個(gè)讀取到的記錄,batch_size自然就是batch的大小了,capacity是蓄水池的容量,表示能容納多少個(gè)樣本,min_after_dequeue是指出隊(duì)操作后還可以供隨機(jī)采樣出批量數(shù)據(jù)的樣本池大小,顯然,capacity要大于min_after_dequeue,官網(wǎng)推薦:min_after_dequeue + (num_threads + a small safety margin) * batch_size,還有一個(gè)參數(shù)就是num_threads,表示所用線程數(shù)目。
min_after_dequeue這個(gè)值越大,隨機(jī)采樣的效果越好,但是消耗的內(nèi)存也越大。
以上這篇Tensorflow 實(shí)現(xiàn)分批量讀取數(shù)據(jù)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- TensorFlow實(shí)現(xiàn)從txt文件讀取數(shù)據(jù)
- TensorFlow 讀取CSV數(shù)據(jù)的實(shí)例
- 利用Tensorflow的隊(duì)列多線程讀取數(shù)據(jù)方式
- tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實(shí)例
- tensorflow入門:TFRecordDataset變長(zhǎng)數(shù)據(jù)的batch讀取詳解
- tensorflow tf.train.batch之?dāng)?shù)據(jù)批量讀取方式
- Tensorflow中使用tfrecord方式讀取數(shù)據(jù)的方法
- 用十張圖詳解TensorFlow數(shù)據(jù)讀取機(jī)制(附代碼)
- TensorFlow高效讀取數(shù)據(jù)的方法示例
- 詳解Tensorflow數(shù)據(jù)讀取有三種方式(next_batch)
- Tensorflow分批量讀取數(shù)據(jù)教程
相關(guān)文章
python連接access數(shù)據(jù)庫兩種方式總結(jié)
這篇文章主要介紹了python連接access數(shù)據(jù)庫兩種方式的相關(guān)資料,SQLAlchemy使用access方言進(jìn)行連接,而pyodbc則通過pyodbc模塊實(shí)現(xiàn)連接,文章還提供了連接代碼示例,需要的朋友可以參考下2025-02-02
使用Python編寫一個(gè)簡(jiǎn)單的tic-tac-toe游戲的教程
這篇文章主要介紹了使用Python編寫一個(gè)簡(jiǎn)單的tic-tac-toe游戲的教程,有利于Python初學(xué)者進(jìn)行上手實(shí)踐,需要的朋友可以參考下2015-04-04
用Python和WordCloud繪制詞云的實(shí)現(xiàn)方法(內(nèi)附讓字體清晰的秘笈)
這篇文章主要介紹了用Python和WordCloud繪制詞云的實(shí)現(xiàn)方法(內(nèi)附讓字體清晰的秘笈),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
Python使用當(dāng)前時(shí)間、隨機(jī)數(shù)產(chǎn)生一個(gè)唯一數(shù)字的方法
這篇文章主要介紹了Python使用當(dāng)前時(shí)間、隨機(jī)數(shù)產(chǎn)生一個(gè)唯一數(shù)字的方法,涉及Python時(shí)間與隨機(jī)數(shù)相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
python神經(jīng)網(wǎng)絡(luò)使用Keras構(gòu)建RNN訓(xùn)練
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)使用Keras構(gòu)建RNN網(wǎng)絡(luò)訓(xùn)練,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2022-05-05

