Alien Queen
Alien Queen
DIIDevHeads IoT Integration Server
Created by Enthernet Code on 8/13/2024 in #middleware-and-os
Efficiently Converting and Quantizing a Trained Model to TensorFlow Lite
TensorFlow Lite also supports other types of quantization, such as full integer quantization or float16 quantization. If you want more control over how the quantization is applied, you can specify the type of quantization during conversion. For instance, if you want to use full integer quantization, you can modify your code to be like this
import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.int8]
tflite_model = converter.convert()

with open('xray_model_integer_quantized.tflite', 'wb') as f:
f.write(tflite_model)

print("Model successfully converted with full integer quantization!")
import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.int8]
tflite_model = converter.convert()

with open('xray_model_integer_quantized.tflite', 'wb') as f:
f.write(tflite_model)

print("Model successfully converted with full integer quantization!")
This will convert both the weights and activations to int8, which is often the most efficient for microcontrollers. Although quantization the accuracy of your model may be affected so you would ahv to test it again to ensure it meets your accuracy requirements, you can test the original and quantized to see if theres any notable difference in model capability.
5 replies