我正在尝试构建一个程序,该程序可以确定我伸出的手指数。我有一个经过训练的单发探测器,可以在我的手周围画一个边界框。盒子被裁剪出来并送入一个卷积网络,该卷积网络经过训练以确定我伸出的手指数。他们都经过培训,可以独立工作。但是,将它们放在同一脚本中似乎没有任何作用。我收到此错误:
ValueError: Tensor Tensor("softmax_1/Softmax:0", shape=(?, 6), dtype=float32)
is not an element of this graph.
我认为这与我同时运行两个神经网络有关。
编译convnet模型后,我尝试编写model._make_predict_function()
。但是我得到了:
File "/Users/spencerkraisler/Desktop/Hand_Sign_Classifier_2/hand_detector.py", line 66, in <module>
pred = model.predict(box_image_exp)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 1169, in predict
steps=steps)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_arrays.py", line 294, in predict_loop
batch_outs = f(ins_batch)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2715, in __call__
return self._call(inputs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2671, in _call
session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2623, in _make_callable
callable_fn = session._make_callable_from_options(callable_opts)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1471, in _make_callable_from_options
return BaseSession._Callable(self, callable_options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1425, in __init__
session._session, options_ptr, status)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Tensor conv2d_1_input:0, specified in either feed_devices or fetch_devices was not found in the Graph
Exception ignored in: <bound method BaseSession._Callable.__del__ of <tensorflow.python.client.session.BaseSession._Callable object at 0x136f91320>>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1455, in __del__
self._session._session, self._handle, status)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.CancelledError: Session has been closed.
代码:
import cv2
import os
import tensorflow as tf
import numpy as np
from keras.models import Sequential, Model
from keras import metrics
from keras.layers import Conv2D, MaxPooling2D, Softmax
from keras.layers import Activation, Dropout, Flatten, Dense, ReLU
from keras.optimizers import Adam
from utils import detector_utils as detector_utils
# initializing convnet
optimizer = Adam(lr=.01)
input_shape = (28, 28, 1)
num_classes = 6
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), input_shape=input_shape))
model.add(ReLU())
model.add(Conv2D(64, kernel_size=(3, 3)))
model.add(ReLU())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(ReLU())
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Softmax())
model.load_weights("./convnet/classifier1.h5")
model.summary()
model.compile(
loss='categorical_crossentropy',
optimizer=optimizer,
metrics=[metrics.categorical_accuracy])
# initializing ssd
detection_graph, sess = detector_utils.load_inference_graph()
score_thresh = 0.2
im_width, im_height = 1280, 720
num_hands_detect = 1
def load_graph(frozen_graph_filename):
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name='prefix')
return graph
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
cap = cv2.VideoCapture(0)
while(True):
_, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
boxes, scores = detector_utils.detect_objects(frame, detection_graph, sess)
box_image = detector_utils.get_box_image(1, score_thresh, scores, boxes, im_width, im_height, frame)
detector_utils.draw_box_on_image(1, score_thresh, scores, boxes, im_width, im_height, frame)
# this mean a hand was detected
if box_image is not None:
box_image = cv2.cvtColor(box_image, cv2.COLOR_BGR2GRAY)
box_image = cv2.resize(box_image, (28, 28))
box_image_exp = np.expand_dims(box_image, 2)
box_image_exp = np.expand_dims(box_image_exp, 0)
# where the convnet makes its prediction
pred = model.predict(box_image_exp)
print(np.argmax(pred))
cv2.imshow('frame', cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
if box_image is not None:
cv2.imshow('hand detection', box_image)
else:
cv2.imshow('hand detection', np.zeros((28, 28, 1)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
每当SSD侦测到我的手时,该脚本应该会打印出convnet的预测。
答案 0 :(得分:0)
我相信问题的根源在于您在默认图下创建了Keras模型,但在detection_graph
范围内调用了它。有几种解决方法。我将尝试将模型创建代码(从model = Sequential()
到model.compile(...)
)包装到一个函数中,并在with detection_graph.as_default():
下调用该函数。另外,请确保将Keras后端会话设置为您明确创建的会话。因此,像这样:
import cv2
...
from keras.optimizers import Adam
from keras import backend as K
from utils import detector_utils as detector_utils
# initializing convnet
optimizer = Adam(lr=.01)
input_shape = (28, 28, 1)
num_classes = 6
def build_keras_model():
model = Sequential()
...
return model
# initializing ssd
detection_graph, sess = detector_utils.load_inference_graph()
...
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
K.set_session(sess)
model = build_keras_model()
model.compile(
loss='categorical_crossentropy',
optimizer=optimizer,
metrics=[metrics.categorical_accuracy])
cap = cv2.VideoCapture(0)
while(True):
_, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
boxes, scores = detector_utils.detect_objects(frame, detection_graph, sess)
box_image = detector_utils.get_box_image(1, score_thresh, scores, boxes, im_width, im_height, frame)
detector_utils.draw_box_on_image(1, score_thresh, scores, boxes, im_width, im_height, frame)
# this mean a hand was detected
if box_image is not None:
box_image = cv2.cvtColor(box_image, cv2.COLOR_BGR2GRAY)
box_image = cv2.resize(box_image, (28, 28))
box_image_exp = np.expand_dims(box_image, 2)
box_image_exp = np.expand_dims(box_image_exp, 0)
# where the convnet makes its prediction
pred = model.predict(box_image_exp)
print(np.argmax(pred))
cv2.imshow('frame', cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
if box_image is not None:
cv2.imshow('hand detection', box_image)
else:
cv2.imshow('hand detection', np.zeros((28, 28, 1)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()