It would be nice if we could use our trained models and create applications with it, don’t you think? I have good news: we can, and it is really easy, fast and free! 😉

I stumbled upon on a site called Heroku when I created a Twitter Bot which uses OpenCV and Dlib to morph faces if somebody sends two photos about persons (I will write about that too).

We can easily deploy our Python (Java, Ruby, Go, Php, …) applications to the site. So why not use it for Deep Learning? Of course we wont’t train our net there but with the trained model we can do very cool stuff in just minutes or hours! I thought I will create a tutorial with a simple examaple and the Iris dataset. I used Keras but I think you can use every Deep Learning framework (but I am not tested it).

This won’t be a super exhausting tutorial because I included my code and I just wanted to show you how can we use Heroku and deep learning to create super awesome apps. Let’s jump into it! 😛

Train the model

First of all we need to train our model, so we can use it later. I will skip this part here because we can find a lot of tutorials on the internet where talented article writers shows us how to create a model for the famous Iris classification problem. But I included a simple code at the github page of this project.
So after the training we will have a model structure json file and a file where we store our weights. This is important because later we will use this to load out model!

Create Flask website

Next we will create a simple website with Flask. In my sample app I provide only 2 routes:

  • /isModelLoaded: This is important because we have to know if our model is loaded so we can use it for prediction. Why is it really important??? Because if you use the free plan on Heroku, and if there is no traffic on your deployed app, it will go to a “sleep” phrase. If there is some traffic, we have to wait longer for the first time and we have to load our model too which takes some time.
  • /predict/<data>: We use this for the prediction with the data which is provided by the user. This is a simple string what we can parse.

I created a new Class called ModelLoader because it uses a different thread so we can load the model asynchronously, so our website won’t time out. And I also created a simple function and with that we can visualize the model’s prediction (plot it) and it also encrypts to a base64 string so we can show the image anywhere, we just have to create an image from the encrypted string.

isModelLoaded and predict returns simple json strings like this with Flasks’s jsonify function:

  "is_model_loaded": true

Deploy it to Heroku

This is the best part, because after that we can create cool applications!
Once we have our Model and our simple website we can deploy it. You have to think about a heroku application like a git repo. If we want to deploy it, we have to install requirements (like Keras, Flask, gunicorn, …) and we have to upload the code we wrote (we have to commit and push it.)

This is the whole process:

git init
git add --all
git commit -m "init"

heroku apps:create YOUR-APP-NAME
heroku buildpacks:add

git push heroku master

heroku ps:scale web=1

We use a custom buildpack because with that we can easily install Keras and other dependencies.

Test it

After it is done we can check out the results! You can find your website at: I deployed my code and you can see it here: OR let’s make a prediction:

Wooohoooo! We are done with our first Heroku-Deep Learning project. 🙂

Awesome application

I also created a really simple Android application which uses the prediction from that site. So people can predict easily and user friendly and the can see the plot whihc we made for our prediction.


As I said you can find the whole code at my github. Click here and you can see it.
The code includes the model training with Keras, the Flask app and the Android sample application.


Stay tuned for my next post. 😉