Deep Learning Mobile Application Development – Part 2: Using a Model Converted to Tensorflow in Android Application

Hello, in the previous article we learned how to convert a model with Keras and convert it to a .pb file. In this article we translate the .pb file into a .tflite file and use it in our Android application.
Here’s what we’re going to do:

• We will download Tensorflow’s Github Depot,
• We will download Bazel,
• We will convert the model we are studying with.
• We will run the .tflite file in our Android Application.

Let’s go with the order.
Let’s download Tensorflow’s Github Repository, we’ll go to this link and clone the repository.
Because I use linux, I will go to the terminal by typing “go clone” and download the repository.

If you cannot do this, click “Clone or Download” and click “Download bump”.
Now let’s download Bazel and go to this link and download the Bazel for your operating system.

Note: Friends using Arch Linux like me can write “yaourt -S bazel” to the terminal and install Bazel.
After you have downloaded the Bazel, open the Tensorflow directory and open the terminal/cmd and type the following command:


bazel build tensorflow/contrib/lite/toco:toco

After running this command, your computer may experience a small-scale shock, but it doesn’t hurt to your computer, so I hope it does not. Our process will take a little longer.
Why don’t I tell you why we use Basel when we’re running? What is it, Basel?

Bazel optimizes what you do when compiling a structure. In this way, you can perform your transactions faster. For more detailed information, please visit Bazel’s official website.
Let’s go back to the application if the process is finished.
Let’s put The .pb file that we created in our previous article, in the “tensorflow” folder.



If you have done everything so smoothly, let’s open the terminal/cmd in this (tensorflow) directory. And paste the following command line.

And paste the following command line.
Let’s recognize the parameters one by one:
input_file = The parameter that we give the name of our graph file,
input_format = The type of file that we export as input
output_format = The type of file that Bazel will give us,
output_file = The name of the file that Bazel will give us,
inference_type = The type of output that the model will give us after giving the model an entry value,
input_type = Type of input value that we will give to model,
input_arrays = Input name of the model (We have printed the input and output names of the model in our previous article)
output_arrays = The output name of the model,
input_shapes = The length of the input values to be given to the model (For example, we will give an 8-member float array, so we have defined input_shapes as 1.8 above.)
When you type this command line, a file called “modelim.lite” will be created in the tensorflow folder.


We can now develop our Android App if everything is done. Enter Android Studio and open a project. I will name the project “DeepLearningIOAndroidTensorflow”. As we open our project, let’s open the gradle (app) file and add the following in order.

Adding these will be the final version of your Gradle file.

Now we can improve the application. We will not create an UI (User Interface) for our project. We will see the result on the console.
To run our Tflite file in our Android application, we need to first put our file into our Android Application. To do this, we need to create the file named “Assets. A video on how to create the Assets file:

Now that we have created the Assets file, we can now put our “.tflite” file into our. Assets folder. Just drag and drop.
After you discard the file, your index will look like this;

If you have done this we can come to the file. We need to make a few changes here. We can start by adding the “Interpreter” class, which we will use to make the changes related to the .tflite, as a global variable.

Our Interpreter class is the class that will allow us to use Tensorflow Framework. When you look inside the class you will see that there are 4 founding methods. So you can export the input values in different ways. We will use the field constructor method as the parameter of the “MappedByteBuffer” class. That’s why we need a “modelim.lite” file in the “assets” folder and a method called “MappedByteBuffer”. Our method is as follows;


This method takes 2 parameters;

• Activity = The name of the Activity to which we provide input and output values. As we will do our operation in MainActivity.class Activitis, we will give this parameter “MainActivity.this” Activity.
• MODEL_FILE = We will give the name of my model.lite as a parameter.

Then let’s call the loadModelFile in method in the onCreate method of Activity.

The modelFile variable is named after our .tflite file. If everything is perfect here, let’s continue.
We will take the input values and get the output value. We give the input values as a float array and take the output value as the float array. Now let’s define 8 float variables and assign them to an array.

Let’s create one out of the array and print the tensorflowa model into this array.

That’s what we do. Now we can compile the application and look at the “Result” among the logs. We expect our result to be 0.

Leave a Reply

Your email address will not be published. Required fields are marked *