我在使用Tensorflow后端创建嵌套模式时遇到麻烦。我要实现的是在一个会话中使用两个模型。我训练了一个模型,并想在SSD COCO对象检测模型中使用它。顺便说一下,我在图和会话之外分配模型(以便一次加载模型)。每当我尝试运行代码时,都将返回错误,例如:
ValueError:张量Tensor(“ activation_76 / Softmax:0”,shape =(?, 4), dtype = float32)不是该图的元素。
我搜索其他堆栈溢出页面,但是无法解决我的问题。 有人可以帮助我了解问题和解决方案吗?
后来我在网上搜索并找到了该网站:
https://github.com/keras-team/keras/issues/6462
然后我尝试了“ model._make_predict_function()”这一行,但是它对我不起作用,这次返回了那个:
ValueError:Tensor(“ Placeholder:0”,dtype = float32)必须来自 与Tensor(“ total:0”,shape =(),dtype = resource)相同的图形。
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 """