Android Application for Dog Breed Classification using TensorFlow Lite

Mourya Polaka
The Startup
Published in
2 min readMay 11, 2020

Image Classification, TensorFlow Lite, MobileNetV2, Android Application

1. Data Set

The Stanford Dogs data set consists of 20,580 images of 120 dog breeds from around the world.

Figure 1: Sample images from the data set

2. Data Preparation

Resize the images by using ImageDataGenerator and create a training set that stores 80% of the images and validation set that stores 20% of images.

IMAGE_SIZE = 224
BATCH_SIZE = 64
datagen = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1./255,
validation_split=0.2)
train_generator = datagen.flow_from_directory(
image_dir,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE,
subset='training')
val_generator = datagen.flow_from_directory(
image_dir,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE,
subset='validation')

3. Model Pipeline

Initialize Base Model

MobileNet V2 model was pre-trained on ImageNet data set, which was trained on over 1.4 million images of 1000 classes. We use this model to extract the features from the custom data set. Change the include_top parameter to False to exclude the classification layers

base_model= tf.keras.applications.MobileNetV2(
input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')

Feature Extraction

Freeze the convolution base of the base model to use it as a feature extractor and add a classifier on top of it to train the model on custom data set.

base_model.trainable = Falsemodel = tf.keras.Sequential([base_model,
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(120, activation='softmax')])

Compile Model

model.compile(optimizer=tf.keras.optimizers.Adam(),
loss='categorical_crossentropy',
metrics=['accuracy'])
epochs = 10
history = model.fit(train_generator,
steps_per_epoch=len(train_generator),
epochs=epochs,
validation_data=val_generator, validation_steps=len(val_generator))

Convert to TFLite Format

saved_model_dir = 'Directory to store the converted model'
tf.saved_model.save(model, saved_model_dir)
converter= tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)

4. Dog Breed Android Application

Setup the environment by cloning the Tensorflow’s GitHub repository. This repository includes examples of various deep learning applications deployed on multiple platforms.

Step 1: Clone the Tensorflow’s repository

git clone https://github.com/tensorflow/examples.git

Step 2: Add the TF Lite file and labels to the assets folder

#Path to assets directory/lite/examples/image_classification/android/app/src/main/assets/

Step 3: Install Android Studio and open the project folder as an existing Android Studio project

#Path to project directory/examples/lite/examples/image_classification/android

Step 4: Enable developer options and USB debugging on your Android device

  • Android 9 (API level 28) and higher: Settings > System > Advanced > Developer Options > USB debugging
  • Android 8.0.0 (API level 26) and Android 8.1.0 (API level 26): Settings > System > Developer Options > USB debugging

Step 5: Sync project with Gradle files

Figure 2: Button to sync Gradle Files

Step 6: Select the device and run the application

Figure 3: List of available devices in the AVD manager

5. Test it on your device

Video 1: Dog breed classifier application on Android device

--

--