Tensorflow Models on Android - Absolute Beginner
Tags: android, machinelearning, tensorflow, python
Published: 2020-05-21 17:20:56

# What is **Tensorflow** Lite? 
* TensorFlow Lite is an open-source framework created by Tensorflow and optimized to run inference on mobile devices:iphone: as well as edge devices. 
**TFLite** is used for deploying pre-trained models on android, iOS and even on your latest [Raspberry Pi](https://www.amazon.in/gp/product/B07XT1QJ4S/ref=as_li_tl?ie=UTF8&tag=masks4all-21&camp=3638&creative=24630&linkCode=as2&creativeASIN=B07XT1QJ4S&linkId=7137f29fa680dcec8bfaca80fa1ed81a)

This article is an introduction to TensorFlow Lite and you will learn how to train the model, convert the model, and run inference on your mobile devices.

## **Requirements**
* Android Studio
  
Let's Create a **Face Mask** :mask: Classifier right in the browser using Google's [Teachable Machine](https://teachablemachine.withgoogle.com/train/image)


All thanks to Prajna Bhandary for the dataset - [Download](https://github.com/prajnasb/observations)

# **Let's Train**
1. Let's use the dataset and upload it to the Teachable Machine
     ![Training](https://i.imgur.com/dn1TXnp.png)
  You can add as many classes with as many images you want.
  More the Images, More the training time. 
  Click the **Train** Model and wait until it completes the training. 



2. ### **The Advanced Tab**
     * This gives you more freedom to select the basic **hyperparameters** that help the Neural network learn. 
     * **Epochs** - 1 epoch is one run of each of the samples in our data and you want to keep increasing until it you get the desired predictions.
     * **Batch Size** - Divides your samples by the Batch Size you choose and that becomes your batch. For Eg: 112 samples/ 16 = 7 Batches
     * 7 batches to be processed by the model to finish 1 Epoch
     * **Learning Rate** - Size of a step that a model makes for choosing the best possible weights in a neural network. The smaller steps take a lot of time to learn which weights would be best and a high learning rate would mean we might miss some best weights possible. So tweak carefully. 

     ![Results](https://i.imgur.com/zKRUUaE.png)
3. ### **Export the model**
    * Choose a quantized model for reducing the model size and accelerating operations on mobile devices while suffering a very minimal loss. 
     ![Export](https://i.imgur.com/RMy9Fbd.png)

4. ### **Deploying on Android Devices**
    * Let's create a **labels.txt** file with all our classes and each class on a separate line
    * We will use this [MobileNet Repository](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/android) for our Mask Detector and update the files respectively.
    * Download or clone the repo and open the project in Android Studio
    * Add the labels.txt file in ```src/main/assets/``` Folder.                                                        

   * Replace the model in the **assets/** folder with our trained model.
    * Change the MODEL_PATH in MainActivity.java
    ``` private static final String MODEL_PATH = 
 "mobilenet_quant_v1_224.tflite";```
    * Connect the USB and RUN the app

Enjoy:clap: in thee glory of creating thee Face Mask Detection Model and running it on mobile devices:iphone:. 

##Additional Resources 


* If you are using a Custom trained model, use this for conversion
[Udacity Tflite conversion](https://colab.research.google.com/github/tensorflow/examples/blob/master/courses/udacity_intro_to_tensorflow_lite/tflite_c04_exercise_convert_model_to_tflite_solution.ipynb) 
* If you are using Keras - use this 
[Keras .pb to .tflite](https://colab.research.google.com/drive/1rYqJmgI_luomxO2D59Q4vtHn580ywxm1?usp=sharing)
* Also, Check out the Udacity Course - [Intro to Tensorflow Lite](https://www.udacity.com/course/intro-to-tensorflow-lite--ud190)
* A Project built on top of TFLite - [Smart Mirror](https://buildawn.com/blog/2020/05/18/smart-mirror-diy-project-under-100/)