Tuesday, 19 October 2021

Understand tflite interpreter output and map it to probabilties of classes

I have trained an ssd model (for object detection) using a pre-trained SSD model from Google and converted it to tflite. I trained it for 10 classes and convert it to tflite. Below is the code that I used to call converted tflite model to inspect the results

import tensorflow as tf
MODEL_PATH = 'tflite_model_path'
IMAGE_PATH = 'image of .jpeg or .png format'

interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
interpreter.allocate_tensors()
img = cv2.imread(IMAGE_PATH)  
image_np = np.array(img)
resized_image = tf.image.resize(image_np, [320, 320])
input_data = tf.convert_to_tensor(np.expand_dims(resized_image, 0), dtype=tf.uint8)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])

print(output_data)
[[0.05507272 0.6531384  0.94264597 1.0628431 ]
[0.09443301 0.23600875 0.86310023 0.59367293]
[0.28634408 0.27470273 0.8326082  0.4465307 ]
[0.04376397 0.27534395 0.92427313 0.9200937 ]
[0.00423892 0.824869   0.09153695 0.9980754 ]
[0.63915586 0.6903409  0.9311851  0.97774005]
[0.11331517 0.25821632 0.67732155 0.4566245 ]
[0.4935118  0.27333832 0.82703865 0.4118209 ]
[0.04359788 0.68944013 0.39454672 1.0057622 ]
[0.3145248  0.13302818 1.0334518  0.9483361 ]]]

Now when calling my tflite model using a tflite interpreter, a few things are not clear to me:

  1. What output_details is returning?
  2. Shape of output_details is tf.Tensor([ 1 10 4], shape=(3,), dtype=int32). What does this shape and these numbers represent?
  3. How to convert this output to the prob corresponding to each class?


from Understand tflite interpreter output and map it to probabilties of classes

No comments:

Post a Comment