使用Tensorflow怎么分批量讀取數(shù)據(jù),針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
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,此時tensor rank發(fā)生了變化,多了一個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ù)與之前基本相同,針對不同格式數(shù)據(jù)集使用不同閱讀器和解碼器即可,后面是產(chǎn)生batch,核心是tf.train.shuffle_batch這個函數(shù),它相當(dāng)于一個蓄水池的功能,第一個參數(shù)代表蓄水池的入水口,也就是逐個讀取到的記錄,batch_size自然就是batch的大小了,capacity是蓄水池的容量,表示能容納多少個樣本,min_after_dequeue是指出隊操作后還可以供隨機(jī)采樣出批量數(shù)據(jù)的樣本池大小,顯然,capacity要大于min_after_dequeue,官網(wǎng)推薦:min_after_dequeue + (num_threads + a small safety margin) * batch_size,還有一個參數(shù)就是num_threads,表示所用線程數(shù)目。
min_after_dequeue這個值越大,隨機(jī)采樣的效果越好,但是消耗的內(nèi)存也越大。
關(guān)于使用Tensorflow怎么分批量讀取數(shù)據(jù)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
本文名稱:使用Tensorflow怎么分批量讀取數(shù)據(jù)-創(chuàng)新互聯(lián)
URL分享:http://aaarwkj.com/article38/gidpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、用戶體驗、網(wǎng)站設(shè)計公司、電子商務(wù)、App設(shè)計、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容