资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用Tensorflow怎么分批量读取数据-创新互联

使用Tensorflow怎么分批量读取数据,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

成都服务器托管,创新互联提供包括服务器租用、眉山服务器托管、带宽租用、云主机、机柜租用、主机租用托管、CDN网站加速、域名申请等业务的一体化完整服务。电话咨询:028-86922220
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发生了变化,多了一个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)

读取数据和解码数据与之前基本相同,针对不同格式数据集使用不同阅读器和解码器即可,后面是产生batch,核心是tf.train.shuffle_batch这个函数,它相当于一个蓄水池的功能,第一个参数代表蓄水池的入水口,也就是逐个读取到的记录,batch_size自然就是batch的大小了,capacity是蓄水池的容量,表示能容纳多少个样本,min_after_dequeue是指出队操作后还可以供随机采样出批量数据的样本池大小,显然,capacity要大于min_after_dequeue,官网推荐:min_after_dequeue + (num_threads + a small safety margin) * batch_size,还有一个参数就是num_threads,表示所用线程数目。

min_after_dequeue这个值越大,随机采样的效果越好,但是消耗的内存也越大。

关于使用Tensorflow怎么分批量读取数据问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


本文名称:使用Tensorflow怎么分批量读取数据-创新互联
当前路径:http://cdkjz.cn/article/gidpp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220