Running ML Models in Android using Tensorflow Lite

Sai Durga Kamesh Kota
Analytics Vidhya
Published in
4 min readJul 27, 2020

--

Running ML Models in Android using Tensorflow Lite

Introduction:-

Generally, after we train a model we need to test it. In the Development phase, it can be done using CLI (Command Line Interface). But when we need to proceed to the deployment phase, We need to make our model run on Web and Android. For Web, We can run our models using Tensorflow.js or using Flask / Django Frameworks. When it comes to android, It would be difficult to run the models in android as it generally requires more RAM and many constraints. For this Google comes up with a mini API known as TensorFlow-Lite. By using Tensorflow-Lite API we can be able to deploy our ML model into any android application. To show a brief of how it works I am showing you How to create a basic linear regression model and deploy it as an android application using Tensorflow Lite.

Understanding the Architecture of Tensorflow-Lite API:-

Before that, we need to understand the architecture of how TensorFlow-lite works.

The architecture of Tensorflow Lite API

It mainly involves 4 steps:-

  1. Training and saving Tensorflow Model:- Firstly we need to train a model using Keras framework and save the model in .H5 or.PB format to load it any time we require.
  2. Creating a TensorFlow Lite Converter:-The TensorFlow Lite converter is a tool available as a Python API that converts trained TensorFlow models into the TensorFlow Lite format. It can also introduce optimizations.
  3. Converting TensorFlow Lite Converter into .tflite format:- Now we need to convert this object to tflite format which will be further used in the android application.
  4. Deploying .tflite into Android Studio and run the Inference:- Now we will use Tensorflow Interpreter API in an android studio to run the .tflite model with data to produce outputs.

In this way, we can run the ML models using Tensorflow Lite API by following the above steps. Now Let’s make a simple linear regression app that takes x as input and return y.

Practical Implementation of Simple Linear Regression App in Android Studio using TensorFlow Lite:-

  1. Firstly, Let’s make a simple linear regression model with x and y as random numbers.

2. Saving the model to .pbtxt file.

3. Now we need to create a TFLite Converter object with a pre-trained model created in Step 1.

4. Now we need to use this converter to convert the object to the tflite object and need to save the object as .tflite file which will be further used.

5. Now open Android Studio and create an Empty Project with language selection as java.

6. Now you need to create the assets folder in the project level with a path of app/src/main folder.

7. Now we need to add a degree.tflite file to the assets folder. The project structure looks as below.

Project Structure

8. Now go to build.gradle(Module: app) and add the following script below build types which helps in not to compress tflite file when app apk is formed.

aaptOptions {
noCompress "tflite"
}

9. Now add the following dependency in dependencies in build.gradle(Module: app).

implementation 'org.tensorflow:tensorflow-lite:+'

10. Now sync your project.

11. Now we need to add the following code to activity_main.xml where a Button for predict button, EditText for input, and a TextView to display output is added.

12. Now go to MainActivity.java and we need to add a method to load the .tflite model file.

13. Now we need to initialize the variables and assign which links with elements in activity_main.xml file.

14. Now we need to import Interpreter from TensorFlow lite API by adding the below line to MainActivity.java

import org.tensorflow.lite.Interpreter;

15. Now add the following script in onCreate method to create the TFLite Interpreter object loaded from the tflite model.

try {
tflite = new Interpreter(loadModelFile());
}catch (Exception ex){
ex.printStackTrace();
}

16. To do Inference create a method that will take input string as input and convert that into a float array and use tflite.run() method to get the output and return output as a float value.

17. Now when we click on predict the method doInference is called and gets the output which is displayed as predicted output in the TextView.

18. The final MainActivity.java Looks as below:-

19. Now run and install it on the Emulator/Phone and the app looks as below.

A Snapshot of How the app looks

Conclusion:-

In this way, by using TensorFlow Lite API we can be able to run our Machine Learning Models in Android Mobiles.

References:-

--

--

Sai Durga Kamesh Kota
Analytics Vidhya

SDE 1 @ Angel One • Fintech • Backend • MLH Fellow 2021 • 2k18–2k22 @ NIT Patna