A better way to deploy Keras models
Streamlined web deployments of Keras models with preprocessing layers
--
Three years ago I published a story about deploying a Keras model using Flask. In that story I described how to set up a Flask server and a simple set of HTML pages to create a web deployment for a Keras model trained on a tabular dataset. This approach worked and provided a relatively simple way to demonstrate web deployment of a Keras model, but it had some significant compromises. I recently had the opportunity to attack the same problem — web deployment of a Keras model-and I found it much easier thanks to Keras preprocessing layers. In this article I’ll describe what was wrong with my original approach and how the preprocessing layers make web deployment of Keras models better.
Problems with the original approach to web deployment with Flask
The approach to deploying Keras models with Flask that I described three years ago involved some messy steps to get around the problem of ensuring that the same processing was applied to data at inference time as was applied at training time. I defined a set of classes to fit into the scikit learn pipeline approach to do the data processing, including assigning numeric values to categorical values, and used these classes to define a pipeline that could be used at training time and again in the deployment.
The code to define the pipelines at training time, as shown in this gist, was particularly complicated:
I wasn’t able to get all the data preprocessing to work in a single pipeline, so I had to create two pipeline objects. To get the whole thing to work, I needed to define the pipeline classes in a separate file that could be imported into both the training code and the deployment code, and I needed to load both pipeline objects in…