Image classification android app using Teachable Machine

Image classification android app using Teachable Machine


Objective: Build a custom object Image classification android app using Teachable Machine.

Train a Machine Learning model for custom object image classification using Teachable Machine, convert it to a TFLite model, and finally deploy it on mobile devices using the sample TFLite image classification app from TensorFlow’s GitHub.

Roadmap

  • Collect the dataset of images.
  • Open Teachable-Machine’s image project
  • Train the model.
  • Evaluate the model.
  • Export the model
  • Create TFLite model for Android
  • Download sample image classification app from TensorFlow and adjust for your custom TFLite model.

Image classification or image recognition is a concept in which you showcase an image to the camera sensor of the device and it will tell you what is present in that image or tell us which class does it belongs to.

In this tutorial, I will walk you through the custom image classification by training a simple deep learning model with the help of an exciting online tool by google: teachablemachine with google, and then exporting the model to TensorFlow lite version which is compatible with android device. Then, finally, we will deploy this model onto an android device.

Model

The training platform used for training our custom image classifier is the teachablemachine with google. This is an exciting platform for learning the deep learning training process with just a very few clicks. First, by uploading the different classes of objects from your system or using a webcam, then training it. Finally, after training, you can export the model of your choice. You can choose whatever format you want and download the model. I am training a model for mask image classification. This will be done in 5 steps mentioned in the section below. The first 3 steps are the same as in the Train image classification model using Teachable Machine tutorial.

  1. Upload your dataset
  2. Train the model
  3. Preview the model
  4. Export the model in TFLite format
  5. Download the TFLite model and adjust the TFLite Image Classification sample app with your custom model

Requirements

  • Your custom object image dataset with different classes.
  • Android Studio 3.2 (installed on a Linux, Mac, or Windows machine)
  • Android device in developer mode with USB debugging enabled
  • A USB cable (to connect Android device to your computer)

LET’S BEGIN !!

Objective: Build an Image classification android app using Teachable Machine.

To start, go to the Teachable-Machine site.

You will find 3 videos there on how to upload your dataset, train your model and convert it to TFLite model. The process is fairly simple. Click on Get Started on the homepage and choose Image Project. You can also sign in using your google drive to save your model files otherwise you will lose all your images and model files if you close the tab or refresh the page.


Step 1. Upload your dataset

Create and upload the dataset to the teachablemachine and define the names of the classes accordingly. I am training a model for mask image classification with 2 classes viz., “with_mask” & “without_mask”.

Teachable Machine UI

Step 2) Train the image classification model

During training, you can tweak the hyperparameters like:

  • No of epochs
  • Batch size
  • Learning rate

Click on Train Model after setting the values of the hyperparameters. This process will take a while depending upon the number of images and epochs.

Important: During training, do not switch tabs as it will stop the training process.


Step 3) Preview your trained model

Once the training is over, you can preview your trained model in the preview window. You can either upload images to test or you can test using a webcam. See preview test results for both below.

USING WEBCAM

USING IMAGES


Step 4) Export the model in TFLite format

Finally, export the model in the form of TensorFlow lite format to deploy on mobile devices .

Click on Export Model and select the TensorFlow Lite tab. Next, choose the model format you want. You can download quantized as well as floating point file format of TFLite.

NOTE: Teachable Machine only supports MobileNet model architecture as of now.

Image for post

The .tflite file should be placed in the asset folder of the android project directory and change the name in the java file which is reading it accordingly(See Step 5)


Step 5) Download the TFLite model and adjust the TFLite Image Classification sample app with your custom model

  • Download the TensorFlow Lite examples archive from here and unzip it. You will find an image classification app inside

C:UserszizouDownloadsexamples-masterexamples-masterliteexamplesimage_classification

  • Next, copy the model.tflite model with metadata and the labels.txt file inside the assets folder in the object detection Android app.

…examples-masterexamples-masterliteexamplesimage_classificationandroidmodelssrcmainassets

IMPORTANT: Switch between inference solutions (Task library vs Support Library)

This Image Classification Android reference app demonstrates two implementation solutions:

(1) lib_task_api that leverages the out-of-box API from the TensorFlow Lite Task Library;

(2) lib_support that creates the custom inference pipleline using the TensorFlow Lite Support Library.

The build.gradle inside app folder shows how to change flavorDimensions "tfliteInference" to switch between the two solutions.

Inside Android Studio, you can change the build variant to whichever one you want to build and run—just go to Build > Select Build Variant and select one from the drop-down menu. See configure product flavors in Android Studio for more details.

For gradle CLI, running ./gradlew build can create APKs under app/build/outputs/apk for both solutions.

Note: If you simply want the out-of-box API to run the app, we recommend lib_task_api for inference. If you want to customize your own models and control the detail of inputs and outputs, it might be easier to adapt your model inputs and outputs by using lib_support.

The file download.gradle directs gradle to download the two models used in the example, placing them into assets.

**For beginners, you can choose “lib_support”. The default implementation is “lib_task_api”. The “lib_support” implementation does not require any metadata while the lib_task_api implementation requires metadata.

To choose a build variant, select the app module in the project manager inside Android Studio, go to Build in the menu bar, and click on Select Build Variant. You will see a window pop up where you can choose your variant as shown below.

  • Next, make changes in the code as mentioned below.
  • Edit the gradle build module file. Open the “build.gradle” file $TF_EXAMPLES/lite/examples/image_classification/android/app/build.gradle to comment out the apply from: ‘download_model.gradle‘ which basically downloads the default image classification app’s TFLite model and overwrites your assets.
// apply from:'download_model.gradle'

Select Build -> Make Project and check that the project builds successfully. You will need Android SDK configured in the settings. You’ll need at least SDK version 23. The build.gradle file will prompt you to download any missing libraries.

  • Next, if your model is named model.tflite , and your labels file labels.txt, the example will use them automatically as long as they’ve been properly copied into the base assets directory. To confirm, open up the $TF_EXAMPLES/lite/examples/image_classification/android/lib_support/src/main/java/org/tensorflow/lite/examples/classification/tflite/ClassifierFloatMobileNet.java file in a text editor or inside Android Studio itself and find the definition of getModelPath(). Verify that it points to your model file: “model.tflite“. Next, verify that the path to labels getLabelPath() points to your label file “labels.txt“.
  • If you also have the quantized TFLite model for MobileNet and both the float and quantized models for EfficientNet we can do the same step as above and make the getModelPath() and getLabelPath() in all the classifiers point to their respective models. See pic below. Note that you need to have other TFLite models for your custom dataset to run the app otherwise it will give an error. However, in this tutorial, I am only demonstrating the model for Float MobileNet, so I will remove the usages and all references of the other 3 classifiers in Android Studio.
  • I have also made another change, since we are using only 2 classes, we need to change MAX_RESULTS in Classifier.java and change the result size from 3 to 2 in the showResultsInBottomSheet() function in CameraActivity.java and comment out the third detected item(recognition2). See pics below. (NOTE: If you have 3 or more classes you do not have to make this change. I have shared my custom Android app on GitHub. You can find the link below in the credits section)
  • Finally, connect the Android device to the computer and be sure to approve any ADB permission prompts that appear on your phone. Select Run -> Run app. Select the deployment target in the connected devices to the device on which the app will be installed. This will install the app on the device. Test your app before making any other new changes or adding more features to it. Now that you’ve made a basic Image classification app using Teachable Machine, you can try and make changes to your TFLite model. I have also given the links to the TensorFlow site’s pages where you can learn how to apply other features and optimizations like post-training quantization etc to your TFLite model. Read the links given below in the Documentation under the Credits section. Have fun!

Read this TensorFlow image classification app GitHub Readme page to learn more-> TensorFlow Lite image classification Android example application


My GitHub

Image Classification sample app

Custom Image Classification

Mask dataset

Prajnasb Github

Check out my Youtube Video on this

Coming Soon!


CREDITS

Documentation / References

Dataset Sources

You can download datasets for many objects from the sites mentioned below. These sites also contain images of many classes of objects along with their annotations/labels in multiple formats such as the YOLO_DARKNET txt files and the PASCAL_VOC xml files.

Other Mask Datasets


2 thoughts on “Image classification android app using Teachable Machine

Leave a Reply