繁体   English   中英

Tensorflow 同时运行 2 个冻结图(并行)

[英]Tensorflow run 2 frozen graphs at the same time (parallel)

是否可以同时运行多个 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..

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

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} )

注意:我假设提要是带有tensor_name:valuesplaceholder_tensor:values键/值对的dict s

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM