Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert pre-trained deepspeech-0.9.3-models.tflite from float32 to int8 or int16? #3807

Open
fzhou-1206 opened this issue Aug 27, 2024 · 2 comments

Comments

@fzhou-1206
Copy link

I want to run deepspeech.tflite on NPU and need to get deepspeech-0.9.3-models.tflite in int8 or int16 format. How can I use existing deepspeech-0.9.3-models.tflite float32 type to get an int type?

@Mari-selvam
Copy link

import tensorflow as tf

# Load the float32 TFLite model
with open("deepspeech-0.9.3-models.tflite", "rb") as f:
    tflite_model = f.read()

# Initialize TFLite Converter with float32 model
converter = tf.lite.TFLiteConverter.from_saved_model("path_to_saved_model")

# Apply Integer Quantization (int8 or int16)
# For int8:
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8  # or tf.int16
converter.inference_output_type = tf.int8  # or tf.int16
# Representative dataset generator for quantization
def representative_data_gen():
    # This function should yield data in the same shape as model input
    for _ in range(100):  # Adjust according to your dataset
        yield [your_input_data]  # Replace `your_input_data` with actual samples

converter.representative_dataset = representative_data_gen

# Convert the model
quantized_tflite_model = converter.convert()

# Save the quantized model
with open("deepspeech-quantized.tflite", "wb") as f:
    f.write(quantized_tflite_model)

@fzhou-1206
Copy link
Author

fzhou-1206 commented Nov 4, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants