Created
May 27, 2019 21:20
-
-
Save frogermcs/9cce1a9d599f0a216693be54a80a5d2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!mkdir "tflite_models" | |
TFLITE_MODEL = "tflite_models/flowers.tflite" | |
TFLITE_QUANT_MODEL = "tflite_models/flowers_quant.tflite" | |
# Get the concrete function from the Keras model. | |
run_model = tf.function(lambda x : flowers_model(x)) | |
# Save the concrete function. | |
concrete_func = run_model.get_concrete_function( | |
tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype) | |
) | |
# Convert the model to standard TensorFlow Lite model | |
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func]) | |
converted_tflite_model = converter.convert() | |
open(TFLITE_MODEL, "wb").write(converted_tflite_model) | |
# Convert the model to quantized version with post-training quantization | |
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func]) | |
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE] | |
tflite_quant_model = converter.convert() | |
open(TFLITE_QUANT_MODEL, "wb").write(tflite_quant_model) | |
print("TFLite models and their sizes:") | |
!ls "tflite_models" -lh | |
# >> TFLite models and their sizes: | |
# >> total 11M | |
# >> -rw-r--r-- 1 root root 2.3M May 27 19:10 flowers_quant.tflite | |
# >> -rw-r--r-- 1 root root 8.5M May 27 19:10 flowers.tflite |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment