title: Loading a huge Keras Model into a Flask App
body: <p>I'm building a small Flask app that uses a convolutional neural net behind the scenes to make predictions on user uploaded images. It works if I load it like this:</p>
  
  <pre><code>@app.route("/uploader", methods=["GET","POST"])
  def get_image():
      if request.method == 'POST':
          f = request.files['file']
          sfname = 'static/'+str(secure_filename(f.filename))
          f.save(sfname)
          clf = catdog.classifier()
          return render_template('result.html', pred = clf.predict(sfname), imgpath = sfname)
  </code></pre>
  
  <p>However, this requires the the classifier (clf) be loaded after the user adds the image. This takes a while, as it requires setting up all weights for a 200+ layer neural network from a pickle file. </p>
  
  <p>What I want to do is load all the weights when the app is spawned. To do this, I've tried this (cutting out unrelated code for HTML templates/imports/app launch):</p>
  
  <pre><code># put model into memory on spawn
  clf = catdog.classifier()
  # Initialize the app
  app = flask.Flask(__name__)
  
  @app.route("/uploader", methods=["GET","POST"])
  def get_image():
      if request.method == 'POST':
          f = request.files['file']
          sfname = 'static/'+str(secure_filename(f.filename))
          f.save(sfname)
          return render_template('result.html', pred = clf.predict(sfname), imgpath = sfname)
  </code></pre>
  
  <p>When I do this, I get this traceback (skipping all the flask specific traces at the top):</p>
  
  <pre><code> File "/Users/zachariahmiller/Documents/Metis/test_area/flask_catdog/flask_backend.py", line 26, in get_image
      return render_template('result.html', pred = clf.predict(sfname), imgpath = sfname)
    File "/Users/zachariahmiller/Documents/Metis/test_area/flask_catdog/catdog.py", line 56, in predict
      prediction = self.model.predict(img_to_predict, batch_size=1, verbose=1)
    File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 1569, in predict
      self._make_predict_function()
    File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 1037, in _make_predict_function
      **kwargs)
    File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2095, in function
      return Function(inputs, outputs, updates=updates)
    File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2049, in __init__
      with tf.control_dependencies(self.outputs):
    File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3583, in control_dependencies
      return get_default_graph().control_dependencies(control_inputs)
    File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3314, in control_dependencies
      c = self.as_graph_element(c)
    File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2405, in as_graph_element
      return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
    File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2484, in _as_graph_element_locked
      raise ValueError("Tensor %s is not an element of this graph." % obj)
  ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 2), dtype=float32) is not an element of this graph.
  </code></pre>
  
  <p>I'm not sure why loading the classifier outside of the specific call as a global object to the app makes it fail. It should be in memory, and I've seen other examples of folks doing this with SKLearn Classifiers. Any ideas on why this causes this error?</p>