0
0
Computer Visionml~20 mins

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

Choose your learning style9 modes available
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.