# Tflite accuracy decreased

**URL:** <https://discuss.ai.google.dev/t/tflite-accuracy-decreased/30873>\
**Category:** General Discussion\
**Tags:** datasets, models, help\_request\
**Created:** [July 23, 2021, 9:32am UTC](https://discuss.ai.google.dev/t/tflite-accuracy-decreased/30873 "2021-07-23T09:32:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prachi\_Mittal](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/prachi_mittal/32/8045_2.png) [@Prachi\_Mittal](https://discuss.ai.google.dev/u/Prachi_Mittal)\
**Post date:** [July 23, 2021, 9:32am UTC](https://discuss.ai.google.dev/t/tflite-accuracy-decreased/30873/1 "2021-07-23T09:32:47Z")

</div>

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:

```auto
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:

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

```

Can someone point out some options or suggestions?

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [July 23, 2021, 10:07am UTC](https://discuss.ai.google.dev/t/tflite-accuracy-decreased/30873/3 "2021-07-23T10:07:53Z")

</div>

There was a mega thread at:

[https://github.com/tensorflow/tensorflow/issues/27303#issuecomment-788155786](https://github.com/tensorflow/tensorflow/issues/27303#issuecomment-788155786)

At the end It seems that there Is a workaround.

---

<div class="post-metadata">

**Author:** ![cortexet](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/cortexet/32/8284_2.png) [@cortexet](https://discuss.ai.google.dev/u/cortexet)\
**Post date:** [July 23, 2021, 10:08am UTC](https://discuss.ai.google.dev/t/tflite-accuracy-decreased/30873/4 "2021-07-23T10:08:32Z")

</div>

You do not have enable any optimizations?

Post the code you have use for convert in tflite.

---

<div class="post-metadata">

**Author:** ![Prachi\_Mittal](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/prachi_mittal/32/8045_2.png) [@Prachi\_Mittal](https://discuss.ai.google.dev/u/Prachi_Mittal)\
**Post date:** [August 3, 2021, 6:27am UTC](https://discuss.ai.google.dev/t/tflite-accuracy-decreased/30873/6 "2021-08-03T06:27:25Z")

</div>

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:

f.write(tflite_model)

```

Let me know if you want more information.

---

<div class="post-metadata">

**Author:** ![cortexet](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/cortexet/32/8284_2.png) [@cortexet](https://discuss.ai.google.dev/u/cortexet)\
**Post date:** [August 4, 2021, 12:10pm UTC](https://discuss.ai.google.dev/t/tflite-accuracy-decreased/30873/8 "2021-08-04T12:10:31Z")

</div>

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.output) #x is the number of the layer where you trunk  
modeltruncate.summary()

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

---

<div class="post-metadata">

**Author:** ![Prachi\_Mittal](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/prachi_mittal/32/8045_2.png) [@Prachi\_Mittal](https://discuss.ai.google.dev/u/Prachi_Mittal)\
**Post date:** [August 4, 2021, 12:13pm UTC](https://discuss.ai.google.dev/t/tflite-accuracy-decreased/30873/10 "2021-08-04T12:13:10Z")

</div>

Thanks, will try doing that.
