Python Tensorflow同时运行2个冻结图(并行)
是否可以同时运行多个tensorflow对象检测模型?(我已经培训了两个模型,并希望两者并行运行) 我编写了这段代码并尝试运行,但它不起作用Python Tensorflow同时运行2个冻结图(并行),python,tensorflow,object-detection,Python,Tensorflow,Object Detection,是否可以同时运行多个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: seri
# 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..
是的,这是绝对可能的,但你做错了。 不要在两个单独的图中定义这两个模型,只需将它们加载到同一个图中(并添加适当的名称范围以避免命名冲突): 注意:我假设提要是
dict
s,带有tensor\u name:values
或placeholder\u tensor:values
键/值对
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} )