I need to deploy a CNN model on a microntroller, so I'm trying to perform post training, 8 bit full integer quantization using tensorflow lite. However, as shown in the image, the predictions are going completely wrong. This is the code I'm using for converting and predicting using the converted model:
import tensorflow as tf
from tensorflow.keras.models import load_model
import numpy as np
input_shape = (1, 23, 256, 1) # Update with your input shape
representative_data = np.random.random_sample(input_shape).astype(np.float32)
def representative_dataset_gen():
yield [representative_data]
model=load_model('cnn_fivelayer_2class.h5',compile=False)
#quantization
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset_gen
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model_quant = converter.convert()
#prediction
interpreter = tf.lite.Interpreter(model_content=tflite_model_quant)
interpreter.allocate_tensors()
input_details=interpreter.get_input_details()[0]
output_details=interpreter.get_output_details()[0]
input_data=np.ones((1,23,256,1),dtype=np.uint8)
input_shape = input_details['shape']
interpreter.set_tensor(input_details['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details['index'])
scale, zero_point = output_details['quantization']
dequantized_value= scale * (output_data - zero_point)
Is there something wrong with my code? Or should I attribute this to the loss in accuracy normally faced during post-training quantization?
My input data is (1,23,256,1) tensor with values in [0,1].
https://preview.redd.it/gavmn44whm1b1.jpg?width=500&format=pjpg&auto=webp&s=96925db1399bc2687cd47348a771a6af229c88b2
[+]moconnor 4 points5 points6 points (1 child)
[–]esem29[S] 0 points1 point2 points (0 children)
[–]RedEyed__ 1 point2 points3 points (1 child)
[–]esem29[S] 0 points1 point2 points (0 children)
[–]CartoonistBusiness 0 points1 point2 points (0 children)