Build a custom image classifier for your android application.

Rafi
Analytics Vidhya
Published in
3 min readFeb 15, 2020

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 to android device. Then, finally, we will deploy this model to an android device.

Let’s take a simple use-case of Fruits Classification Application for android.

The WorkFlow for Image classification in android

Model

The training platform used for training custom image classifier is the teachablemachine with google. This is an exciting platform for learning the deep learning training process just at a click by just uploading the different class of datasets or using a webcam, then train it quite easily. Finally, after training, you can export the model of your choice. I have exported it to the TensorFlow lite version as I have to run this on an android device. you can choose whatever format you want and download the model.

Training process teachablemachine-with-google

Requirements

  • Image dataset of different classes(for custom training) (you can download the fruit dataset used in the process download link:) Fruit
  • 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)

Build and run

Step 1. Upload the dataset(custom dataset)

Prepare and upload the dataset to the teachablemachine with google site and define the number of classes accordingly. Train the image classification model over there and finally, export the model in the form of TensorFlow lite format.

During training, you can change the no of hyperparameters like:

  • No of epochs
  • Batch size
  • Learning rate

After the training is done, you have to export the model in the TensorFlow lite format for the deployment in android devices. The .tflite files should be placed in the asset folder of the android project directory and change the name in the java file which is reading it. Also, you can download quantized as well as FLOAT file format of tflite.

Exporting the model

Step 2. Clone this repository for image classification using deep learning

Clone this GitHub repository below to your computer and save it to the folder of your choice. This the java code for android application.

Place the .tflite file to the asset folder and replace the name and you can build the application now.

Step 3. Build the Android Studio project

Select Build -> Make Project and check that the project builds successfully. You will need the 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. you have to put the fruits.tflite to the asset folder of the android structure project directory and change the labels according to the number of the class you have trained.

Step 4. Install and run the app

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.

Download the code:

References:

--

--

Rafi
Analytics Vidhya

Deep Learning, Computer-Vision, Object Detection, CNN architecture, Jetson Nano, Android-AI