Amit Shekhar

Creating Custom Model For Android Using TensorFlow

As I had promised in my previous article on building TensorFlow for Android that I will be writing an article on How to train custom model for Android using TensorFlow. So, I have written this article. Still more to come.

If you have not checked my article on building TensorFlow for Android, check here.

Android TensorFlow Machine Learning Example
Machine Learning: Integrating Tensorflow in Androidblog.mindorks.com

In this article, we will train a model to recognize the handwritten digits. Here, we will use the famous MNIST Image Dataset which like the Hello World in Machine Learning for simplicity.

Credit: The classifier example has been taken from Google TensorFlow example. The custom drawing view used in this project is taken from here.

Here is the complete sample project.

MindorksOpenSource/AndroidTensorFlowMNISTExample
AndroidTensorFlowMNISTExample - Android TensorFlow MachineLearning MNIST Example (Building Model with TensorFlow for…github.com

The complete code to train and save the model which will directly run on Android is below. Read it carefully.

Explanation of code:

  • First, it downloads the dataset from the MNIST.
  • Then it starts training the model.
  • After training, it checks accuracy.
  • Then, it saves the model to the given path and checks the accuracy again.

Check the complete python code here.

MindorksOpenSource/AndroidTensorFlowMNISTExample
AndroidTensorFlowMNISTExample - Android TensorFlow MachineLearning MNIST Example (Building Model with TensorFlow for…github.com

It is very easy to understand it by reading the python code.

When we run this code, we get the required trained model that will be used in the Android application.

If you are getting any problem in building the project, connect with me, I will be happy to help.

Happy Coding :)

Thanks for reading this article. Be sure to click ❤ below to recommend this article if you found it helpful. It means a lot to me.

For more about programming, follow me and Mindorks, so you’ll get notified when we write new posts.

Check out all the Mindorks best articles here.

Also, Let’s become friends on Twitter, Linkedin, Github, and Facebook.