<jupyter_start><jupyter_text>Dynamic range quantization<jupyter_code>converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()
with open('extractor_2_dynrange.tflite', 'wb') as f:
    f.write(tflite_quant_model)<jupyter_output><empty_output><jupyter_text>Full integer quantization<jupyter_code># def representative_dataset():
#     for data in tf.data.Dataset.from_tensor_slices((images)).batch(1).take(100):
#         yield [tf.dtypes.cast(data, tf.float32)]

def representative_dataset():
    for _ in range(100):
        data = np.random.rand(1, 244, 244, 3)
        yield [data.astype(np.float32)]
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset
tflite_quant_model = converter.convert()
with open('extractor_2_fullint.tflite', 'wb') as f:
    f.write(tflite_quant_model)<jupyter_output><empty_output><jupyter_text>Float16 quantization<jupyter_code>converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.float16]
tflite_quant_model = converter.convert()
with open('extractor_2_float16.tflite', 'wb') as f:
    f.write(tflite_quant_model)<jupyter_output><empty_output><jupyter_text>Opening a TFLite model<jupyter_code>model = keras.models.load_model("extractor_2.h5")
model.compile()
random_image = np.random.rand(1, 112, 112, 3)
features = model.predict(random_image)
features[0][:10]
interpreter = tf.lite.Interpreter("extractor_2_float16.tflite")
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
isinstance(interpreter, tf.lite.Interpreter)
input_data = np.array(random_image, dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
features[0][:10]<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup