title: How to use multiple keras models in a single thread?
body: <p>I want to use pretrained and saved keras models for prediction. I trained and saved the models to json files in different threads with:</p>
  
  <pre><code>model_json = model.to_json()
  with open("model.json", "w") as json_file:
  json_file.write(model_json)
  </code></pre>
  
  <p>When I tried to use these models, I need to read them one by one and get prediction results. The first one works well. However when it comes to the second model, I read it from json file and run <code>model.predict(dataToPredict)</code>,
  it returns an error indicating that the layers are not the element of the graph. Here is the error:</p>
  
  <p>TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("conv_layer1_input:0", shape=(?, 1182, 1), dtype=float32) is not an element of this graph.</p>
  
  <p>I used </p>
  
  <pre><code>json_file = open(path, 'r')
  loaded_model_json = json_file.read()
  json_file.close()
  with tf.Session():
      model = model_from_json(loaded_model_json)
      predictionResult = model.predict(dataToPredict)
  </code></pre>
  
  <p>for each model and it didn't raise the error this time. However, it seems <strong>weights of the models are randomly generated, not read from the file</strong> or I initialized them by mistake with this <code>with tf.Session():</code>. That's why they produce different results every time I run them with the same data. I also tried using:</p>
  
  <pre><code>with tf.Graph().as_default():
  </code></pre>
  
  <p>without <code>with tf.Session():</code> but it raised the same error again.
  I am confused about "graph" and "session" terms. I thought keras manages both graph and session. If so, what is the problem? Here is my prediction code:</p>
  
  <pre><code>predictions = []
  for modelFilePath in modelFilePaths:
  with tf.Graph().as_default():
      json_file = open(modelFilePath , 'r')
      loaded_model_json = json_file.read()
      json_file.close()
      model = model_from_json(loaded_model_json)
      predictions.append(model.predict(dataToPredict))
  </code></pre>