Using tflite model for inference using python

Hi all,

I have a custom trained object detection model (ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8) trained using tensorflow2 API. I have converted to tflite (post training quantization), using the steps mentioned here:

The convertion steps are as follows:
import tensorflow as tf

TEST_DIR = "/home/nxf47724/data/code/python/model_convert"

# A generator that provides a representative dataset
def representative_data_gen():
  dataset_list = + '/quante/*')
  for i in range(100):
    image = next(iter(dataset_list))
    image =
    image =, channels=3)
    image = tf.image.resize(image, [IMAGE_SIZE, IMAGE_SIZE])
    image = tf.cast(image / 255., tf.float32)
    image = tf.expand_dims(image, 0)
    yield [image]

#converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter = tf.lite.TFLiteConverter.from_saved_model('/home/nxf47724/data/code/python/study/models/ssd_mobilenet_v2_320x320_coco17_tpu-8/exported_tflite/saved_model/',signature_keys=['serving_default'])
#converter.experimental_new_converter = False
# This enables quantization
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# This sets the representative dataset for quantization
converter.representative_dataset = representative_data_gen
# This ensures that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# For full integer quantization, though supported types defaults to int8 only, we explicitly declare it for clarity.
converter.target_spec.supported_types = [tf.int8]
# These set the input and output tensors to uint8 (added in r2.3)
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model = converter.convert()

with open('/home/python/study/models/ssd_mobilenet_v2_320x320_coco17_tpu-8/exported_tflite/ssd_mobilenet_v2_quant.tflite', 'wb') as f:

when I am using it in inference, I am getting the classes and scores as below:

classes [128 128 128 128 255 128 128 128 128 128]
scores [255 255 255 255 255 255 255 255 255 255]

and the bounding box values are also not coming properly

How to use int8 quantized model for inference in python? what are the correct preprocessing steps to be followed? and what is the reason for getting output in
128 and 255 format

Sorry, I don’t know that tutorial but given you have the data to train the model, I’d try this tutorial: Object Detection with TensorFlow Lite Model Maker

or try to adapt it.