How to convert .pb to TFLite format ?

Tensorflow:

TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries and community resources that lets researchers push the state-of-the-art in Machine Learning. Furthermore, tensorflow helps developers easily build and deploy Machine Learning powered applications. Additionally tflite come together with tensorflow which is useful for building different applications.

Inceptionv3:

Transfer learning is a machine learning method which utilizes a pre-trained neural network. For example, the image recognition model called Inception-v3 . It consists of :

  • Feature extraction part with a convolutional neural network.
  • Classification part with fully-connected and softmax layers.

Conversion to TFLITE:

After  retraining the machine learning model in your own data set, you get a file with .pb extension. But, sometimes you may need to convert the .pb extension file into the .tflite extension file for android application. In that case you need to do some steps which is described below. This tutorial doesn’t  cover steps for object detection model.

Step 1:

Install a python virtual environment platform using the command and  “sudo apt-get install python3-venv“.

Step 2:

Create a virtual environment using a command  “python3 -m venv <virtual environment name>” Example. python3 -m venv convert-to-tflite. After that, activate the virtual environment using command “source venv/bin/activate”

Step 3:

Install required Tensorflow libraries. I assume If you train your model in your computer you definitely have those libraries. If you don’t have tensorflow then you can follow our another tutorial easy steps to install tensorflow  and youtube channel kharbari with tutorial video  How to install Tensorfow 2.X and 1.X on ubuntu System?  After that , install toco libraries which is used to convert the pb extension file into tflite. Command to install toco is :

pip3 install toco
Step 4:

Find the input , output array name and tensor shape  required  to convert .pb file into .tflite file using python script below.

import tensorflow as tf

def load_graph(frozen_graph_filename):
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name="prefix")
    return graph


if __name__ == '__main__':
    graph = load_graph('/home/sgc/tutorial-directory/myfile.pb')
    for op in graph.get_operations():
        abc = graph.get_tensor_by_name(op.name + ":0")
        print(abc)

From python script you will get input and output tensor and tensor size . Figure below shows the input tensor name , shape and output tensor name.

Input Tensor Name and Shape.
Output Tensor Name
Step 5:

This is the final step of conversion to tflite file. The command for the conversion to tflite is:

toco --graph_def_file=myfile.pb --output_file=output.tflite --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --input_shape=1,299,299,3 --input_array=ResizeBilinear --output_array=final_result --inference_type=FLOAT --input_type=FLOAT
Final Conversion Step to Tflite

You can watch the video on our youtube channel kharbari showing detail steps.

To sum up, you can’t use these above steps for the object detection model, however you can use this for inception-v3 model. The steps for objection detection model is quiet complex. Detail steps for object detection model is in our another post “How to convert .pb to TFLite format (Object Detection )?” 

About sgc908

Graduate Research Assistant at North Dakota State University, Precision Agriculture, Machine Learning, Deep Learning and Big Data.

View all posts by sgc908 →