DEV Community

Cover image for Tensorflow Models on Android - Absolute Beginner
Nilesh Arnaiya
Nilesh Arnaiya

Posted on

Tensorflow Models on Android - Absolute Beginner

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

  1. Let's use the dataset and upload it to the Teachable Machine
    Training
    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

  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
  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 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

Top comments (4)

Collapse
 
veselinastaneva profile image
Vesi Staneva

Great tutorial! 👏 Check out the Awesome Teachable Machine List for useful resources and ideas for your next projects.

Collapse
 
mariakhala profile image
mariakhala

Can you share trained model file and label

Collapse
 
nilesharnaiya profile image
Nilesh Arnaiya

The trained model will be available from teachablemachine itself. Once you export the model, it will be downloaded.

Collapse
 
googleaicodes profile image
GOOGLE AI

This is very useful. Thanks