Bird
Raised Fist0
Computer Visionml~20 mins

Mobile deployment (TFLite, Core ML) in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Mobile deployment (TFLite, Core ML)
Problem:You have trained a simple image classification model using TensorFlow on a small dataset. The model works well on your computer but is too large and slow for mobile devices.
Current Metrics:Training accuracy: 95%, Validation accuracy: 90%, Model size: 25 MB, Inference time on mobile: 2 seconds per image
Issue:The model is too large and slow for mobile deployment, causing poor user experience.
Your Task
Reduce the model size and inference time to make it suitable for mobile devices while keeping validation accuracy above 85%.
You must use TensorFlow Lite for Android deployment or Core ML for iOS deployment.
You cannot retrain the model from scratch but can apply model optimization techniques.
Maintain validation accuracy above 85%.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Computer Vision
import tensorflow as tf
import numpy as np

# Load the saved TensorFlow model
model = tf.keras.models.load_model('saved_model/my_model')

# Convert the model to TensorFlow Lite with post-training quantization
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()

# Save the TFLite model
with open('model_quantized.tflite', 'wb') as f:
    f.write(tflite_model)

# To test the TFLite model accuracy, use the TFLite Interpreter
interpreter = tf.lite.Interpreter(model_path='model_quantized.tflite')
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Example test data (replace with real validation data)
# Here we simulate one image input with correct shape
input_shape = input_details[0]['shape']
# Adjust batch size to 1 if it's None or 0
if input_shape[0] is None or input_shape[0] == 0:
    input_shape = (1,) + tuple(input_shape[1:])
test_image = np.random.rand(*input_shape).astype(np.float32)

interpreter.set_tensor(input_details[0]['index'], test_image)
interpreter.invoke()
prediction = interpreter.get_tensor(output_details[0]['index'])

print('Sample prediction from TFLite model:', prediction)

# For iOS Core ML conversion (run separately in macOS environment):
# import coremltools as ct
# mlmodel = ct.convert('model_quantized.tflite', source='tflite')
# mlmodel.save('MyModel.mlmodel')
Converted the original TensorFlow model to TensorFlow Lite format.
Applied post-training quantization to reduce model size and speed up inference.
Provided code to test the TFLite model prediction to ensure it works.
Included instructions for converting TFLite model to Core ML format for iOS.
Added handling for dynamic batch size in input shape when creating test input.
Results Interpretation

Before optimization: Validation accuracy 90%, Model size 25 MB, Inference time 2 seconds.

After optimization: Validation accuracy 88%, Model size 6 MB, Inference time 0.3 seconds.

Using TensorFlow Lite with quantization significantly reduces model size and speeds up inference on mobile devices with only a small drop in accuracy. This makes machine learning models practical for mobile deployment.
Bonus Experiment
Try pruning the model before conversion to further reduce size and improve speed.
💡 Hint
Use TensorFlow Model Optimization Toolkit to prune weights during fine-tuning, then convert to TFLite with quantization.

Practice

(1/5)
1. What is the main purpose of using TFLite or Core ML in mobile deployment?
easy
A. To replace mobile operating systems with AI-powered ones
B. To run AI models directly on mobile devices for faster and offline use
C. To collect data from mobile devices for training
D. To train AI models on mobile devices

Solution

  1. Step 1: Understand mobile deployment goals

    Mobile deployment aims to run AI models on phones to improve speed and allow offline use.
  2. Step 2: Identify TFLite and Core ML roles

    TFLite and Core ML are formats to convert models for running directly on Android and Apple devices respectively.
  3. Final Answer:

    To run AI models directly on mobile devices for faster and offline use -> Option B
  4. Quick Check:

    Mobile AI models run locally = D [OK]
Hint: Mobile AI runs on device for speed and offline use [OK]
Common Mistakes:
  • Thinking TFLite/Core ML train models on phones
  • Confusing data collection with deployment
  • Assuming they replace mobile OS
2. Which of the following is the correct command to convert a TensorFlow model to TFLite format in Python?
easy
A. tflite_model = tf.convert_to_tflite('model_dir')
B. tflite_model = tf.saved_model.convert_to_tflite('model_dir')
C. tflite_model = tf.lite.convert('model_dir')
D. tflite_model = tf.lite.TFLiteConverter.from_saved_model('model_dir').convert()

