gpt4 book ai didi

python - 将一张图复制到另一张图

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:02 24 4
gpt4 key购买 nike

我想将加载的图表复制到另一个图表。这就是我正在尝试做的事情

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 (已弃用),如下所示:

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)

但这引发了我以下错误:

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

最佳答案

感谢解答here ,我能够使用 export_meta_graphimport_scoped_meta_graph 解决该问题。

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

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

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

以上所有张量均导入到默认图中。

关于python - 将一张图复制到另一张图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58568663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com