lossing too much accuracy
I have noticed that When i train just for 1 epoch and quantize it to int8, I lose 2-3% but when I train for more epoch I lose more then 30%. Why is the case. Hi @TayyabaZainab0807 

Could you please provide the standalone code to reproduce the issue?

The quantization aware training is supposed to give better model accuracy than post training quantization. Please refer to [document](https://www.tensorflow.org/model_optimization/guide/quantization/training) for QAT and let us know if it helps.

Thanks. Hello @TayyabaZainab0807 

Could you please try to use OPTIMIZE_FOR_SIZE while optimization and use the data type of float for input and output to get the better accuracy. 
And also, try this :
 def generate_representative_dataset():
        for i in range(x_train.shape[0]):
            print(i, end="\r")
            yield [tf.expand_dims(x_train[i], axis=0)]


I am not sure about this, But you can try this once 
 > Hi @TayyabaZainab0807
> 
> Could you please provide the standalone code to reproduce the issue?
> 
> The quantization aware training is supposed to give better model accuracy than post training quantization. Please refer to [document](https://www.tensorflow.org/model_optimization/guide/quantization/training) for QAT and let us know if it helps.
> 
> Thanks.

Thank you for your reply.
Here is a colab notebook:
https://colab.research.google.com/drive/1iw4vJKyMuFTnOY6hyDFj2PJ2ZhBJlWDE?usp=sharing


This are the results for my trail. I get different accuracy every time, the difference in accuracy can be from 1% - more then 50%:
Testing X_test_results_quantized.csv
TP= 145
TN= 1039
FP= 0
FN= 1316
accuracy: 0.4736


Testing X_test_results_normal.csv
TP= 1441
TN= 1028
FP= 11
FN= 20
accuracy: 0.9876 
> OPTIMIZE_FOR_SIZE

@netra2109  Thanks for your reply. I removed the int input and outputs but still face the same challenge. I didn't understand what you meant by OPTIMIZE_FOR_SIZE.

Does this go somewhere here?
converter.optimizations = [tf.lite.Optimize.DEFAULT] 
@TayyabaZainab0807 

yeah, adjust the optimizations if needed. 
 converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]

 > @TayyabaZainab0807
> 
> yeah, adjust the optimizations if needed. converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]

OPTIMIZE_FOR_SIZE also didn't work for me, still got 44% accuracy. Hi @TayyabaZainab0807 

The issue with your code is not initialising the interpreter while testing the samples in the loop. 

Please find the [gist](https://colab.research.google.com/gist/pjpratik/e8926a25ac83179fb29717dc1657608f/60903.ipynb) which obtained `98.04%` accuracy on the converted quantized tflite model.

Thanks. It seems to work now..Thank you very much. Is there any documentation on this? I would like to read a bit into that. Hi @TayyabaZainab0807 

Glad it worked. Please check the [documentation](https://www.tensorflow.org/lite/guide/inference#running_a_model) on steps involved in running a model in TFLite.

Please feel free to close the issue since it is resolved.

Thanks. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60903">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60903">No</a>
 Hello, 

Thank you for your input on this. I am facing exactly the same issue (working with MobilenetV3 on int8 quantisation and observing big accuracy drop / kind of random results depending on a particular attempt - both with PTQ and QAT). The proposed solution did not work, may I please double check if it solved the issues in case of @TayyabaZainab0807 ? Any chance you could please elaborate why it is required to call the following on every iteration? I thought it would be okay to call it only once before the cycle? @pjpratik 
`interpreter = tf.lite.Interpreter(model_path='keras_lstm/model_int8.tflite')
 interpreter.allocate_tensors()` 

Thank you very much.
I have a model composed of some tf.keras.layers.Conv1D and custom Upsampling1DLayer and CustomCropping1D layer:
The model produces correct results after training but when I convert it to tflite with int8 quantization, my accuracy drops by more than 50%.

I am unable to understand why is that? and how can I avoid this much loss.

def tflite_conversion(model):
    run_model = tf.function(lambda x: model(x))
    concrete_func = run_model.get_concrete_function(tf.TensorSpec([1,6000,3], model.inputs[0].dtype))
    MODEL_DIR = "keras_lstm"
    model.save(MODEL_DIR, save_format="tf", signatures=concrete_func)

    converter = tf.lite.TFLiteConverter.from_saved_model(MODEL_DIR)
    Choice="_int8"
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    converter.inference_input_type = tf.int8  # or tf.uint8
    converter.inference_output_type = tf.int8  # or tf.uint8
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8,tf.lite.OpsSet.TFLITE_BUILTINS]
    #converter.experimental_select_user_tf_ops = [Upscaling1D]

    def generate_representative_dataset():
        for i in range(int(x_train.shape[0]/100)):
            print(i,end="\r")
            yield [tf.expand_dims(x_train[i], axis=0)]

    converter.representative_dataset = generate_representative_dataset

    tflite_model = converter.convert()
    open("keras_lstm/model"+Choice+".tflite", "wb").write(tflite_model)
    return tflite_model