TensorFlow is a powerful tool that help us to integrate python in Mobile app. I already wrote a blog about the TensorFlow implementation in iOS. Click here to Visit blog.
In this blog, I will show how to convert a model to .tflite. We assume that you have already created a model in Python.You have the following two options for using the converter:
- Python API (recommended): This makes it easier to convert models as part of the model development pipeline, apply optimisations, add metadata and has many more features.
- Command line: This only supports basic model conversion.
Let start:
1. Using Python: Suppose we have model in directory fruits.
1 2 3 4 5 6 7 8 9 |
import tensorflow as tf # Convert the model converter = tf.lite.TFLiteConverter.from_saved_model(fruits_dir) tflite_model = converter.convert() # Save the model. with open('fruits_model.tflite', 'wb') as f: f.write(tflite_model) |
Note: you can execute this code from local or colab.
2. Using Command: To execute the cmd you have to install TensorFlow with pip in your system. Here is the step to install TenserFlow.
Now run below command.
1 2 3 |
tflite_convert \ --saved_model_dir=/tmp/fruits_model \ --output_file=/tmp/fruits_model.tflite |
Visit git reference to know about command.
Note: TensorFlow not recommended this because it only supports basic model conversion.
Conclusion:
Using this code and command we can easily create MobileNet mode.
I hope this Blog will help you to create .tflite file. If you feel any doubt or query please comment below.
Visit my other blogs click here.
Thank you.