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
%cd /mydrive/customTF2/data/ | |
'''********************************** | |
FOR FLOATING-POINT INFERENCE | |
**********************************''' | |
import tensorflow as tf | |
saved_model_dir = '/mydrive/customTF2/data/tflite/saved_model' | |
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) | |
tflite_model = converter.convert() | |
open("/mydrive/customTF2/data/tflite/detect.tflite", "wb").write(tflite_model) | |
'''************************************************** | |
FOR FLOATING-POINT INFERENCE WITH OPTIMIZATIONS | |
***************************************************''' | |
# import tensorflow as tf | |
# converter = tf.lite.TFLiteConverter.from_saved_model('/mydrive/customTF2/data/tflite/saved_model',signature_keys=['serving_default']) | |
# converter.optimizations = [tf.lite.Optimize.DEFAULT] | |
# converter.experimental_new_converter = True | |
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, | |
# tf.lite.OpsSet.SELECT_TF_OPS] | |
# tflite_model = converter.convert() | |
# with tf.io.gfile.GFile('/mydrive/customTF2/data/tflite/detect.tflite', 'wb') as f: | |
# f.write(tflite_model) | |
'''********************************** | |
FOR DYNAMIC RANGE QUANTIZATION | |
************************************* | |
The model is now a bit smaller with quantized weights, but other variable data is still in float format.''' | |
# import tensorflow as tf | |
# converter = tf.lite.TFLiteConverter.from_saved_model('/mydrive/customTF2/data/tflite/saved_model',signature_keys=['serving_default']) | |
# converter.optimizations = [tf.lite.Optimize.DEFAULT] | |
# tflite_quant_model = converter.convert() | |
# with tf.io.gfile.GFile('/mydrive/customTF2/data/tflite/detect.tflite', 'wb') as f: | |
# f.write(tflite_quant_model) | |
# '''*********************************************************************** | |
# FOR INTEGER WITH FLOAT FALLBACK QUANTIZATION WITH DEFAULT OPTMIZATIONS | |
# ************************************************************************** | |
# Now all weights and variable data are quantized, and the model is significantly smaller compared to the original TensorFlow Lite model. | |
# However, to maintain compatibility with applications that traditionally use float model input and output tensors, | |
# the TensorFlow Lite Converter leaves the model input and output tensors in float''' | |
# import tensorflow as tf | |
# import numpy as np | |
# saved_model_dir = '/mydrive/customTF2/data/tflite/saved_model' | |
# def representative_dataset(): | |
# for _ in range(100): | |
# data = np.random.rand(1, 320, 320, 3) | |
# yield [data.astype(np.float32)] | |
# converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) | |
# converter.optimizations = [tf.lite.Optimize.DEFAULT] | |
# converter.representative_dataset = representative_dataset | |
# tflite_quant_model = converter.convert() | |
# with open('/mydrive/customTF2/data/tflite/detect.tflite', 'wb') as f: | |
# f.write(tflite_quant_model) | |
# '''********************************* | |
# FOR FULL INTEGER QUANTIZATION | |
# ************************************ | |
# The internal quantization remains the same as previous float fallback quantization method, | |
# but you can see the input and output tensors here are also now integer format''' | |
# import tensorflow as tf | |
# import numpy as np | |
# saved_model_dir = '/mydrive/customTF2/data/tflite/saved_model' | |
# def representative_dataset(): | |
# for _ in range(100): | |
# data = np.random.rand(1, 320, 320, 3) | |
# yield [data.astype(np.float32)] | |
# converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) | |
# converter.optimizations = [tf.lite.Optimize.DEFAULT] | |
# converter.representative_dataset = representative_dataset | |
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] | |
# converter.inference_input_type = tf.uint8 | |
# converter.inference_output_type = tf.uint8 | |
# tflite_quant_model_full_int = converter.convert() | |
# with open('/mydrive/customTF2/data/tflite/detect.tflite', 'wb') as f: | |
# f.write(tflite_quant_model_full_int) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment