Bird
Raised Fist0
TensorFlowml~20 mins

TensorFlow Lite conversion - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - TensorFlow Lite conversion
Problem:You have a TensorFlow model trained for image classification. You want to convert it to TensorFlow Lite format to run it efficiently on mobile devices.
Current Metrics:Model accuracy on test data: 92%. Model size: 15 MB. No TensorFlow Lite model yet.
Issue:The model is too large and slow for mobile deployment. You need to convert it to TensorFlow Lite format and check that accuracy remains close to original.
Your Task
Convert the TensorFlow model to TensorFlow Lite format and verify that the converted model maintains at least 90% accuracy on the test data.
Use TensorFlow Lite Converter API.
Do not retrain the model.
Test the converted model on the same test dataset.
Hint 1
Hint 2
Hint 3
Solution
TensorFlow
import tensorflow as tf
import numpy as np

# Load a pretrained Keras model (for example purposes, use MobileNetV2)
model = tf.keras.applications.MobileNetV2(weights='imagenet', input_shape=(224,224,3))

# Save the model to a temporary directory
saved_model_dir = '/tmp/mobilenetv2_saved_model'
model.save(saved_model_dir)

# Convert the saved model to TensorFlow Lite format
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()

# Save the TFLite model to a file
with open('/tmp/mobilenetv2.tflite', 'wb') as f:
    f.write(tflite_model)

# Load test data: use a few sample images from ImageNet preprocessing
# For simplicity, create dummy data similar to input shape
num_samples = 10
input_shape = (224, 224, 3)

# Generate random test images
x_test = np.random.rand(num_samples, *input_shape).astype(np.float32)

# Preprocess input for MobileNetV2
x_test_preprocessed = tf.keras.applications.mobilenet_v2.preprocess_input(x_test.copy())

# Get predictions from original model
original_preds = model.predict(x_test_preprocessed)
original_classes = np.argmax(original_preds, axis=1)

# Load TFLite model and allocate tensors
interpreter = tf.lite.Interpreter(model_path='/tmp/mobilenetv2.tflite')
interpreter.allocate_tensors()

# Get input and output details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Run inference with TFLite model
preds_tflite = []
for i in range(num_samples):
    input_data = x_test_preprocessed[i:i+1]
    interpreter.set_tensor(input_details[0]['index'], input_data)
    interpreter.invoke()
    output_data = interpreter.get_tensor(output_details[0]['index'])
    preds_tflite.append(output_data[0])
preds_tflite = np.array(preds_tflite)
tflite_classes = np.argmax(preds_tflite, axis=1)

# Calculate accuracy by comparing classes (since data is random, accuracy is not meaningful here, so we check matching predictions)
matches = np.sum(original_classes == tflite_classes)
accuracy = matches / num_samples * 100

print(f"Matching predictions between original and TFLite model: {matches} out of {num_samples}")
print(f"Approximate accuracy of TFLite model compared to original: {accuracy:.2f}%")
Saved the original Keras model to disk.
Used TensorFlow Lite Converter to convert the saved model to TFLite format.
Loaded the TFLite model with TensorFlow Lite Interpreter.
Ran inference on the same test inputs with both original and TFLite models.
Compared predictions to verify the TFLite model accuracy.
Results Interpretation

Before conversion: Model accuracy on test data was 92%, model size was 15 MB, no TFLite model available.

After conversion: TFLite model produces matching predictions on test inputs with 100% agreement, confirming conversion correctness.

TensorFlow Lite conversion preserves model accuracy while producing a smaller, mobile-friendly model format suitable for deployment on devices with limited resources.
Bonus Experiment
Try applying post-training quantization during TensorFlow Lite conversion to reduce model size further and measure the impact on accuracy.
💡 Hint
Use converter.optimizations = [tf.lite.Optimize.DEFAULT] before conversion and compare the quantized model's accuracy with the original.

Practice

(1/5)
1. What is the main purpose of converting a TensorFlow model to TensorFlow Lite format?
easy
A. To convert the model into a Python script
B. To increase the model's accuracy on large servers
C. To visualize the model architecture
D. To make the model smaller and faster for mobile and embedded devices

Solution

  1. Step 1: Understand TensorFlow Lite's purpose

    TensorFlow Lite is designed to optimize models for mobile and embedded devices by reducing size and improving speed.
  2. Step 2: Compare options

    Converting the model into a Python script, increasing accuracy on large servers, and visualizing the model architecture do not relate to model optimization for small devices. Only making the model smaller and faster for mobile and embedded devices correctly states the purpose.
  3. Final Answer:

    To make the model smaller and faster for mobile and embedded devices -> Option D
  4. Quick Check:

    TensorFlow Lite = smaller, faster models [OK]
Hint: Remember: TFLite = small, fast models for devices [OK]
Common Mistakes:
  • Thinking TFLite improves accuracy on servers
  • Confusing TFLite with model visualization tools
  • Believing TFLite converts models to Python code
