最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Tensorflow分批量讀取數(shù)據(jù)教程

 更新時間:2020年02月07日 11:26:14   作者:freedom098  
今天小編就為大家分享一篇Tensorflow分批量讀取數(shù)據(jù)教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

之前的博客里使用tf讀取數(shù)據(jù)都是每次fetch一條記錄,實際上大部分時候需要fetch到一個batch的小批量數(shù)據(jù),在tf中這一操作的明顯變化就是tensor的rank發(fā)生了變化,我目前使用的人臉數(shù)據(jù)集是灰度圖像,因此大小是92*112的,所以最開始fetch拿到的圖像數(shù)據(jù)集經(jīng)過reshape之后就是一個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ù)據(jù)拆分成兩個大部分,一個是函數(shù)專門負責讀取數(shù)據(jù)和解碼數(shù)據(jù),一個函數(shù)則負責生產(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]) # 恢復圖像原始大小
  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
  # 預取圖像和label并隨機打亂,組成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ù),它相當于一個蓄水池的功能,第一個參數(shù)代表蓄水池的入水口,也就是逐個讀取到的記錄,batch_size自然就是batch的大小了,capacity是蓄水池的容量,表示能容納多少個樣本,min_after_dequeue是指出隊操作后還可以供隨機采樣出批量數(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這個值越大,隨機采樣的效果越好,但是消耗的內(nèi)存也越大。

以上這篇Tensorflow分批量讀取數(shù)據(jù)教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

鄂托克前旗| 承德县| 班戈县| 大悟县| 永定县| 同德县| 绥滨县| 濮阳县| 宽城| 宁远县| 临江市| 阳泉市| 措美县| 察隅县| 赣榆县| 安远县| 玉田县| 普安县| 藁城市| 广安市| 安泽县| 雷波县| 伊春市| 木兰县| 龙泉市| 南靖县| 青海省| 山丹县| 凤冈县| 丰镇市| 陵水| 竹北市| 西城区| 三原县| 曲阜市| 柯坪县| 张家港市| 广宁县| 桐庐县| 信宜市| 扎兰屯市|