gpt4 book ai didi

python - TypeError : Cannot interpret feed_dict key as Tensor,需要了解 session 和图表

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

我是 tensorflow 新手,并使用移动设备发布此内容。我实际上希望 session 保持事件状态,因此使用了以下编码方式。但是,在一个地方我可以看到占位符节点存在,但出现错误,表明该节点不存在。我怀疑是否使用同一张图表。

class Model(object):

def __init__(self, model_path):
self.sess = tf.Session()
self.graph = tf.Graph()
self.graph_def = tf.GraphDef()

with open(model_path, "rb") as f:
self.graph_def.ParseFromString(f.read())

with tf.Graph().as_default() as self.graph:
tf.import_graph_def(self.graph_def)

self.input_operation = self.graph.get_operation_by_name('import/Placeholder')
self.output_operation = self.graph.get_operation_by_name('import/final_result')

def predict(self, images):
dims_expander = tf.expand_dims(images, 0)
resized = tf.image.resize_bilinear(dims_expander, [299, 299])
normalized = tf.divide(tf.subtract(resized, [0]), [255])
print(normalized)
for op in self.graph.get_operations():
print(op.name)
results = self.sess.run(self.output_operation.outputs[0], {self.input_operation.outputs[0]: self.sess.run(normalized)})
results = np.squeeze(results)
top_k = results.argsort()[-5:][::-1]
labels = ['1','0']
print(labels[top_k[0]], results[top_k[0]])

然后制作一个对象。

model = Model('identification_model.pb').predict(img_from_openCV)

以下是输出错误,包括打印第一个要导入/占位符的节点的打印语句

Tensor("truediv_13:0", shape=(1, 299, 299, 3), dtype=float32)
import/Placeholder
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1091 subfeed_t = self.graph.as_graph_element(
-> 1092 subfeed, allow_tensor=True, allow_operation=False)
1093 except Exception as e:

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in as_graph_element(self, obj, allow_tensor, allow_operation)
3489 with self._lock:
-> 3490 return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
3491

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation)
3568 if obj.graph is not self:
-> 3569 raise ValueError("Tensor %s is not an element of this graph." % obj)
3570 return obj

ValueError: Tensor Tensor("import/Placeholder:0", shape=(?, 299, 299, 3), dtype=float32) is not an element of this graph.

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)

----> 1 model = Model('identification_model.pb').predict(img)


32 for op in self.graph.get_operations():
33 print(op.name)
---> 34 results = self.sess.run(self.output_operation.outputs[0], {self.input_operation.outputs[0]: self.sess.run(normalized)})
35 results = np.squeeze(results)
36 top_k = results.argsort()[-5:][::-1]

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
927 try:
928 result = self._run(None, fetches, feed_dict, options_ptr,
--> 929 run_metadata_ptr)
930 if run_metadata:
931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1093 except Exception as e:
1094 raise TypeError(
-> 1095 'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
1096
1097 if isinstance(subfeed_val, ops.Tensor):

TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("import/Placeholder:0", shape=(?, 299, 299, 3), dtype=float32) is not an element of this graph.

最佳答案

当您创建 session 时,默认启动默认图,其中不包含您要查找的操作(它们都在 self.graph 中)。你应该做这样的事情:

with tf.Session(self.graph) as sess:
sess.run(...)

现在,sess 将可以访问 self.input_operationself.output_operation。在您的代码中,这意味着您应该在创建 self.graph 之后创建 session 。顺便说一句,大多数时候使用默认图表会更方便(正是为了避免此类问题)。可能是this post也会在这方面为您提供帮助。

关于python - TypeError : Cannot interpret feed_dict key as Tensor,需要了解 session 和图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54865209/

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