首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将一个图表复制到另一个图表

将一个图表复制到另一个图表
EN

Stack Overflow用户
提问于 2019-10-26 14:45:45
回答 1查看 72关注 0票数 1

我想将一个加载的图复制到另一个图中。这就是我想要做的

代码语言:javascript
运行
复制
import tensorflow as tf
import numpy as np
import cv2

input_names = ['image_tensor']
pb_fname1 = "/Users/vedanshu/frozen_graph/ssd_tomato_l1_frozen_graph.pb"

def get_frozen_graph(graph_file):
    """Read Frozen Graph file from disk."""
    with tf.gfile.FastGFile(graph_file, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    return graph_def

trt_graph1 = get_frozen_graph(pb_fname1)

detection_graph1 = tf.Graph()
with detection_graph1.as_default():
    tf.import_graph_def(trt_graph1, name='')
    tf_sess1 = tf.Session(graph=detection_graph1)

tf_input1 = tf_sess1.graph.get_tensor_by_name(input_names[0] + ':0')
tf_scores1 = tf_sess1.graph.get_tensor_by_name('detection_scores:0')
tf_boxes1 = tf_sess1.graph.get_tensor_by_name('detection_boxes:0')
tf_classes1 = tf_sess1.graph.get_tensor_by_name('detection_classes:0')
tf_num_detections1 = tf_sess1.graph.get_tensor_by_name('num_detections:0')

现在,我想将tf_input1tf_scores1tf_boxes1tf_num_detections1复制到另一个图中。目前我正在尝试使用copy_op_to_graph (废弃的),如下所示:

代码语言:javascript
运行
复制
detection_graph2 = tf.Graph()

namespace = "Ved"
copied_variables = []

tf_num_detections1_copy = tf.contrib.copy_graph.copy_op_to_graph(tf_num_detections1, detection_graph2,copied_variables, namespace)

但这给我抛出了以下错误:

代码语言:javascript
运行
复制
WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
  * https://github.com/tensorflow/addons
If you depend on functionality not listed there, please file an issue.

---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-9-807d2dc0bb17> in <module>
      3 # grid_roies_copy = tf.contrib.copy_graph.copy_op_to_graph(grid_roies, detection_graph2, copied_variables, namespace)
      4 # tf_input1_copy = tf.contrib.copy_graph.copy_op_to_graph(tf_input1, detection_graph2,copied_variables, namespace)
----> 5 tf_num_detections1_copy = tf.contrib.copy_graph.copy_op_to_graph(tf_num_detections1, detection_graph2,copied_variables, namespace)

/usr/local/lib/python3.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py in copy_op_to_graph(org_instance, to_graph, variables, scope)
    167     #output.
    168     op = org_instance.op
--> 169     new_op = copy_op_to_graph(op, to_graph, variables, scope)
    170     output_index = op.outputs.index(org_instance)
    171     new_tensor = new_op.outputs[output_index]

/usr/local/lib/python3.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py in copy_op_to_graph(org_instance, to_graph, variables, scope)
    195     #If it has inputs, call this function recursively on each.
    196     new_inputs = [
--> 197         copy_op_to_graph(x, to_graph, variables, scope) for x in op.inputs
    198     ]
    199 

/usr/local/lib/python3.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py in <listcomp>(.0)
    195     #If it has inputs, call this function recursively on each.
    196     new_inputs = [
--> 197         copy_op_to_graph(x, to_graph, variables, scope) for x in op.inputs
    198     ]
    199 

... last 3 frames repeated, from the frame below ...

/usr/local/lib/python3.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py in copy_op_to_graph(org_instance, to_graph, variables, scope)
    167     #output.
    168     op = org_instance.op
--> 169     new_op = copy_op_to_graph(op, to_graph, variables, scope)
    170     output_index = op.outputs.index(org_instance)
    171     new_tensor = new_op.outputs[output_index]

RecursionError: maximum recursion depth exceeded while calling a Python object
EN

回答 1

Stack Overflow用户

发布于 2019-10-26 21:51:42

多亏了答案here,我才能用export_meta_graphimport_scoped_meta_graph解决这个问题。

代码语言:javascript
运行
复制
import tensorflow as tf
import numpy as np
import cv2

input_names = ['image_tensor']
pb_fname1 = "/Users/vedanshu/frozen_graph/ssd_tomato_l1_frozen_graph.pb"

def get_frozen_graph(graph_file):
    """Read Frozen Graph file from disk."""
    with tf.gfile.FastGFile(graph_file, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    return graph_def

trt_graph1 = get_frozen_graph(pb_fname1)

detection_graph1 = tf.Graph()
with detection_graph1.as_default():
    tf.import_graph_def(trt_graph1, name='')
    tf_sess1 = tf.Session(graph=detection_graph1)

from tensorflow.python.framework import meta_graph

graph = tf.get_default_graph()
tf_sess_main = tf.Session(graph=graph)

meta_graph1 = tf.train.export_meta_graph(graph=detection_graph1)
meta_graph.import_scoped_meta_graph(meta_graph1, import_scope='ved_graph1',)

# Print the name of all Ops in default graph

for op in tf.get_default_graph().get_operations():
    print(op.name)
print()

现在我可以将张量从一个图导入到另一个图,如下所示:

代码语言:javascript
运行
复制
tf_input1 = graph.get_tensor_by_name('ved_graph1/image_tensor:0')
tf_scores1 = graph.get_tensor_by_name('ved_graph1/detection_scores:0')
tf_boxes1 = graph.get_tensor_by_name('ved_graph1/detection_boxes:0')
tf_classes1 = graph.get_tensor_by_name('ved_graph1/detection_classes:0')
tf_num_detections1 = graph.get_tensor_by_name('ved_graph1/num_detections:0')

所有上述张量都会导入到默认图中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58568663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档