Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpret TFLite output tensor

I am executing a TFLite model on python in order to make predictions based on input data. The model has been trained on AutoML-Google-API, then I downloaded its TFLite model. I used tf.lite.Interpreter to load the model and run an inference as follows

input_details = interpreter.get_input_details();print(input_details )
output_details = interpreter.get_output_details();print(output_details )
//...preparing input_data
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index']);print(output_data )

The results are as follows:

input_details: 
[{'name': 'image',
  'index': 0,
  'shape': array([  1, 224, 224,   3]),
  'dtype': numpy.uint8,
  'quantization': (0.007874015718698502, 128)}]
output_details: 
[{'name': 'scores',
  'index': 173,
  'shape': array([ 1, 10]),
  'dtype': numpy.uint8,
  'quantization': (0.00390625, 0)}]
output_data : 
array([[ 34, 100,  67,  14,  15,  24,  21,  18,  25,  37]], dtype=uint8)

The output_data has some integer numbers, is it true to say that "the index of its largest number corresponds to the predicted label", and how can I convert those numbers to probability?

like image 269
Hadi Zanddizari Avatar asked Mar 22 '26 10:03

Hadi Zanddizari


1 Answers

You could use softmax to convert the numbers into probability distribution and argmax to get the index of the label with the largest probability.

So something like this:

output_probs = tf.math.softmax(output_data)
pred_label = tf.math.argmax(output_probs)
like image 161
sakumoil Avatar answered Mar 24 '26 00:03

sakumoil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!