python - 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..
解决方案
是的,这绝对有可能,但你做错了。不要在两个单独的图中定义两个模型,只需将它们加载到同一个图中(并添加正确的名称范围以避免命名冲突):
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} )
注意:我假设提要是dict
stensor_name:values
或placeholder_tensor:values
键/值对
推荐阅读
- javascript - 使用 ng-repeat 根据来自不同对象的 id 绑定一组对象的值
- node.js - 如何设置列表样式以强制提示为按钮?
- php - 非基本案例如何在递归中工作?
- sql - 如何在 T-SQL 中选择 24 小时内的 MIN 和 MAX 日期时间
- android-studio - 当我在 android studio 中运行 flutter build apk 命令时,出现 AndroidX 不兼容问题
- robotics - 如果有新文件,RPA 机器人如何不断查看文件夹?
- java - 长时间运行的调度程序抛出异常访问 jpa/ejb 对象
- node.js - NodeJS + Graphql:即使在发送响应后我如何运行代码?
- laravel - mb_strpos() 期望参数 1 是字符串,在 Laravel 中查询 2 个表时给出的对象
- arrays - 如何从Matlab中的变量中获取单元格编号时在excel中读取一行?