Я пытаюсь создать программу, которая может определить, сколько пальцев я держу на руке.У меня есть детектор одиночного выстрела, обученный рисовать ограничивающую рамку вокруг моей руки.Коробка обрезается и подается в коннет, который обучен определять, сколько пальцев я держу.Они оба обучены и работают индивидуально, просто отлично.Однако, помещая их обоих в один и тот же сценарий, кажется, ничего не работает.Я получаю эту ошибку:
ValueError: Tensor Tensor("softmax_1/Softmax:0", shape=(?, 6), dtype=float32)
is not an element of this graph.
Я считаю, что это как-то связано с тем, как я запускаю две нейронные сети одновременно.
Я попытался написать model._make_predict_function()
после того, как скомпилировал модель convnet.И все же я получил это:
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()
Этот сценарий должен распечатывать прогнозы Connet, когда SSD обнаруживает мою руку.