Practical Exercises Chapter 8
Exercise 1: Convert a TensorFlow Model to TensorFlow Lite
Task: Convert a pre-trained TensorFlow model (e.g., a simple image classification model) to TensorFlow Lite format for edge deployment. Apply quantization to reduce the model size.
Solution:
# Load a pre-trained model (for example, from a saved model directory)
model = tf.keras.models.load_model('my_saved_model')
# Convert the model to TensorFlow Lite format
converter = tf.lite.TFLiteConverter.from_keras_model(model)
# Apply quantization to optimize the model
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
# Save the TensorFlow Lite model to a file
with open('model_quantized.tflite', 'wb') as f: