Tensorflow Models on Android - Absolute Beginner
Nilesh Arnaiya
Posted on May 21, 2020
What is Tensorflow Lite?
- TensorFlow Lite is an open-source framework created by Tensorflow and optimized to run inference on mobile devices📱 as well as edge devices. TFLite is used for deploying pre-trained models on android, iOS and even on your latest Raspberry Pi
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 😷 Classifier right in the browser using Google's Teachable Machine
All thanks to Prajna Bhandary for the dataset - Download
Let's Train
Let's use the dataset and upload it to the Teachable Machine
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.-
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.
-
Export the model
- Choose a quantized model for reducing the model size and accelerating operations on mobile devices while suffering a very minimal loss.
-
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 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👏 in thee glory of creating thee Face Mask Detection Model and running it on mobile devices📱.
Additional Resources
- If you are using a Custom trained model, use this for conversion Udacity Tflite conversion
- If you are using Keras - use this Keras .pb to .tflite
- Also, Check out the Udacity Course - Intro to Tensorflow Lite
- A Project built on top of TFLite - Smart Mirror
Posted on May 21, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.