title: Fixing Flask timeout due to instantiation of large Keras model on Heroku
body: <p>So I have a simple demo Flask application meant to serve a Keras model that I trained. The application works fine running locally on my computer but often times out on Heroku. In the application the model is loaded when Flask is started. This makes the most sense to as then we don't have to reload the model on each request. Normally this would be fine since Flask would run continuously. However, given this is a free Heroku app, Heroku deactivates my instance after 30 minutes. This means each time the app has to reload the model from scratch which causes a timeout error due to the model being instantiated (which often takes 20+ seconds) (occasionally it also has an out of memory error). I would like to either reduce the model load time if possible. Barring that find another way to avoid timeout on Heroku without upgrading (i.e multi-threading perhaps). I realize I could host my model on a GPU on AWS or something then call it, but I would like to keep it as simple and cost effective as possible. It's not meant to serve 1000s of requests but merely work when 1 or 2 people click the link from my paper.</p>
  
  <p>Code is below.</p>
  
  <pre><code>import os
  from flask import Flask, redirect, url_for, request, render_template, send_from_directory
  from werkzeug import secure_filename
  from examples2.example_keras import SimpleResNet50, ResNet2
  
  # folder to upload pictures
  UPLOAD_FOLDER = 'uploads/'
  # what files can upload
  ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
  
  # start + config
  app = Flask(__name__)
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  app.config['ALLOWED_EXTENSIONS']=ALLOWED_EXTENSIONS
  model = ResNet2("long_path/model_weights.h5")
  # main route
  @app.route('/')
  def index():
      return render_template('upload.html')
  </code></pre>
  
  <p>Example error code</p>
  
  <pre><code>at=error code=H12 desc="Request timeout" method=GET path="/" host=lung-r.herokuapp.com request_id=79bfba10-810e-444d-a73a-d32bcd68d603 fwd="24.198.104.217" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https
  2018-06-25T05:22:18.893433+00:00 app[web.1]: load model weights_path:
  </code></pre>