Converting Tensorflow Model to Tensorflow Lite

Sanpreet Singh
3 min readJul 28, 2019
Converting Tensorflow Model to Tensorflow Lite
Converting Tensorflow Model to Tensorflow Lite

What is Tensorflow Lite

Tensorflow Lite is Tensorflow light weight solution for mobile and embedded devices. It helps to understand one of the most important technology that is edge computing which enables to run the model on the devices instead of running from the server. It lets you run machine-learned models on mobile devices with low latency, so you can take advantage of them to do classification, regression or anything else you might want without necessarily suffering a round trip to a server. It helps in saving money and providing more speed. This post is focused on converting the tensorflow model into tensorflow lite. Code used for this post has been taken from this link, so all the credit for the code goes to this link. I will try to explain the steps with my experience and knowledge.

Converting the tensorflow model into the tensorflow lite

Whole implementation is done using Mnist Dataset which is availabe frrom tensorflow with keras library (tf.keras.datasets.mnist)

# importing tensorflow
# if you have not, please install it using below command
# pip3 install tensorflow
import tensorflow as tf
# importing keras from tensorflow
from tensorflow import keras
# importing numpy to handle arrays
import numpy as np
# to show plots matplotlib is used
import matplotlib.pyplot as plt
# displaying the version of tensorflow
print("TensorFlow version {}".format(tf.__version__))

Step 2: Loading the mnist dataset from tensorflow library

# mnist dataset from tensorflow keras
mnist = tf.keras.datasets.mnist
# loading training as well as testing images as well as labels
(images_train, labels_train),(images_test, labels_test) = mnist.load_data()

Step3: Creating Tensorflow Keras Model

# creating the layers for the sequential model
# input>>dense>>dropout>>dense>>dropout>>dense
# dropout is used for regularization
# softmax layer with 10 neurons/classes is used to predict the outcome
model = tf.keras.models.Sequential(
[
tf.keras.layers.Flatten(input_shape=(28,28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
]
)
# model is compiled with loss function as sparse_categorical_crossentropy and optimizer as adam
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)

Step 4: Fitting the model

# model is fit with training data as well as training labels
# model is trained with 5 epochs
# one can train for more epochs
model.fit(images_train, labels_train, epochs=5)

Step 5: Printing the summary of the model

model.summary()
Output looks like
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
_________________________________________________________________
dense (Dense) (None, 512) 401920
_________________________________________________________________
dropout (Dropout) (None, 512) 0
_________________________________________________________________
dense_1 (Dense) (None, 512) 262656
_________________________________________________________________
dropout_1 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 10) 5130
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0

Step 6: Saving the model

# naming the model
keras_mnist_model = 'mnist_model.h5'
model.save(keras_mnist_model)

Step 7: Saving the tensorflow model into tflite model

tflite_mnist_model = "mnist_model.tflite"
converter = tf.contrib.lite.TFLiteConverter.from_keras_model_file(
keras_mnist_model )
tflite_model = converter.convert()
open(tflite_mnist_model, "wb").write(tflite_model)
Tensorflow Model Vs Tensorflow Lite
Tensorflow Model Vs Tensorflow Lite

Conclusion

I hope that readers gained some good insights about tensorflow lite saving from this post. Special thanks goes to Mirek Stanek for writing an article on this and special thanks to code that he has shared as this code belongs to him and I just made the code more elaborate to help readers more.

Originally published at http://ersanpreet.wordpress.com on July 28, 2019.

--

--