Deep Learning using TensorFlow Lite — for Android, iOS and Raspberry pi

Ravi Valecha
4 min readNov 13, 2020

TensorFlow Lite is an open source deep learning framework for on-device inference.

Neural network are very computationally expensive, because of that most of architectures can’t be executed on a mobile device. To overcome this problem, we have the TensorFlow Lite — library used to optimize models in multiple ways so we can execute them on a mobile device or any other embedded device. There are few steps required to convert and optimize the TensorFlow neural network for mobile devices ( Android or iOS) using TesnsorFlow Lite

Workflow to convert a TensorFlow model for mobile devices —

Train a model on computer using TensorFlow

Optimizing model using TensorFlow Lite

Inference time on mobile devices, means process of executing a TensorFlow Lite model on-device in order to make predictions based on input data.

Source code to convert TensorFlow model to Lite

Installing dependencies and setting up the environment

pip install tensorflow-gpu==1.13.1

Importing project dependencies

Dataset pre-processing — Loading the Fashion MNIST dataset

Load data from fashion MNIST dataset and divide it in for training and testing.

Image normalization

Before building a model using dataset, image normalization is required. Reshape train data in to 28 X 28 matrix.

Building and compile a model

Before convert a model for mobile devices, its required to build and save the model. Create an instance of sequential model. a sequential model is appropriate for plain stack of layers, which have exactly one input tensor and one output tensor. Sequential model is not good if model or layers have multiple input and output or in case of layer sharing.

Added three layers, we can add layers incrementally in sequential model. First layer is Dense or fully connected layer have 128 units and activation function is Relu, and input_shape which is equivalent to dataset X_train.

Second layer is dropout layer .Third layer is a Dense layer with softmax as an activation function.

Train the model

1 epoch = one forward pass and one backward pass of all the training examples. Set epochs to 5

Test and save model

Evaluate the model using test data. Check the loss and accuracy, if its good save the model otherwise retrain the model again with different set of epochs

Convert and save model using TensorFlow lite

Pass the model to TensorFlow lite and save it to a file with extension .tflite.

Last deploy the lite model on to mobile device. Run your model on-device with the TensorFlow lite interpreter. To perform an inference with a TensorFlow Lite model, you must run it through an interpreter.

TensorFlow Lite inference typically follows the following steps:

Loading a model

  1. Load .tflite model into memory, which contains the model's execution graph.

Transforming data

  1. Transform input data format that expected by the model.

Running inference

This step involves using the TensorFlow Lite API to execute the model. It involves a few steps such as building the interpreter, and allocating tensors.

Interpreting output

This step receive results from the model inference.

TensorFlow inference APIs are provided for Android, iOS and Linux platform.

On Android, TensorFlow Lite inference can be performed using either Java or C++ APIs. The Java APIs provide convenience and can be used directly within your Android Activity classes. The C++ APIs offer more flexibility and speed, but may require writing JNI wrappers to move data between Java and C++ layers.

On iOS, TensorFlow Lite is available with native iOS libraries written in Swift and Objective C.

On Linux platforms (Rasberry Pi you can run inferences using TensorFlow Lite APIs available in C++ and Python.

--

--