title: How to share a Tensorflow Keras model with a Flask route function?
body: <p>Does tensorflow maintains its own internal global state, which is broken by loading the model in one function and trying to use it in another?</p>
  
  <p>Using singleton for storing model:</p>
  
  <pre><code>class Singleton(object):
      _instances = {}
  
      def __new__(class_, *args, **kwargs):
          if class_ not in class_._instances:
              class_._instances[class_] = super(Singleton, class_).__new__(class_, *args, **kwargs)
          return class_._instances[class_]
  
  
  class Context(Singleton):
      pass
  </code></pre>
  
  <p>When I do:</p>
  
  <pre><code>@app.route('/file', methods=['GET', 'POST'])
  def upload_file():
      if request.method == 'POST':
          file = request.files['file']
          if file and allowed_file(file.filename):
              # filename = secure_filename(file.filename)
              filename = file.filename
              filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
              file.save(filepath)
  
              context = Context()
              if context.loaded:
  
                  img = cv2.imread(filepath)
                  img = cv2.resize(img, (96, 96))
                  img = img.astype("float") / 255.0
                  img = img_to_array(img)
                  img = np.expand_dims(img, axis=0)
  
                  classes = context.model.predict(img)
  
  def api_run():
      context = Context()
      context.model = load_model('model.h5')
      context.loaded = True
  </code></pre>
  
  <p>I'm getting some error: <code>ValueError: Tensor Tensor("dense_1/Softmax:0", shape=(?, 2), dtype=float32) is not an element of this graph.</code></p>
  
  <p>However if I will move <code>context.model = load_model('model.h5')</code> inside <code>upload_file</code> function then everything will work. Why is that happening? How to store model for later use?</p>