How to run a trained Tensorflow model on Cloud Foundry

AI models are becoming more and more commodity nowadays. This entry is focusing on, how you can train a Tensorflow model offline and push it to Cloud Foundry to provide your model as an API. Our starting point is the very good beginner sample for MNIST image recognition from Tensorflow itself https://www.tensorflow.org/get_started/mnist/beginners

You can get the sources of this examples from my Github repository. https://github.com/cgrotz/tensorflow-cloudfoundy

The first thing to do, is to add the following lines at the end of your training code:

This saves the currently trained model into the folder model as the trained checkpoint. Which allows you to reload the model later.

The next step is to create a web application that is able to load the model and provide an API to it. For this we are going to use Flask. Flask is a microframework for Python, that provides the possibility to quickly and easily provide HTTP APIs. To install flask you can simply use pip (e.g. pip install Flask).

You can see the whole code in the run.py file. I’m going to highlight the main points here.

First of all, we need to normally init the variables and model we are going to run.

Next we are going to load the previously stored value for the variables. This recreates the model we previously trained.

Afterwards we create our Flask request handler.

That’s all, if you have installed Tensorflow and Flask you can now simply run python run.py  to start the API locally.

The next step is to deploy our simple application to Cloud Foundry. I assume you already have either a Cloud Foundry installation up and running or have an account with run.pivotal.io. Also you should have setup your cf-cli tool to point to this installation.

We are going to add Gunicorn. Gunicornis a Python HTTP Server and provides a pre-fork worker model. The Gunicorn server provides a great basis for Python web applications with it’s worker model and is widely used. And a great match for running Python on Cloud Foundry.

Our application is now like every other Python app, we want to run on Cloud Foundry. There are 3 key files, that we need to know about, in order for the Cloud Foundry buildpack to identify our app as a Python app and package it correctly.

requirements.txt – This file contains the libraries that PIP should install. PIP is run by the Python buildpack while creating the droplet. The content should look like the following lines:

runtime.txt – This file contains which Python version to use. Our example needs python 3.4 to run. The content should look like the following lines:

Procfile – This file gives the command line with which the buildpack should start the application

When you have configured those files, you can simple push your app using cf-cli from the folder:

Now let’s make a test request to our  AI API. For example the following json array is the letter 3 or at least my painting of the letter 3 using paint and a touchpad.


You can simply POST it against your newly created Cloud Foundry app,  just set the Content-Type header to application/json so that the content is automatically read. The response should look like the following

In this call the 3was actually correctly detected, hurray. Of course you can make the API fancier and maybe add some authentication and authorisation to it. Maybe you also want to add a billing option to earn some money with your awesome model.

Ideally you automate the training of the model using for example Jenkins to automatically re-train and re-deploy the model if the newer version has a higher accuracy.

Leave a Reply