# TFLite model load error

**URL:** <https://forums.openmv.io/t/tflite-model-load-error/1994>\
**Category:** OpenMV Boards\
**Created:** [November 4, 2020, 7:28pm UTC](https://forums.openmv.io/t/tflite-model-load-error/1994 "2020-11-04T19:28:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)\
**Post date:** [November 4, 2020, 7:28pm UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/1 "2020-11-04T19:28:49Z")

</div>

I have created and tested a fully uint8 quantized TFLite model on PC.

When loading the model in OpenMV with tfmodel = tf.load(“Model\_UInt8\_Quant\_V8.tflite”, load\_to\_fb=True), I’m getting this error:

OSError: tensorflow/lite/micro/kernels/quantize.cc:68 input-\>type == kTfLiteFloat32 || input-\>type == kTfLiteInt16 || input-\>type == kTfLiteInt8 was not true.  
Node QUANTIZE (number 0f) failed to prepare with status 1  
AllocateTensors() failed!

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [November 4, 2020, 8:44pm UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/2 "2020-11-04T20:44:45Z")

</div>

One of the layers in your model is not quantized.

If possible please use Edge Impulse.

---

<div class="post-metadata">

**Author:** ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)\
**Post date:** [November 5, 2020, 12:50am UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/3 "2020-11-05T00:50:04Z")

</div>

Both input and output layers of my quantized tflite model are uint8

TFLite input details [{‘name’: ‘rescaling\_6\_input’, ‘index’: 21, ‘shape’: array([1, 64, 64, 1]), ‘shape\_signature’: array([-1, 64, 64, 1]), ‘dtype’: \<class ‘numpy.uint8’\>, ‘quantization’: (1.0, 0), ‘quantization\_parameters’: {‘scales’: array([1.], dtype=float32), ‘zero\_points’: array([0]), ‘quantized\_dimension’: 0}, ‘sparsity\_parameters’: {}}]

TFLite output details [{‘name’: ‘Identity’, ‘index’: 22, ‘shape’: array([1, 2]), ‘shape\_signature’: array([-1, 2]), ‘dtype’: \<class ‘numpy.uint8’\>, ‘quantization’: (0.025388231500983238, 124), ‘quantization\_parameters’: {‘scales’: array([0.03], dtype=float32), ‘zero\_points’: array([124]), ‘quantized\_dimension’: 0}, ‘sparsity\_parameters’: {}}]

The tf.load error message seems to be that it is not accepting input type uint8.  
Error message “input-\>type == kTfLiteFloat32 || input-\>type == kTfLiteInt16 || input-\>type == kTfLiteInt8 was not true”

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [November 5, 2020, 4:38am UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/4 "2020-11-05T04:38:18Z")

</div>

Please try int8.

CMSISNN is not optimized for uint8. The mode will run slower even if it were passing that check.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [November 5, 2020, 4:38am UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/5 "2020-11-05T04:38:22Z")

</div>

Please try int8.

CMSISNN is not optimized for uint8. The mode will run slower even if it were passing that check.

---

<div class="post-metadata">

**Author:** ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)\
**Post date:** [November 5, 2020, 7:26am UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/6 "2020-11-05T07:26:45Z")

</div>

Which version of tflite is used in openmv firmware?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [November 5, 2020, 6:32pm UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/7 "2020-11-05T18:32:27Z")

</div>

We’re using this branch/commit: [GitHub - openmv/tensorflow at 6a303b679d1a7e9978a05129d20c3eb578509fdb](https://github.com/openmv/tensorflow/tree/6a303b679d1a7e9978a05129d20c3eb578509fdb)

---

<div class="post-metadata">

**Author:** ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)\
**Post date:** [November 5, 2020, 7:09pm UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/8 "2020-11-05T19:09:23Z")

</div>

I got the int8 tflite model working in python on PC. Was missing the scaling step.

The example for tflite has a scaling step before sending to the image to the tflite model: [https://www.tensorflow.org/lite/performance/post\_training\_integer\_quant](https://www.tensorflow.org/lite/performance/post_training_integer_quant)

# rescale input data to uint8

input\_scale, input\_zero\_point = input\_details[“quantization”]  
test\_image = test\_image / input\_scale + input\_zero\_point

Does tf.classify perform this scaling step, or does it need to be done to the image before sending to tf.classify?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [November 5, 2020, 8:28pm UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/9 "2020-11-05T20:28:43Z")

</div>

We convert the model from RGB888 to whatever the input layer format is. I.e. uint8, int8, or float. We don’t do rescaling to maximize the bounds however.

---

<div class="post-metadata">

**Author:** ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)\
**Post date:** [November 5, 2020, 11:40pm UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/10 "2020-11-05T23:40:01Z")

</div>

Sorry, I should have mentioned I’m working with grayscale. Does tf.classify also convert grayscale images to the tflite int8 input format?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [November 6, 2020, 2:49pm UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/11 "2020-11-06T14:49:26Z")

</div>

Yes, also, just read the code if you have more questions:

[https://github.com/openmv/openmv/blob/master/src/omv/py/py\_tf.c#L221](https://github.com/openmv/openmv/blob/master/src/omv/py/py_tf.c#L221)

---

<div class="post-metadata">

**Author:** ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)\
**Post date:** [November 6, 2020, 4:35pm UTC](https://forums.openmv.io/t/tflite-model-load-error/1994/12 "2020-11-06T16:35:26Z")

</div>

Great, Thanks.  
I have confirmed by confusion matrix of grayscale images that an int8 quantized tflite model on PC with tensorflow interpreter AND rescaling, matches the tf.classify on OpenMV WITHOUT rescaling.
