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发布于 2018-09-11 21:46:21
您的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的图像。
https://stackoverflow.com/questions/49819047
复制相似问题