How to Deploy TensorFlow Regression Model in Android — TF Lite — Part 1
Have you ever thought of using the Tensorflow model in Android devices? In this topic, you will learn about
- Tensorflow
- Keras
- Create simple regression dataset using Numpy
- Neural network model using Keras
- Train simple regression model using Tensorflow
- Predict using our trained model
- Saving our trained model
- Convert your model to TF Lite model
- Using Interpreter to test Lite model
- Deploy Lite model to Android
Tensorflow
Tensorflow is an open-source artificial intelligence library to train and build machine learning models. Tensorflow runs on a variety of platforms like Cloud, Website, Mobile & Embedded IoT devices. Tensorflow models cannot be directly deployed to the mobile devices, you need to convert to tflite version using TensorFlow Keras library.
Keras
Keras is a high-level API runs above Tensorflow to build and train deep neural network models. Keras is also used by other Machine learning libraries like Pytorch, Theano & CNTK.
Create simple regression datasets
In this section, we are going to create a model to predict the multiple of 2, e.g for input 4 the output will be 8. A simple 2x multiplication logic.
Create a python file e.g regression.py and import the below dependencies
import numpy as np
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
Every model needs data to get trained, let’s create data using Numpy in the below code.
values = np.arange(2, 1000, 2)
labels = np.arange(1, 500, 1)
Variable values will hold the multiplication NumPy array value from 2..1000 and the variable label which acts as the label to be trained will hold the NumPy array values 1..500.
Neural network model using Keras
Let’s create a Sequential model with one Dense layer and compile it. Here we are using optimizer Adam with l2 regularization and Mean Squared Error (MSE) as our loss function.
Since it is a regression model we are not using any metrics like accuracy
model = Sequential([Dense(1, input_shape=[1])])
model.compile(optimizer=keras.optimizers.Adam(lr=2), loss=keras.losses.mean_squared_error)
Train neural network model
Train the compiled model using the fit function for 100 epochs.
model.fit(labels, values, epochs=100)Epoch 97/100499/499 [==============================] - 0s 46us/sample - loss: 1.3670e-15
Epoch 98/100
499/499 [==============================] - 0s 46us/sample - loss: 1.3670e-15
Epoch 99/100
499/499 [==============================] - 0s 46us/sample - loss: 1.3670e-15
Epoch 100/100
499/499 [==============================] - 0s 46us/sample - loss: 1.3670e-15
Predict using the trained model
Using model.predict we can predict the output of any values using our trained model, let's say we want to predict the multiple of 2 for the value 10 as below.
result = model.predict([10])
print(result)[[20.]]
We got the value as 20, which is correct. Our model is trained well to predict the output of multiplication by 2.
Saving our trained model
We don't want to keep training our model multiple times to predict our values, So for that, we can save the model using the save function.
model.save('regression.h5')
The saved model can also be loaded using keras.load, this will help us to save our time from training the model again and again.
tf.keras.models.load_model('regression.h5')
We just finished the first step of deploying TensorFlow models , In the next article we will learn about Converting and deploying tflite model to Android.
You can run the above code from the below colab link
Follow me on Linkedin