【问题标题】:sess.run() is too slowsess.run() 太慢了
【发布时间】:2018-09-23 22:25:48
【问题描述】:

Tensorflow 对象检测模块的 sess.run() 函数需要大约 2.5 秒来检测 600x600 图像中的边界边界。我怎样才能加快这段代码的速度?

def run(image, detection_graph):

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        # Definite input and output Tensors for detection_graph
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        # Each box represents a part of the image where a particular object was detected.
        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

        # the array based representation of the image will be used later in order to prepare the
        # result image with boxes and labels on it.
        image_np = image
        # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
        image_np_expanded = np.expand_dims(image_np, axis=0)
        # Actual detection.
        print("2")
        start_time = datetime.datetime.now()
        (boxes, scores, classes, num) = sess.run(
          [detection_boxes, detection_scores, detection_classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})
        end_time = datetime.datetime.now()
        diff = (end_time - start_time).total_seconds()*1000
        print (diff)
        print("3")

        return boxes[0], scores[0]
        #print scores
        #print classes

【问题讨论】:

  • 你在 CPU 上运行吗?显卡?如果是 CPU,一个明显的建议是获得 GPU。 600x600 是一个非常大的图像,可以操作,减少输入大小可能会起作用。 2.5s 对于这么大的图像并假设一个相当大的模型听起来不是那么不合理。
  • 好的,谢谢。是的,我在 CPU 上运行。那么我应该将图像调整为更小的尺寸吗?

标签: tensorflow object-detection sess.run


【解决方案1】:

您的sess.run 执行时间对于第一次 运行来说是正常的,之后它的运行速度可能会快100 倍(不是开玩笑)。

关键是重用会话,在您的示例中,我将添加另一个图像评估并测量该时间并检查性能是否有所提高,例如:

# all your prev code here
print (diff)
print("3")

image_np = image2 # get another image from somewhere
image_np_expanded = np.expand_dims(image_np, axis=0)
start_time = datetime.datetime.now()

(boxes, scores, classes, num) = sess.run(
          [detection_boxes, detection_scores, detection_classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})
end_time = datetime.datetime.now()

diff = (end_time - start_time).total_seconds()*1000
print("Detection #2")
print(diff)

因此,您不需要 GPU 或更小的图像(目前),只需“预热”会话并将其用于所有预测。

我目前在测试环境中的设置非常适中,Ubuntu 的最新版本在 VirtualBox 上运行,单核且无 GPU(MobileNet2 + COCO 数据集),一旦会话“温暖”,我得到的时间相当不错.

--- 3.7862255573272705 seconds ---
--- 0.21631121635437012 seconds ---
--- 0.1784508228302002 seconds ---

注意第一个缓慢的执行时间,最后一个是大小为 1050*600 的图像

【讨论】:

    猜你喜欢
    • 2013-03-10
    • 2014-06-07
    • 2016-05-31
    • 2011-07-07
    • 2015-08-23
    • 2012-07-05
    • 2016-01-08
    • 2014-03-12
    • 2021-03-26
    相关资源
    最近更新 更多