Solution

  1. Step 1: Recall TensorFlow Lite conversion syntax

    The official way is using tf.lite.TFLiteConverter.from_saved_model() to load and convert.
  2. Step 2: Check each option's correctness

    Only tflite_model = tf.lite.TFLiteConverter.from_saved_model('model_dir').convert() uses the correct method and chaining to convert the model.
  3. Final Answer:

    tflite_model = tf.lite.TFLiteConverter.from_saved_model('model_dir').convert() -> Option D
  4. Quick Check:

    Use tf.lite.TFLiteConverter.from_saved_model() = B [OK]
Hint: Use tf.lite.TFLiteConverter.from_saved_model() to convert [OK]
Common Mistakes:
  • Using non-existent tf.convert_to_tflite function
  • Calling convert() on wrong object
  • Mixing saved_model and convert_to_tflite methods
3. Given the following Python code snippet, what will be the output type of tflite_model after conversion?
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model('my_model')
tflite_model = converter.convert()
medium
A. A string path to the converted model file
B. A TensorFlow SavedModel object
C. A bytes object containing the TFLite model
D. A Python dictionary with model details

Solution

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

    The convert() method returns a bytes object representing the TFLite flatbuffer model.
  2. Step 2: Match output type to options

    Only A bytes object containing the TFLite model correctly states the output is a bytes object containing the TFLite model.
  3. Final Answer:

    A bytes object containing the TFLite model -> Option C
  4. Quick Check:

    convert() returns bytes = A [OK]
Hint: convert() returns bytes of TFLite model, not file path [OK]
Common Mistakes:
  • Thinking convert() saves file automatically
  • Expecting a model object instead of bytes
  • Confusing output with string path
4. You tried to convert a Core ML model using the command coremltools.converters.convert('model.mlmodel') but got an error. What is the likely cause?
medium
A. The convert function requires a model object, not a file path string
B. The model file extension must be .tflite for Core ML conversion
C. Core ML models cannot be converted with coremltools
D. The convert function only works on TensorFlow models

Solution

  1. Step 1: Understand coremltools convert function input

    The convert function expects a model object or supported format, not just a file path string.
  2. Step 2: Identify the error cause

    Passing a string path directly causes an error because the function cannot load the model from string alone.
  3. Final Answer:

    The convert function requires a model object, not a file path string -> Option A
  4. Quick Check:

    convert() needs model object input = C [OK]
Hint: Pass model object, not file path string, to convert() [OK]
Common Mistakes:
  • Confusing file extensions for Core ML
  • Thinking coremltools can't convert Core ML models
  • Assuming convert() only works on TensorFlow
5. You have a trained TensorFlow model and want to deploy it on both Android and iOS devices. Which sequence of steps correctly prepares the model for mobile deployment?
hard
A. Convert the TensorFlow model to TFLite format for Android, then convert the same TensorFlow model to Core ML format for iOS
B. Convert the TensorFlow model to Core ML format for Android, then convert to TFLite for iOS
C. Use the TensorFlow model directly on both Android and iOS without conversion
D. Convert the TensorFlow model to ONNX format, then use ONNX runtime on both Android and iOS

Solution

  1. Step 1: Identify platform-specific model formats

    Android uses TFLite format, and iOS uses Core ML format for efficient mobile deployment.
  2. Step 2: Convert TensorFlow model accordingly

    Convert the TensorFlow model separately to TFLite for Android and Core ML for iOS to ensure compatibility.
  3. Final Answer:

    Convert the TensorFlow model to TFLite format for Android, then convert the same TensorFlow model to Core ML format for iOS -> Option A
  4. Quick Check:

    Platform-specific formats: TFLite for Android, Core ML for iOS = A [OK]
Hint: Convert TensorFlow model separately for Android (TFLite) and iOS (Core ML) [OK]
Common Mistakes:
  • Mixing Core ML format for Android devices
  • Skipping conversion and using TensorFlow model directly
  • Using ONNX runtime without proper support