Deep Learning Mobile Application Development – Part 1: Conversion of Keras Trained Model to TensorFlow

Hi, in this article I will talk about how to use a model you’ve trained with Keras in the Android application. Here’s what we will do:

  • We will train a model with Keras and dataset,
  • We will record the model we have trained with the extension .pb,
  • We will convert the .pb file to a file named Bazel .tflite,
  • We will use the .tflite file with the Android application.

Let’s start then. We will use Keras’ framework on Python. I’m going to use Pycharm. For friends who want to download Pycharm:

https://www.jetbrains.com/pycharm/download/#section=linux

 

Note: Pycharm Professional is a paid IDE. If you want to use it for free, you can download the Pycharm Community. If you want to use it for free, you can download Pycharm Community.

Pre-requisites:

  • Python 3
  • Tensorflow 1.7
  • Numpy 1.14.2
  • Sklearn
  • Keras 2.1.5

Note: If you are a Windows user, you can use these command lines by downloading Bash Shell.

As you type them into the terminal one by one, you’ll have to install each library or framework.

Let’s download our data set before the application. To download the data set:

Note: Cut a few lines in your data set and paste it somewhere, because the data we will use in training and the data that we will use for trial purposes are different.

The application checks whether you have diabetes, according to some input values. If the result is 0, it is clean, and 1 is the problem. We will save this file as “pima-indians-diabetes.data” in the project that will open in our directory.

Now let’s go to practice.

We open Pycharm and create a Python file. I named this file “main”. And we’re installing the following libraries.

As we load the libraries, we continue to implement it.

In this way, we created and trained our artificial neural network, let’s make a guess.For this, we write the following lines below.

Now let’s get it to work and see if we can figure it out.

The result should be “0”. You may not understand the above codes. It’s quite normal because the main thing we’re writing is how to use the model we’re training on Android phones.

Now let’s create the .pb file extension. Beware you’ll have to fine-tune here!

We need to import these libraries before continuing the application.

Now let’s write a function like this;

This function will allow us to register our model with a .pb extension. Now we have to use this function. To do this, we add these lines of code at the bottom of the file we’ve trained, but don’t run the file, we have more work to do.

As you can see above, there are two parameters:

  • name input name “
  • “output name “

The input name of our input name model and the output name is the output name of our model. These variables can be obtained as follows;

Note: Print and save the values of the as “inputName” and in “outPutName” function variables, because we will use it for Bazel in the future.

In this way, we have assigned the input and output name of your model to variables named “inputName” and “outPutName”. As we have done this, we can now call our function this way.

(WARNING: remember to define the inputName and outPutName variables before calling the function!)

After you do this, let’s run the python file that we wrote. After running, a folder called “out” will be created. We will use the  “modelimtest.pb” file under this folder.

 

If everything went so well, good job. If it didn’t, try the above steps again.

In the next article we will use .pb file with Bazel to convert to .tflite and use it in android application.

Leave a Reply

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