Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
test-images Added more test images in deploy_mnist project Apr 8, 2019 Update Apr 8, 2019
requirements.txt Added project to show how to deploy the model to production Apr 6, 2019 Added project to show how to deploy the model to production Apr 6, 2019

The purpose of this project is to show how to move your machine learning models in production. In this project, we'll train the MNIST model, save the model to the file, load the model from the file in the flask app and predict the digit for the new images. Since input images in MNIST are 28x28 greyscale images, the images used for predictions have to be processed. They should be converted to greyscale and resized to 28x28 pixels. Because of this, you may not get the accuracy in predictions but you will learn how to move your model to production (and which is the sole objective of this project).

We'll use Flask for exposing the model using the REST API for predictions. Flask is a micro web framework written in Python. It's lightweight and easy to learn.


Clone to repository

git clone

Set the Python path - On CloudxLab, the default installation is python2

export PATH=/usr/local/anaconda/bin/:$PATH

Create virtual environment

cd ml/projects/deploy_mnist/
virtualenv -p python3 venv

Activate virtual environment

source venv/bin/activate

Install the flask and other requirements

pip install -r requirements.txt

Train the model

The trained model will be saved in trained_models directory

mkdir -p trained_models

Start the flask server for predictions

For the API code, see the file under flask_app directory. Run the server on port 4041. If the port is already in use then use any of the port in the range of 4040 to 4060 as on CloudxLab only these ports are open for public access.

cd flask_app
export LC_ALL=en_US.utf-8
export LANG=en_US.utf-8
flask run --host --port 4041

Predict the digit for the new image

We will use the test images for predictions. Login to another console and run below commands.

cd ml/projects/deploy_mnist/
curl -F 'file=@test-images/7.png'

The REST API will return something like below JSON object


Public API

Your flask server is running on the CloudxLab web console. Let's say your web console is then the end Point URL will be

You can call/use this REST API by using the above mentioned End Point URL.

Replace 4041 with the port number on which your server is running.

Next Steps

The above flask server runs in the development mode. For production usage, you would like to run the server using Nginx and uWSGI. For details please follow this documentation

You can’t perform that action at this time.