【问题标题】:What is the most efficient way to load data into Tensorflow for real time inference?将数据加载到 Tensorflow 进行实时推理的最有效方法是什么?
【发布时间】:2019-02-15 20:23:56
【问题描述】:

在为 Tensorflow(网络摄像头图像)设置数据输入管道时,需要花费大量时间将数据从系统 RAM 加载到 GPU 内存。

我正在尝试通过我的对象检测网络提供恒定的图像流 (1024x1024)。我目前在 AWS 上使用 V100 来执行推理。

第一次尝试是使用一个简单的 feed dict 操作。

# Get layers
img_input_tensor = sess.graph.get_tensor_by_name('import/input_image:0')
img_anchors_input_tensor = sess.graph.get_tensor_by_name('import/input_anchors:0')
img_meta_input_tensor = sess.graph.get_tensor_by_name('import/input_image_meta:0')
detections_input_tensor = sess.graph.get_tensor_by_name('import/output_detections:0')

detections = sess.run(detections_input_tensor,
                 feed_dict={img_input_tensor: molded_image, img_meta_input_tensor: image_meta, img_anchors_input_tensor: image_anchor})

这产生了每张图像大约 0.06 毫秒的推理时间。

但是,在阅读了 Tensorflow 手册后,我注意到推荐使用 tf.data API 来加载数据以进行推理。

# setup data input
data = tf.data.Dataset.from_tensors((img_input_tensor, img_meta_input_tensor, img_anchors_input_tensor, detections_input_tensor))
iterator = data.make_initializable_iterator()  # create the iterator
next_batch = iterator.get_next()

# load data
sess.run(iterator.initializer,
                 feed_dict={img_input_tensor: molded_image, img_meta_input_tensor: image_meta, img_anchors_input_tensor: image_anchor})

# inference
detections = sess.run([next_batch])[0][3]

这将推理时间加快到 0.01 毫秒,加载数据所需的时间为 0.1 毫秒。这个Iterator 方法比'慢'的feed_dict 方法要长得多。我可以做些什么来加快加载过程吗?

【问题讨论】:

    标签: python-3.x tensorflow gpu


    【解决方案1】:

    Here 是数据管道优化的绝佳指南。我个人认为.prefetch 方法是提升输入管道的最简单方法。但是,本文提供了更高级的技术。

    但是,如果您的输入数据不在 tfrecords 中,而是您自己提供,您必须以某种方式自己实现所描述的技术(缓冲、交错操作)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 1970-01-01
      • 2017-11-18
      相关资源
      最近更新 更多