Python 如何创建嵌套的tensorflow结构?

Python 如何创建嵌套的tensorflow结构?,python,tensorflow,graph,keras,Python,Tensorflow,Graph,Keras,我无法使用Tensorflow后端创建嵌套模式。我想要实现的是在一个会话中使用两个模型。我训练了一个模型,想在SSD COCO对象检测模型中使用它。顺便说一下,我在图形和会话之外分配模型(以便加载模型一次)。每当我尝试运行代码时,都会返回如下错误: ValueError:张量张量(“激活_76/Softmax:0”,形状=(?,4), dtype=float32)不是此图的元素 我搜索其他堆栈溢出页面,但我无法解决我的问题。 有人能帮我理解问题和解决方法吗 后来我在网上搜索,找到了这个网站:

我无法使用Tensorflow后端创建嵌套模式。我想要实现的是在一个会话中使用两个模型。我训练了一个模型,想在SSD COCO对象检测模型中使用它。顺便说一下,我在图形和会话之外分配模型(以便加载模型一次)。每当我尝试运行代码时,都会返回如下错误:

ValueError:张量张量(“激活_76/Softmax:0”,形状=(?,4), dtype=float32)不是此图的元素

我搜索其他堆栈溢出页面,但我无法解决我的问题。 有人能帮我理解问题和解决方法吗

后来我在网上搜索,找到了这个网站:

然后我尝试了“model.\u make\u predict\u function()”这一行,但它对我不起作用,这次它返回了一个:

ValueError:Tensor(“占位符:0”,dtype=float32)必须来自 与张量相同的图形(“总计:0”,形状=(),数据类型=资源)


你有没有试过把
model=tf.keras.models.load\u model(“CNN-3”)
放在带有检测图的
中。as\u default():
上下文?我试过了,就像你说的那样,它对我不起作用。我试图用
tf.Session(graph=detection\u graph)作为sess:
放在后面,在While循环之前,它对我有效。我真的很感谢你的回答@frankegoesdown,因为我几乎有一天都在试图解决这个问题。你有没有试过把
model=tf.keras.models.load\u model(“CNN-3”)
放在
的检测图中。as\u default()。我试图用
tf.Session(graph=detection\u graph)作为sess:
放在后面,在While循环之前,它对我有效。我真的很感激你的回答@frankegoesdown,因为我几乎有一天在试图解决这个问题。
model = tf.keras.models.load_model("CNN-3")

with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    while True:


      ret, image_np = cap.read()

      if W is None or H is None:
          (H, W)= image_np.shape[:2]


      image_np_expanded = np.expand_dims(image_np, axis=0)
      image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

      boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
      scores = detection_graph.get_tensor_by_name('detection_scores:0')

      classes = detection_graph.get_tensor_by_name('detection_classes:0')
      num_detections = \
              detection_graph.get_tensor_by_name('num_detections:0')



      (boxes, scores, classes, num_detections) = sess.run(
          [boxes, scores, classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})

      boxes = np.squeeze(boxes)
      scores = np.squeeze(scores)
      classes = np.squeeze(classes)

      indices = np.argwhere(classes == 1)
      boxes = np.squeeze(boxes[indices])

      scores = np.squeeze(scores[indices])
      classes = np.squeeze(classes[indices])

      rects = []
      boxes = scale(boxes, 0, 1)

      my_prediction = model.predict("Some images")

      """ ^ these codes return an error """