title: ValueError: Tensor Tensor("dense_1/Sigmoid:0", shape=(?, 1), dtype=float32) is not an element of this graph
body: <p>Im using tf.keras to load a model I made previously with tf.keras but when I try to make the prediction I just get this</p>
  
  <pre><code>[ERROR] [1560045312.143498]: bad callback: &lt;function callback at 0x7f16fe94b8c0&gt;
  Traceback (most recent call last):
    File "/opt/ros/kinetic/lib64/python2.7/site-packages/rospy/topics.py", line 750, in _invoke_callback
      cb(msg)
    File "/home/franky/catkin_ws_kinetic/src/tfm/scripts/nnet_predictor.py", line 50, in callback
      true_face.eyes[1].height
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/keras/engine/training.py", line 1113, in predict
      self, x, batch_size=batch_size, verbose=verbose, steps=steps)
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/keras/engine/training_arrays.py", line 195, in model_iteration
      f = _make_execution_function(model, mode)
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/keras/engine/training_arrays.py", line 122, in _make_execution_function
      return model._make_execution_function(mode)
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/keras/engine/training.py", line 1989, in _make_execution_function
      self._make_predict_function()
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/keras/engine/training.py", line 1979, in _make_predict_function
      **kwargs)
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/keras/backend.py", line 3201, in function
      return GraphExecutionFunction(inputs, outputs, updates=updates, **kwargs)
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/keras/backend.py", line 2939, in __init__
      with ops.control_dependencies(self.outputs):
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/framework/ops.py", line 5028, in control_dependencies
      return get_default_graph().control_dependencies(control_inputs)
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/framework/ops.py", line 4528, in control_dependencies
      c = self.as_graph_element(c)
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3478, in as_graph_element
      return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
    File "/usr/lib64/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3557, in _as_graph_element_locked
      raise ValueError("Tensor %s is not an element of this graph." % obj)
  ValueError: Tensor Tensor("dense_1/Sigmoid:0", shape=(?, 1), dtype=float32) is not an element of this graph.
  </code></pre>
  
  <p>I also may say that I am using this in a ros framework (robot operating system [it is not an operating system, just have a super misleading name, I am on linux]) so I know that the callback() is being called in a thread and I can't avoid using ros.</p>
  
  <p>Also I have tested that if I use the prediction function in the main thread, everything works fine.</p>
  
  <p>I have already tried the <code>with graph.as_default():</code> and <code>clear_session()</code> solutions, but no luck.</p>
  
  <p>I have already check that every import is from tf.keras and I'm not mixing tf.keras with keras</p>
  
  <p>I also tried to use a Lock() to avoid having the predict() function being called 2+ at the same time</p>
  
  <pre><code>#!/usr/bin/python2
  from tensorflow import keras
  from tensorflow.keras.models import model_from_json
  from tfm_msgs.msg import IsLooking
  import numpy as np
  from tensorflow.keras.backend import clear_session
  import tensorflow as tf
  from threading import Thread, Lock
  # other non relevant imports
  
  def callback(face_array_stamped):
      global mutex
      mutex.acquire()
      try:
          global graph
          # with graph.as_default():
          global my_model
          global pub
          true_faces = []
          for face in face_array_stamped.faces:
              if len(face.eyes) == 2:
                  true_faces.append(face)
          if len(true_faces) == 1:
              true_face = true_faces[0]
              prediction = my_model.predict(np.array([[
                  #all the data here
                                          ]]))[0]
              #↑↑↑↑↑It crashes here↑↑↑↑↑↑
              #more non relevant stuff
      finally:
          mutex.release()
  
  if __name__ == '__main__':
      # clear_session()
      model_dir = str(os.path.dirname(os.path.abspath(__file__))) + "/../nnet_models/"
      json_file = open(model_dir+'model.json', 'r')
      my_model = model_from_json(json_file.read())
      json_file.close()
      my_model.load_weights(model_dir+'model.h5')
      # my_model._make_predict_function()
      my_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
      # my_model.summary()
  </code></pre>
  
  <p>I would expect the code not to crash</p>