首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sess.run()太慢了

sess.run()太慢了
EN

Stack Overflow用户
提问于 2018-04-13 14:14:39
回答 1查看 3.1K关注 0票数 4

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-09-11 21:46:21

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

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

代码语言:javascript
复制
# 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 ),一旦会话“温暖”,我得到的时间是相当不错的。

代码语言:javascript
复制
--- 3.7862255573272705 seconds ---
--- 0.21631121635437012 seconds ---
--- 0.1784508228302002 seconds ---

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49819047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档