Chapter 8: Machine Learning in the Cloud and Edge Computing
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:
import tensorflow as tf
# 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:
f.write(tflite_model)
print("Model successfully converted and quantized to TensorFlow Lite format.")
In this exercise:
- We load a TensorFlow model and convert it into the TensorFlow Lite format.
- Post-training quantization is applied to optimize the model, reducing its size and improving inference speed on edge devices.
Exercise 2: Run a TensorFlow Lite Model on Android
Task: Integrate a TensorFlow Lite model into an Android app and perform inference. Assume you have a TFLite model ready (model.tflite
), and the goal is to run inference on an Android device.
Solution:
import org.tensorflow.lite.Interpreter;
import android.content.res.AssetManager;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class TFLiteModel {
private Interpreter tflite;
// Constructor: Load the TensorFlow Lite model from assets
public TFLiteModel(AssetManager assetManager, String modelPath) throws IOException {
ByteBuffer modelBuffer = loadModelFile(assetManager, modelPath);
tflite = new Interpreter(modelBuffer);
}
// Load model from assets into ByteBuffer
private ByteBuffer loadModelFile(AssetManager assetManager, String modelPath) throws IOException {
try (InputStream inputStream = assetManager.open(modelPath)) {
byte[] modelBytes = new byte[inputStream.available()];
inputStream.read(modelBytes);
ByteBuffer buffer = ByteBuffer.allocateDirect(modelBytes.length)
.order(ByteOrder.nativeOrder());
buffer.put(modelBytes);
buffer.rewind();
return buffer.asReadOnlyBuffer(); // Make buffer read-only
}
}
// Perform inference with input data
public float[] runInference(float[] inputData) {
float[][] outputData = new float[1][10]; // Assuming 10 output classes
ByteBuffer inputBuffer = ByteBuffer.allocateDirect(inputData.length * 4)
.order(ByteOrder.nativeOrder());
for (float value : inputData) {
inputBuffer.putFloat(value);
}
inputBuffer.rewind();
tflite.run(inputBuffer, outputData);
return outputData[0]; // Return results
}
}
In this exercise:
- We load the TensorFlow Lite model using the TFLite Interpreter in an Android app.
- The model is used to perform inference on input data, such as image features, returning predictions.
Exercise 3: Deploy a Model Using ONNX Runtime
Task: Convert a PyTorch model to ONNX format and run inference using ONNX Runtime on a resource-constrained device like a Raspberry Pi.
Solution:
import torch
import torch.nn as nn
import onnx
import onnxruntime as ort
# Define a simple PyTorch model
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc = nn.Linear(784, 10)
def forward(self, x):
return self.fc(x)
# Create an instance of the model
model = SimpleModel()
# Create a dummy input for model tracing
dummy_input = torch.randn(1, 784)
# Export the model to ONNX format
torch.onnx.export(
model,
dummy_input,
"model.onnx",
opset_version=11, # Specify opset version for compatibility
verbose=True, # Enable verbosity for debugging
input_names=["input"], # Define input name
output_names=["output"] # Define output name
)
print("Model successfully converted to ONNX format.")
# Run inference using ONNX Runtime
ort_session = ort.InferenceSession("model.onnx")
def to_numpy(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
# Run inference
input_data = to_numpy(dummy_input)
input_name = ort_session.get_inputs()[0].name # Dynamically obtain input name
outputs = ort_session.run(None, {input_name: input_data})
print("ONNX Model Inference Output:", outputs)
In this exercise:
- We define a simple PyTorch model and convert it to ONNX format using
torch.onnx.export()
. - The ONNX Runtime is used to run inference on the converted model, making it suitable for edge devices like the Raspberry Pi.
Exercise 4: Deploy a TensorFlow Lite Model on Raspberry Pi
Task: Deploy a TensorFlow Lite model on a Raspberry Pi and run inference on a sample dataset.
Solution:
import numpy as np
import tensorflow as tf
# Load the TensorFlow Lite model
interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()
# Get input and output tensor details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Prepare a sample input (assuming the model expects a 1D array)
input_data = np.array([[1.0, 2.0, 3.0, 4.0]], dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
# Run inference
interpreter.invoke()
# Retrieve the output from the model
output_data = interpreter.get_tensor(output_details[0]['index'])
print(f"Model output: {output_data}")
In this exercise:
- We deploy a TensorFlow Lite model on a Raspberry Pi using the TFLite Interpreter.
- The model performs inference on input data, making predictions based on the trained model.
Exercise 5: Convert a TensorFlow Lite Model to Core ML
Task: Convert a TensorFlow Lite model to Core ML format for deployment on an iOS device.
Solution:
import coremltools
import tensorflow as tf
# Load the TensorFlow Lite model
model = tf.keras.models.load_model('my_model.h5')
# Convert the model to Core ML format
coreml_model = coremltools.convert(model)
# Save the Core ML model
coreml_model.save('MyCoreMLModel.mlmodel')
print("Model successfully converted to Core ML format.")
In this exercise:
- We convert a TensorFlow Lite model to Core ML format using the coremltools library.
- The model can now be deployed in an iOS application using Core ML.
These practical exercises covered key steps for deploying machine learning models to mobile and edge devices using frameworks like TensorFlow Lite, ONNX, and Core ML. By completing these exercises, you’ll gain hands-on experience in optimizing and running models on resource-constrained devices, making your machine learning applications more versatile and efficient.
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:
import tensorflow as tf
# 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:
f.write(tflite_model)
print("Model successfully converted and quantized to TensorFlow Lite format.")
In this exercise:
- We load a TensorFlow model and convert it into the TensorFlow Lite format.
- Post-training quantization is applied to optimize the model, reducing its size and improving inference speed on edge devices.
Exercise 2: Run a TensorFlow Lite Model on Android
Task: Integrate a TensorFlow Lite model into an Android app and perform inference. Assume you have a TFLite model ready (model.tflite
), and the goal is to run inference on an Android device.
Solution:
import org.tensorflow.lite.Interpreter;
import android.content.res.AssetManager;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class TFLiteModel {
private Interpreter tflite;
// Constructor: Load the TensorFlow Lite model from assets
public TFLiteModel(AssetManager assetManager, String modelPath) throws IOException {
ByteBuffer modelBuffer = loadModelFile(assetManager, modelPath);
tflite = new Interpreter(modelBuffer);
}
// Load model from assets into ByteBuffer
private ByteBuffer loadModelFile(AssetManager assetManager, String modelPath) throws IOException {
try (InputStream inputStream = assetManager.open(modelPath)) {
byte[] modelBytes = new byte[inputStream.available()];
inputStream.read(modelBytes);
ByteBuffer buffer = ByteBuffer.allocateDirect(modelBytes.length)
.order(ByteOrder.nativeOrder());
buffer.put(modelBytes);
buffer.rewind();
return buffer.asReadOnlyBuffer(); // Make buffer read-only
}
}
// Perform inference with input data
public float[] runInference(float[] inputData) {
float[][] outputData = new float[1][10]; // Assuming 10 output classes
ByteBuffer inputBuffer = ByteBuffer.allocateDirect(inputData.length * 4)
.order(ByteOrder.nativeOrder());
for (float value : inputData) {
inputBuffer.putFloat(value);
}
inputBuffer.rewind();
tflite.run(inputBuffer, outputData);
return outputData[0]; // Return results
}
}
In this exercise:
- We load the TensorFlow Lite model using the TFLite Interpreter in an Android app.
- The model is used to perform inference on input data, such as image features, returning predictions.
Exercise 3: Deploy a Model Using ONNX Runtime
Task: Convert a PyTorch model to ONNX format and run inference using ONNX Runtime on a resource-constrained device like a Raspberry Pi.
Solution:
import torch
import torch.nn as nn
import onnx
import onnxruntime as ort
# Define a simple PyTorch model
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc = nn.Linear(784, 10)
def forward(self, x):
return self.fc(x)
# Create an instance of the model
model = SimpleModel()
# Create a dummy input for model tracing
dummy_input = torch.randn(1, 784)
# Export the model to ONNX format
torch.onnx.export(
model,
dummy_input,
"model.onnx",
opset_version=11, # Specify opset version for compatibility
verbose=True, # Enable verbosity for debugging
input_names=["input"], # Define input name
output_names=["output"] # Define output name
)
print("Model successfully converted to ONNX format.")
# Run inference using ONNX Runtime
ort_session = ort.InferenceSession("model.onnx")
def to_numpy(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
# Run inference
input_data = to_numpy(dummy_input)
input_name = ort_session.get_inputs()[0].name # Dynamically obtain input name
outputs = ort_session.run(None, {input_name: input_data})
print("ONNX Model Inference Output:", outputs)
In this exercise:
- We define a simple PyTorch model and convert it to ONNX format using
torch.onnx.export()
. - The ONNX Runtime is used to run inference on the converted model, making it suitable for edge devices like the Raspberry Pi.
Exercise 4: Deploy a TensorFlow Lite Model on Raspberry Pi
Task: Deploy a TensorFlow Lite model on a Raspberry Pi and run inference on a sample dataset.
Solution:
import numpy as np
import tensorflow as tf
# Load the TensorFlow Lite model
interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()
# Get input and output tensor details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Prepare a sample input (assuming the model expects a 1D array)
input_data = np.array([[1.0, 2.0, 3.0, 4.0]], dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
# Run inference
interpreter.invoke()
# Retrieve the output from the model
output_data = interpreter.get_tensor(output_details[0]['index'])
print(f"Model output: {output_data}")
In this exercise:
- We deploy a TensorFlow Lite model on a Raspberry Pi using the TFLite Interpreter.
- The model performs inference on input data, making predictions based on the trained model.
Exercise 5: Convert a TensorFlow Lite Model to Core ML
Task: Convert a TensorFlow Lite model to Core ML format for deployment on an iOS device.
Solution:
import coremltools
import tensorflow as tf
# Load the TensorFlow Lite model
model = tf.keras.models.load_model('my_model.h5')
# Convert the model to Core ML format
coreml_model = coremltools.convert(model)
# Save the Core ML model
coreml_model.save('MyCoreMLModel.mlmodel')
print("Model successfully converted to Core ML format.")
In this exercise:
- We convert a TensorFlow Lite model to Core ML format using the coremltools library.
- The model can now be deployed in an iOS application using Core ML.
These practical exercises covered key steps for deploying machine learning models to mobile and edge devices using frameworks like TensorFlow Lite, ONNX, and Core ML. By completing these exercises, you’ll gain hands-on experience in optimizing and running models on resource-constrained devices, making your machine learning applications more versatile and efficient.
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:
import tensorflow as tf
# 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:
f.write(tflite_model)
print("Model successfully converted and quantized to TensorFlow Lite format.")
In this exercise:
- We load a TensorFlow model and convert it into the TensorFlow Lite format.
- Post-training quantization is applied to optimize the model, reducing its size and improving inference speed on edge devices.
Exercise 2: Run a TensorFlow Lite Model on Android
Task: Integrate a TensorFlow Lite model into an Android app and perform inference. Assume you have a TFLite model ready (model.tflite
), and the goal is to run inference on an Android device.
Solution:
import org.tensorflow.lite.Interpreter;
import android.content.res.AssetManager;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class TFLiteModel {
private Interpreter tflite;
// Constructor: Load the TensorFlow Lite model from assets
public TFLiteModel(AssetManager assetManager, String modelPath) throws IOException {
ByteBuffer modelBuffer = loadModelFile(assetManager, modelPath);
tflite = new Interpreter(modelBuffer);
}
// Load model from assets into ByteBuffer
private ByteBuffer loadModelFile(AssetManager assetManager, String modelPath) throws IOException {
try (InputStream inputStream = assetManager.open(modelPath)) {
byte[] modelBytes = new byte[inputStream.available()];
inputStream.read(modelBytes);
ByteBuffer buffer = ByteBuffer.allocateDirect(modelBytes.length)
.order(ByteOrder.nativeOrder());
buffer.put(modelBytes);
buffer.rewind();
return buffer.asReadOnlyBuffer(); // Make buffer read-only
}
}
// Perform inference with input data
public float[] runInference(float[] inputData) {
float[][] outputData = new float[1][10]; // Assuming 10 output classes
ByteBuffer inputBuffer = ByteBuffer.allocateDirect(inputData.length * 4)
.order(ByteOrder.nativeOrder());
for (float value : inputData) {
inputBuffer.putFloat(value);
}
inputBuffer.rewind();
tflite.run(inputBuffer, outputData);
return outputData[0]; // Return results
}
}
In this exercise:
- We load the TensorFlow Lite model using the TFLite Interpreter in an Android app.
- The model is used to perform inference on input data, such as image features, returning predictions.
Exercise 3: Deploy a Model Using ONNX Runtime
Task: Convert a PyTorch model to ONNX format and run inference using ONNX Runtime on a resource-constrained device like a Raspberry Pi.
Solution:
import torch
import torch.nn as nn
import onnx
import onnxruntime as ort
# Define a simple PyTorch model
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc = nn.Linear(784, 10)
def forward(self, x):
return self.fc(x)
# Create an instance of the model
model = SimpleModel()
# Create a dummy input for model tracing
dummy_input = torch.randn(1, 784)
# Export the model to ONNX format
torch.onnx.export(
model,
dummy_input,
"model.onnx",
opset_version=11, # Specify opset version for compatibility
verbose=True, # Enable verbosity for debugging
input_names=["input"], # Define input name
output_names=["output"] # Define output name
)
print("Model successfully converted to ONNX format.")
# Run inference using ONNX Runtime
ort_session = ort.InferenceSession("model.onnx")
def to_numpy(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
# Run inference
input_data = to_numpy(dummy_input)
input_name = ort_session.get_inputs()[0].name # Dynamically obtain input name
outputs = ort_session.run(None, {input_name: input_data})
print("ONNX Model Inference Output:", outputs)
In this exercise:
- We define a simple PyTorch model and convert it to ONNX format using
torch.onnx.export()
. - The ONNX Runtime is used to run inference on the converted model, making it suitable for edge devices like the Raspberry Pi.
Exercise 4: Deploy a TensorFlow Lite Model on Raspberry Pi
Task: Deploy a TensorFlow Lite model on a Raspberry Pi and run inference on a sample dataset.
Solution:
import numpy as np
import tensorflow as tf
# Load the TensorFlow Lite model
interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()
# Get input and output tensor details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Prepare a sample input (assuming the model expects a 1D array)
input_data = np.array([[1.0, 2.0, 3.0, 4.0]], dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
# Run inference
interpreter.invoke()
# Retrieve the output from the model
output_data = interpreter.get_tensor(output_details[0]['index'])
print(f"Model output: {output_data}")
In this exercise:
- We deploy a TensorFlow Lite model on a Raspberry Pi using the TFLite Interpreter.
- The model performs inference on input data, making predictions based on the trained model.
Exercise 5: Convert a TensorFlow Lite Model to Core ML
Task: Convert a TensorFlow Lite model to Core ML format for deployment on an iOS device.
Solution:
import coremltools
import tensorflow as tf
# Load the TensorFlow Lite model
model = tf.keras.models.load_model('my_model.h5')
# Convert the model to Core ML format
coreml_model = coremltools.convert(model)
# Save the Core ML model
coreml_model.save('MyCoreMLModel.mlmodel')
print("Model successfully converted to Core ML format.")
In this exercise:
- We convert a TensorFlow Lite model to Core ML format using the coremltools library.
- The model can now be deployed in an iOS application using Core ML.
These practical exercises covered key steps for deploying machine learning models to mobile and edge devices using frameworks like TensorFlow Lite, ONNX, and Core ML. By completing these exercises, you’ll gain hands-on experience in optimizing and running models on resource-constrained devices, making your machine learning applications more versatile and efficient.
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:
import tensorflow as tf
# 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:
f.write(tflite_model)
print("Model successfully converted and quantized to TensorFlow Lite format.")
In this exercise:
- We load a TensorFlow model and convert it into the TensorFlow Lite format.
- Post-training quantization is applied to optimize the model, reducing its size and improving inference speed on edge devices.
Exercise 2: Run a TensorFlow Lite Model on Android
Task: Integrate a TensorFlow Lite model into an Android app and perform inference. Assume you have a TFLite model ready (model.tflite
), and the goal is to run inference on an Android device.
Solution:
import org.tensorflow.lite.Interpreter;
import android.content.res.AssetManager;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class TFLiteModel {
private Interpreter tflite;
// Constructor: Load the TensorFlow Lite model from assets
public TFLiteModel(AssetManager assetManager, String modelPath) throws IOException {
ByteBuffer modelBuffer = loadModelFile(assetManager, modelPath);
tflite = new Interpreter(modelBuffer);
}
// Load model from assets into ByteBuffer
private ByteBuffer loadModelFile(AssetManager assetManager, String modelPath) throws IOException {
try (InputStream inputStream = assetManager.open(modelPath)) {
byte[] modelBytes = new byte[inputStream.available()];
inputStream.read(modelBytes);
ByteBuffer buffer = ByteBuffer.allocateDirect(modelBytes.length)
.order(ByteOrder.nativeOrder());
buffer.put(modelBytes);
buffer.rewind();
return buffer.asReadOnlyBuffer(); // Make buffer read-only
}
}
// Perform inference with input data
public float[] runInference(float[] inputData) {
float[][] outputData = new float[1][10]; // Assuming 10 output classes
ByteBuffer inputBuffer = ByteBuffer.allocateDirect(inputData.length * 4)
.order(ByteOrder.nativeOrder());
for (float value : inputData) {
inputBuffer.putFloat(value);
}
inputBuffer.rewind();
tflite.run(inputBuffer, outputData);
return outputData[0]; // Return results
}
}
In this exercise:
- We load the TensorFlow Lite model using the TFLite Interpreter in an Android app.
- The model is used to perform inference on input data, such as image features, returning predictions.
Exercise 3: Deploy a Model Using ONNX Runtime
Task: Convert a PyTorch model to ONNX format and run inference using ONNX Runtime on a resource-constrained device like a Raspberry Pi.
Solution:
import torch
import torch.nn as nn
import onnx
import onnxruntime as ort
# Define a simple PyTorch model
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc = nn.Linear(784, 10)
def forward(self, x):
return self.fc(x)
# Create an instance of the model
model = SimpleModel()
# Create a dummy input for model tracing
dummy_input = torch.randn(1, 784)
# Export the model to ONNX format
torch.onnx.export(
model,
dummy_input,
"model.onnx",
opset_version=11, # Specify opset version for compatibility
verbose=True, # Enable verbosity for debugging
input_names=["input"], # Define input name
output_names=["output"] # Define output name
)
print("Model successfully converted to ONNX format.")
# Run inference using ONNX Runtime
ort_session = ort.InferenceSession("model.onnx")
def to_numpy(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
# Run inference
input_data = to_numpy(dummy_input)
input_name = ort_session.get_inputs()[0].name # Dynamically obtain input name
outputs = ort_session.run(None, {input_name: input_data})
print("ONNX Model Inference Output:", outputs)
In this exercise:
- We define a simple PyTorch model and convert it to ONNX format using
torch.onnx.export()
. - The ONNX Runtime is used to run inference on the converted model, making it suitable for edge devices like the Raspberry Pi.
Exercise 4: Deploy a TensorFlow Lite Model on Raspberry Pi
Task: Deploy a TensorFlow Lite model on a Raspberry Pi and run inference on a sample dataset.
Solution:
import numpy as np
import tensorflow as tf
# Load the TensorFlow Lite model
interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()
# Get input and output tensor details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Prepare a sample input (assuming the model expects a 1D array)
input_data = np.array([[1.0, 2.0, 3.0, 4.0]], dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
# Run inference
interpreter.invoke()
# Retrieve the output from the model
output_data = interpreter.get_tensor(output_details[0]['index'])
print(f"Model output: {output_data}")
In this exercise:
- We deploy a TensorFlow Lite model on a Raspberry Pi using the TFLite Interpreter.
- The model performs inference on input data, making predictions based on the trained model.
Exercise 5: Convert a TensorFlow Lite Model to Core ML
Task: Convert a TensorFlow Lite model to Core ML format for deployment on an iOS device.
Solution:
import coremltools
import tensorflow as tf
# Load the TensorFlow Lite model
model = tf.keras.models.load_model('my_model.h5')
# Convert the model to Core ML format
coreml_model = coremltools.convert(model)
# Save the Core ML model
coreml_model.save('MyCoreMLModel.mlmodel')
print("Model successfully converted to Core ML format.")
In this exercise:
- We convert a TensorFlow Lite model to Core ML format using the coremltools library.
- The model can now be deployed in an iOS application using Core ML.
These practical exercises covered key steps for deploying machine learning models to mobile and edge devices using frameworks like TensorFlow Lite, ONNX, and Core ML. By completing these exercises, you’ll gain hands-on experience in optimizing and running models on resource-constrained devices, making your machine learning applications more versatile and efficient.