Tflite accuracy decreased

I am working on the simple_audio command recognition model, provided by tensorflow. I have successfully converted the model into a tflite model. But, the accuracy of the converted model has downgraded drastically. I used the same test dataset for both the models, but the latter performed poorly. I have also tried quantisation-aware training, but there isn’t much change. I used signal.stft for deriving the spectrogram of the given audio file as I cannot use tf.stft in the inference code. I have tried a lot of different ways to debug it, but am facing issues.

The only difference I can think of is in the process of generating the spectrogram.

IN TF Model its done as this:

def get_spectrogram(waveform):

 zero_padding = tf.zeros([16000] - tf.shape(waveform), dtype=tf.float32)
 waveform = tf.cast(waveform, tf.float32)
 equal_length = tf.concat([waveform, zero_padding], 0)
 spectrogram = tf.signal.stft(
  equal_length, frame_length=255, frame_step=128)
 spectrogram = tf.abs(spectrogram)
 return spectrogram

While in tflite model its written as:

def get_spectrogram1(mySound):
 res = int(''.join(map(str, y)))
 zero_padding=np.zeros((16000) - res,dtype=np.float32)
 f,t,Zw= signal.stft(equal_length, nperseg=247,noverlap=122)
 return Zw

Can someone point out some options or suggestions?

There was a mega thread at:

At the end It seems that there Is a workaround.

You do not have enable any optimizations?

Post the code you have use for convert in tflite.

Yes, I have used optimisation during the conversion of the tflite model. The code used for the conversion is attached below:

converter = tf.lite.TFLiteConverter.from_keras_model(model)

converter.optimizations = [tf.lite.Optimize.DEFAULT]

tflite_model = converter.convert()

with open('model.tflite', 'wb') as f:


Let me know if you want more information.

you can try without optimizations but i do not thing is the issues

do you have try to show refult of get_spectrogram and chek if is the same value?

you can also try to trunk you model and check after eache layer if the result are equal
and found the layer that do a error

modeltruncate = Model(inputs=model.input, outputs=model.layers[x].output) #x is the number of the layer where you trunk

predict=modeltruncate.predict(matrixTest); # your data set

Thanks, will try doing that.