Step By Step Guide To Run Your Trained Neural Network Model On Android (Part I)

Anuradha Niroshan
3 min readAug 29, 2017

In this post I am trying to explain how to run a trained neural network model on android. For this guide I’m going to use Keras deep learning library to implement the neural network. The proof of concept code is available on my GitHub repository. I’ve wrote this post in two parts. Part I will provide all the background information of deep learning libraries that will be useful to understand this guide. In part II , I will guide you through the real coding stuff. you can read more articles like this on my blog.

Why you should read this Article ?

Training the model in Android phone itself might not be a good idea. raining phase requires lot of resources and may take several hours depending on the training data set you have. One option is that you could train the model on a server or in your laptop and import it to your android project. If the model is evolving then you can retrain it and just update the app with new model.

This might be a really useful article if you are an android app developer. There is a plethora of articles written on this subject which I have listed in references which are little bit complicated to begin with. After reading this post I encourage you to read those articles as well. So Let’s get started.

Prerequisites

This post doesn’t intend to give you a deep explanation about internals of neural networks. So you must at least have some idea about machine learning , what neural networks are and how they work. you should also have some knowledge about python basics. If so you are good to go.

What you’ll learn from this guide….

  1. Introduction to TensorFlow and Keras deep learning libraries.
  2. Creating a Neural Network using Keras Deep learning library.
  3. How to save your trained Model as protobuf(.pb) file.
  4. Importing trained model to your android project and make predictions.

Introduction to TensorFlow and Keras deep learning libraries

What is TensorFlow?

The main reason why I have include TensorFlow in this guide is because you need to have a little understanding about what a node is. It will help you to understand the code of converting your Keras model to protobuf file. If you are confused right now, don’t be, It will all make sense while you go through the post. ;)

Ok back to TensorFlow. Its an open source library developed by google engineers for machine learning tasks. The important fact about TensorFlow is that it provides multiple API’s. Unlike very high level libraries like Skit-learn, TensorFlow allows fine control over the models.

A tensor is nothing but a set of primitive values shaped into an array of any number of dimensions. tensor’s rank is its number of dimensions.

Node takes zero or more tensors as inputs and produces a tensor as an output. For example you can have store constant values in two nodes and get the addition of the values of these two nodes to another node. These nodes have unique names. If you want more details there’s a nice tutorial here.

What is Keras ?

Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano. All these libraries are developed for deep learning which facilitate the back-propagation calculation using GPU. Keras is just an interface for using the underlying deep learning libraries. Using Keras is pretty straight forward and simple compared to above libraries. In the next part we’ll see how to implement a Neural Network using Keras.

What I have just said explained by following picture.

Now that we have covered all the background stuff, lets jump into coding in Part II.

--

--

Anuradha Niroshan

Computer Science and Engineering, University of Moratuwa. Lives in Srilanka. “Blogging is a conversation, not a code.”