Skip to content

Instantly share code, notes, and snippets.

@dansitu
Created March 14, 2019 17:33
Show Gist options
  • Save dansitu/7400095f683ce536226bf7a0e0cb3205 to your computer and use it in GitHub Desktop.
Save dansitu/7400095f683ce536226bf7a0e0cb3205 to your computer and use it in GitHub Desktop.
# Load TensorFlow
import tensorflow as tf
# Set up the converter
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
# Perform conversion and output file
tflite_quant_model = converter.convert()
output_dir.write_bytes(tflite_quant_model)
@awitwicki
Copy link

awitwicki commented Nov 4, 2019

Unfortunately this code doesn't helped me.

$ sudo edgetpu_compiler coral.tflite

Edge TPU Compiler version 2.0.267685300
Invalid model: coral.tflite
Model not quantized

P.S. I found solution

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=1)
model.evaluate(x_test,  y_test, verbose=2)

image_shape = (28, 28)
def representative_dataset_gen():
    num_calibration_images = 10
    for i in range(num_calibration_images):
        image = tf.random.normal([1] + list(image_shape))
        yield [image]

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

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.representative_dataset = representative_dataset_gen
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_quant_model = converter.convert()

open("coral8.tflite", "wb").write(tflite_quant_model)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment