Android and machine learning

Glitch
3 min readJun 4, 2021

Ok guys so today as I got less time I have just followed a quick tutorial about how to integrate tensorflow in an android app and have tried to break down the same for you .So quickly just in 3 easy steps we are gonna wrap this.

1. Install Tensorflow on your machine

Tensorflow as it is, works with Python. So you’ll need to have Python to work with. The installation revolve around getting Python setup on your machine. The following provides you the full detail.

Installing TensorFlow | TensorFlow

The following guides explain how to install TensorFlow libraries for use in other programming languages. These APIs are…

As for me, I’m using a Windows with Python 2.7 readily available. So I just did the following

$ sudo easy_install pip
$ pip install — upgrade virtualenv
$ virtualenv — system-site-packages tensorflow
$ cd tensorflow
$ source ./bin/activate
$ pip install --upgrade tensorflow

2. Training the Machine

Firstly, you’ll need to have a set of pictures for TensorFlow to train with. They should be place in folder of specific label of them.

I have only about 30 pictures each label, this is consider small set, good training data should go up to thousands and more. The quality of pictures and the variation will have great impact on the train result accuracy, so choose prudently

Training:

After you have the pictures setup, you could now feed it to TensorFlow using the below command running it’s given python script retrain.py. Most of the parameters are just indication of where the output is stored. The most important are graph.pb and labels.txt file output, as they will be your trained model assets used in the Android App.

python tensorflow/examples/image_retraining/retrain.py \
--image_dir=/YourTrainImageFolder \
—-output_graph=trainOut/hero_graph.pb \
--output_labels=trainOut/hero_labels.txt \
--summaries_dir=trainOut/summaries/ \
--bottleneck_dir=trainOut/bottlenecks \
--model_dir=trainOut/models/ \
--saved_model_dir=trainOut/saveModelDir

This will run for a while, as by default it will run 4000 rounds of training. You could use —-how_many_training_steps=500 to reduce the step to 500, or more as you could explore the time vs accuracy it could produce.

Validation:

After completed, it’s a good idea to test how good the trained model is. There’s a provided script to do that i.e. label_image.py. The input are the standard input

python tensorflow/examples/label_image/label_image.py  \
--graph=trainOut/hero_graph.pb \
--labels=trainOut/hero_labels.txt \
--input_layer=Mul \
--output_layer=final_result \
--input_mean=128 \
--input_std=128 \
--image=/YourTestImageFolder/YourImage

If you run correctly, it could result something similar to below, stating that Ironman is about 82% confidence accuracy, if the provided image has Ironman in it.

ironman 0.8167289
batman 0.09269745
superman 0.082504764
spiderman 0.008068823

To get further detail of how things work, you could refer to the extra references below

How to Retrain Inception’s Final Layer for New Categories | TensorFlow

The script can take thirty minutes or more to complete, depending on the speed of your machine. The first phase…

3. Deploy into Android App

Finally, you have the model (stripped_graph.pb and labels.txt)! They that can be deployed into your Android App.You could start off looking at this official reference

From the sample project, you could extract some useful classes. I took TensorFlowImageClassifier and it’s Classifier for my App below.

This makes it really simple to do the rest. I just

a. Import that library (in app’s build.gradle)

implementation 'org.tensorflow:tensorflow-android:1.5.0'

b. Put in your model files in the assets folder.

c. Initialize the Classifier (the the right graph model and labels file etc) at the start of the activity (onCreate())

classifier = TensorFlowImageClassifier.create(
assets, MODEL_FILE, LABEL_FILE, INPUT_WIDTH, INPUT_HEIGHT,
IMAGE_MEAN, IMAGE_STD, INPUT_NAME, OUTPUT_NAME)

d. Provide the camera captured bitmap, and run recognize (when the Recognize Button is clicked)

classifier?.recognizeImage(bitmap)

e. Close the classifier upon exiting the App (e.g. onDestroy())

classifier?.close()

Ta-da !! that complete all our steps to complete our app and have integrated TensorFlow with our app . Keep Coding.

--

--