首页 > 解决方案 > Tensorflow 同时运行 2 个冻结图(并行)

问题描述

是否可以同时运行多个 tensorflow 对象检测模型?(我已经训练了两个模型并希望同时运行)我编写了这段代码并尝试运行但它不起作用。

# First Frozen
detection_graph1 = tf.Graph()
with detection_graph1.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH1, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')

# Second Frozen
detection_graph2 = tf.Graph()
with detection_graph2.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH2, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')

def run_inference_for_multiple_images(path,graph1,graph2):
  with graph1.as_default():
    with tf.Session() as sess1:
      with graph2.as_default():
        with tf.Session() as sess2:
          #detection code..

标签: pythontensorflowobject-detection

解决方案


是的,这绝对有可能,但你做错了。不要在两个单独的图中定义两个模型,只需将它们加载到同一个图中(并添加正确的名称范围以避免命名冲突):

graph = tf.Graph() # just one graph, with both models loaded
with graph.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH1, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='first_graph')
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH2, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='second_graph')

# [...] get the correct input and output tensors for the two graphs via their names

with tf.Session(graph=graph) as sess: # just one session
  # Running only one of the two at a time
  res_1 = sess.run(outputs_from_graph_1, feed_dict=graph_1_feeds)
  res_2 = sess.run(outputs_from_graph_2, feed_dict=graph_2_feeds)

  # Actually running them in parallel (might not fit in memory!)
  res_1_and_2 = sess.run( outputs_from_graph_1 + outputs_from_graph_2, {**graph_1_feeds, **graph_2_feeds} )

注意:我假设提要是dictstensor_name:valuesplaceholder_tensor:values键/值对


推荐阅读