Search code examples
pythontensorflowgoogle-coral

convert .pb model into quantized tflite model


Totally new to Tensorflow, I have created one object detection model (.pb and .pbtxt) using 'faster_rcnn_inception_v2_coco_2018_01_28' model I found from TensorFlow zoo. It works fine on windows but I want to use this model on google coral edge TPU. How can I convert my frozen model into edgetpu.tflite quantized model?


Solution

  • There are 2 more steps to this pipeline:

    enter image description here

    1) Convert the .pb -> tflite:

    I won't go through details since there are documentation on this on tensorflow official page and it changes very often, but I'll still try to answer specifically to your question. There are 2 ways of doing this:

    • Quantization Aware Training: this happens during training of the model. I don't think this applies to you since your question seems to indicates that you were not aware of this process. But please correct me if I'm wrong.

    • Post Training Quantization: Basically loading your model where all tensors are of type float and convert it to a tflite form with int8 tensors. Again, I won't go into too much details, but I'll give you 2 actual examples of doing so :) a) with code b) with tflite_convert tools

    2) Compile the model from tflite -> edgetpu.tflite:

    Once you have produced a fully quantized tflite model, congrats your model is now much more efficient for arm platform and the size is much smaller. However it will still be ran on the CPU unless you compile it for the edgetpu. You can review this doc for installation and usage. But compiling it is as easy as:

    $ edgetpu_compiler -s your_quantized_model.tflite
    

    Hope this helps!