TFLite: Activity detection With TFLite Model Maker.

What is Tensorflow lite model maker?

Earlier it was not allowed to train the model directly with TFLite; developer was required first to train the model with TensorFlow, then save the model as intermediate form and then convert the saved model to TFLite using TensorFlow Lite converter.

In 2020, TensorFlow introduced the TensorFlow Lite Model Maker package that facilitates us to train a TFLite model with the custom dataset. TensorFlow Lite Model Maker uses the transfer learning method to train the model, so it also works with less training data.

The Model Maker library supports Image classification, Object detection, Text classification, BERT Question-Answer, Audio classification and recommendation.

This tutorial demonstrates use the of Image classification.

– First, you need a proper dataset with a fall person and a not-fall person.

pose_dataset
|__ fall
    |______ fall-person1.jpg
    |______ fall-person2.jpg
    |______ ...
|__ not-fall
    |______ not-fall-person1.jpg
    |______ not-fall-person2.jpg
    |______ ...

– Then, you need to upload this dataset into google drive to train your model in the google colab VM.

Finally, start training your model into google colab by running the following command.

– We first need to install several packages, including model maker from the official git repository.

!pip install -q tflite-model-maker

– Import the required packages.

import os
import numpy as np
import tensorflow as tf
assert tf.__version__.startswith('2')
from tflite_model_maker import model_spec
from tflite_model_maker import image_classifier
from tflite_model_maker.config import ExportFormat
from tflite_model_maker.config import QuantizationConfig
from tflite_model_maker.image_classifier import DataLoader
import matplotlib.pyplot as plt
import tflite_model_maker

– As we have uploaded the dataset and our final model will be stored in drive, we need to connect drive into colab VM.

from google.colab import drive
drive.mount('/content/gdrive')

– Get the dataset path to google drive.

image_path = '/content/gdrive/MyDrive/pose_dataset/'

–  Load input data and split it into training data and testing data.

data = DataLoader.from_folder(image_path)
train_data, rest_data = data.split(0.8)
validation_data, test_data = rest_data.split(0.5)

– Customize the TensorFlow model and start the training.

model = image_classifier.create(train_data, validation_data=validation_data, epochs=12)

– Evaluate the model.

loss, accuracy = model.evaluate(test_data)

– Then, finally, export the model into the tflite model.

model.export(export_dir='/content/gdrive/MyDrive/pose_dataset')

– Final, result.

tflite-model-maker-result

Leave a Reply

Your email address will not be published. Required fields are marked *

Other Tutorials Tutorials