This repository was created to help clarify how to utilise flask and gunicorn to easily deploy a python/keras deep learning model as a web app on Heroku. This example features code for online deployment of a binary medical image classification model, based on convolutional neural network architecture. The CNN has two hidden layers and has been trained on the following chest x-ray image dataset for pneumonia: Kermany Daniel, Zhang Kang, Goldbaum Michael (2018). Labeled Optical Coherence Tomography (OCT) and Chest X-Ray Images for Classification. Mendeley Data, v2, http://dx.doi.org/10.17632/rscbjbr9sj.2. The trained model achieved accuracy of more than 86% on the test set and its weights have been saved in the Models folder (see file: trained_model.h5) in the very useful HDF5 format. You may use your own saved trained model! Just make sure you put it in the Models folder and name it appropriately so that the flask app may call it.
A JavaScript app running on the browser calls the Flask app (app.py) to load the model weights and return results to the JavaScript app (through the 'GET' and 'POST' methods). More details may be found at: Deploy Keras Neural Network to Flask web service | Part 1 - Overview
This web application has been created using code developed by Xin Fu (please see the following repository: https://github.com/mtobeiyf/keras-flask-deploy-webapp. The changes to what Xin Fu had already prepared were the following:
- Inclusion of a procfile to serve the app with gunicorn on the Heroku server after upload (for uploading this python app to Heroku please follow Heroku documentation on how to use git with Heroku)
- As per Heroku requirements for stability and reproducibility, versions of all required python environments were specified in the requirements.txt file
- Inclusion of gunicorn to the requirements.txt file
- Changed the app.py (flask app) file to adapt it to the required functionality according to the trained binary image classification model. The program was also modified to delete every uploaded image after providing the prediction. This will prevent exceeding capacity limits on Heroku servers. The last lines of the file have been modified to work with gunicorn. The gevent serving code lines have been commented out, but are still there in case someone would like to run the code locally as Xin Fu explained in the README of his repo https://github.com/mtobeiyf/keras-flask-deploy-webapp
- The Index.html and base.html files have been modified accordingly to include references and information about the model
- This README file has been adapted to provide instructions about heroku deployment. The part for customisation has been modified also
To create a web app that runs locally, please see Xin Fu's repo (https://github.com/mtobeiyf/keras-flask-deploy-webapp) and follow instructions in the README file, to run the model locally. Gevent or gunicorn may be used for local deployment too.
Place your trained keras deep learning model to the models directory.
See Keras applications for more available models such as DenseNet, MobilNet, NASNet, etc.
Modify files in templates
and static
directory.
index.html
, base.html
for the UI and main.js
for all the behaviors
To deploy it for public use, you need to upload this app on heroku or other python enabled server. However heroku is pretty good at recognising and running python apps.
Check Siraj's "How to Deploy a Keras Model to Production" video. The corresponding repo.
Building a simple Keras + deep learning REST API.
How to deploy a Flask App to Heroku.
"# deep-learning-pneumonia-detection" "# deep-learning-pneumonia-detection-web-app"