I am trying to use 2 different models in predict the label of objects on an image captured by video camera. The code is as following:
class Main(object):
_detector = None
_classifier = None
def __init__(self):
np.set_printoptions(suppress=True)
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.45
set_session(tf.Session(config=config))
self._detector = get_detector_model()
self._classifier = get_classifier_model()
def webcam_detect(self):
capture = cv2.VideoCapture(0)
while True:
ret, img = capture.read()
preds = self._detector.predict(img, batch_size=1, verbose=1)
for x in preds:
labels = self._classifier.predict(x)
cv2.putText(img,str(labels), (0, 0), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255),2)
cv2.imshow('img', img)
cv2.waitKey(20)
capture.release()
cv2.destroyAllWindows()
return
if __name__=="__main__":
main = Main()
main.webcam_detect()
However, when I run this code, it throw the following error:
Traceback (most recent call last):
File "main.py", line 133, in <module>
main.webcam_detect()
File "main.py", line 98, in webcam_detect
labels = self._classifier.predict(img)
File "/Users/classifiers/gender_resnet/gender_classifier_resnet.py", line 47, in predict
resnet_preds = self._resnet_model.predict(x)
File "/Users/PythonEnv/lib/python2.7/site-packages/keras/engine/training.py", line 1176, in predict
self._make_predict_function()
File "/Users/PythonEnv/lib/python2.7/site-packages/keras/engine/training.py", line 734, in _make_predict_function
**kwargs)
File "/Users/PythonEnv/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1031, in function
return Function(inputs, outputs, updates=updates)
File "/Users/PythonEnv/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 992, in __init__
with tf.control_dependencies(self.outputs):
File "/Users/PythonEnv/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3615, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "/Users/PythonEnv/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3346, in control_dependencies
c = self.as_graph_element(c)
File "/Users/PythonEnv/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2458, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "/Users/PythonEnv/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2537, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("AvgPool:0", shape=(?, ?, ?, 2048), dtype=float32) is not an element of this graph.
My guess is that the opencv creates a new thread for the video capture and run the _classifier in a different thread. Since Tensorflow runs in a session, the second model _classifier throws the error.
If I need to run multiple models with Keras using Tensorflow backend, what should I do to make this work?
Thanks a lot.
Please make sure that the boxes below are checked before you submit your issue. Thank you!
[x] Check that you are up-to-date with the master branch of Keras. You can update with:
pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps
[x] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps
[x] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).
i face the same problem as yours,if you have solved it,please inform me, thank you in advance
https://github.com/fchollet/keras/issues/2397 this issue may help
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
Most helpful comment
https://github.com/fchollet/keras/issues/2397 this issue may help