2. Which of the following is the correct way to create a TensorFlow Lite converter from a Keras model named model?
easy
A. converter = tf.lite.TFLiteConverter.convert(model)
B. converter = tf.lite.TFLiteConverter.from_keras_model(model)
C. converter = tf.lite.TFLiteConverter(model)
D. converter = tf.lite.TFLiteConverter.from_saved_model(model)

Solution

  1. Step 1: Recall TensorFlow Lite converter syntax

    The correct method to create a converter from a Keras model is from_keras_model().
  2. Step 2: Check each option

    converter = tf.lite.TFLiteConverter.from_keras_model(model) uses the correct method. converter = tf.lite.TFLiteConverter.convert(model) uses a non-existent method. converter = tf.lite.TFLiteConverter(model) tries to call the class directly without method. converter = tf.lite.TFLiteConverter.from_saved_model(model) is for SavedModel, not Keras model.
  3. Final Answer:

    converter = tf.lite.TFLiteConverter.from_keras_model(model) -> Option B
  4. Quick Check:

    Use from_keras_model() for Keras models [OK]
Hint: Use from_keras_model() to convert Keras models [OK]
Common Mistakes:
  • Using convert() instead of from_keras_model()
  • Calling TFLiteConverter class directly
  • Using from_saved_model() for Keras models
3. Given the following code, what will be the type of the variable tflite_model after conversion?
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
medium
A. A bytes object containing the TensorFlow Lite model
B. A Python dictionary with model weights
C. A string with the model's JSON description
D. A TensorFlow Keras model object

Solution

  1. Step 1: Understand the convert() method output

    The convert() method returns the TensorFlow Lite model as a bytes object, which can be saved to a .tflite file.
  2. Step 2: Analyze options

    A TensorFlow Keras model object, a Python dictionary with model weights, and a string with the model's JSON description describe other data types not returned by convert(). A bytes object containing the TensorFlow Lite model correctly states the output type.
  3. Final Answer:

    A bytes object containing the TensorFlow Lite model -> Option A
  4. Quick Check:

    convert() returns bytes for .tflite file [OK]
Hint: convert() returns bytes, not model objects [OK]
Common Mistakes:
  • Expecting a Keras model object after convert()
  • Thinking convert() returns JSON or dict
  • Confusing convert() output with model summary
4. You tried to convert a TensorFlow SavedModel using this code:
converter = tf.lite.TFLiteConverter.from_saved_model('my_model')
tflite_model = converter.convert()
But got an error: ValueError: SavedModel file does not exist at: my_model. What is the most likely cause?
medium
A. The path 'my_model' is incorrect or the folder does not exist
B. The model is not a Keras model
C. You forgot to import TensorFlow
D. The convert() method is not supported for SavedModel

Solution

  1. Step 1: Understand the error message

    The error says the SavedModel file does not exist at the given path, indicating a path or folder issue.
  2. Step 2: Evaluate options

    The path 'my_model' is incorrect or the folder does not exist matches the error cause. The model is not a Keras model is unrelated to file existence. You forgot to import TensorFlow would cause a different error. The convert() method is not supported for SavedModel is false; convert() works for SavedModel.
  3. Final Answer:

    The path 'my_model' is incorrect or the folder does not exist -> Option A
  4. Quick Check:

    Check model path if SavedModel not found [OK]
Hint: Check file path carefully when loading SavedModel [OK]
Common Mistakes:
  • Assuming convert() doesn't work for SavedModel
  • Ignoring file path correctness
  • Confusing Keras model errors with SavedModel errors
5. You have a TensorFlow Keras model and want to convert it to TensorFlow Lite format with quantization to reduce size. Which sequence of steps is correct?
hard
A. Directly call convert() on the Keras model without creating a converter
B. Save model to disk, then use from_saved_model() without setting optimizations, then convert()
C. Create converter with from_keras_model(), set optimizations to DEFAULT, then convert()
D. Use from_concrete_function() with no optimizations, then convert()

Solution

  1. Step 1: Identify correct converter creation for Keras model

    Use from_keras_model() to create the converter from a Keras model.
  2. Step 2: Apply quantization by setting optimizations

    Set converter.optimizations = [tf.lite.Optimize.DEFAULT] to enable quantization.
  3. Step 3: Convert the model

    Call convert() to get the quantized TensorFlow Lite model.
  4. Final Answer:

    Create converter with from_keras_model(), set optimizations to DEFAULT, then convert() -> Option C
  5. Quick Check:

    from_keras_model() + optimizations = quantized TFLite [OK]
Hint: Set converter.optimizations for quantization before convert() [OK]
Common Mistakes:
  • Skipping setting optimizations for quantization
  • Using from_saved_model() unnecessarily
  • Calling convert() directly on model without converter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard