Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconDeep Learning and AI Superhero
Deep Learning and AI Superhero

Chapter 3: Deep Learning with Keras

Practical Exercises Chapter 3

Exercise 1: Saving and Loading a Keras Model

Task: Train a simple neural network on the MNIST dataset, save the model using the SavedModel format, and then load the saved model to make predictions on the test set.

Solution:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import mnist

# Load the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Normalize the data
X_train, X_test = X_train / 255.0, X_test / 255.0

# Define a simple Sequential model
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(X_train, y_train, epochs=5)

# Save the model in SavedModel format
model.save('saved_model')

# Load the saved model
loaded_model = tf.keras.models.load_model('saved_model')

# Use the loaded model to make predictions
predictions = loaded_model.predict(X_test)
print(f"Predictions for the first test sample: {predictions[0]}")

Exercise 2: Deploying a Keras Model with TensorFlow Serving

Task: Train a neural network, save it in the SavedModel format, and prepare the model for deployment using TensorFlow Serving. Write a client-side script that sends a request to the model’s API for predictions.

Solution:

Step 1: Save the model for TensorFlow Serving.

# Save the model in SavedModel format for TensorFlow Serving
model.save('serving_model/keras_served_model')

Step 2: Run TensorFlow Serving using Docker.

docker pull tensorflow/serving
docker run -p 8501:8501 --name tf_serving \\
  --mount type=bind,source=$(pwd)/serving_model/keras_served_model,target=/models/keras_served_model \\
  -e MODEL_NAME=keras_served_model -t tensorflow/serving

Step 3: Send a request to TensorFlow Serving.

import requests
import json
import numpy as np

# Prepare input data
input_data = np.expand_dims(X_test[0], axis=0).tolist()

# Define the API URL for TensorFlow Serving
url = '<http://localhost:8501/v1/models/keras_served_model:predict>'

# Send a POST request to TensorFlow Serving
response = requests.post(url, json={"instances": input_data})

# Parse the response
predictions = response.json()['predictions']
print(f"Predictions: {predictions}")

Exercise 3: Deploying a Keras Model with Flask

Task: Create a Flask web app that loads a trained Keras model and serves it via an API for making predictions. Write a client script to send a request to the Flask app.

Solution:

Step 1: Create the Flask app.

from flask import Flask, request, jsonify
from tensorflow.keras.models import load_model
import numpy as np

# Initialize the Flask app
app = Flask(__name__)

# Load the trained Keras model
model = load_model('saved_model')

# Define an API route for predictions
@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json(force=True)
    input_data = np.array(data['instances'])
    predictions = model.predict(input_data)
    return jsonify(predictions=predictions.tolist())

# Run the Flask app
if __name__ == '__main__':
    app.run(port=5000, debug=True)

Step 2: Client-side script to send a request to the Flask app.

import requests
import json
import numpy as np

# Prepare input data
input_data = np.expand_dims(X_test[0], axis=0).tolist()

# Define the Flask API URL
url = '<http://localhost:5000/predict>'

# Send a POST request to the Flask API
response = requests.post(url, json={"instances": input_data})

# Parse the response
predictions = response.json()['predictions']
print(f"Predictions: {predictions}")

Exercise 4: Converting a Keras Model to TensorFlow Lite

Task: Convert a trained Keras model to TensorFlow Lite format and save it. Use the TensorFlow Lite interpreter to run inference on the test dataset.

Solution:

Step 1: Convert the model to TensorFlow Lite.

# Convert the Keras model to TensorFlow Lite format
converter = tf.lite.TFLiteConverter.from_saved_model('saved_model')
tflite_model = converter.convert()

# Save the TensorFlow Lite model to a file
with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

Step 2: Load and run the model with TensorFlow Lite.

import tensorflow as tf
import numpy as np

# Load the TensorFlow Lite model
interpreter = tf.lite.Interpreter(model_path='model.tflite')
interpreter.allocate_tensors()

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

# Prepare input data (e.g., first test sample)
test_sample = np.expand_dims(X_test[0], axis=0).astype(np.float32)

# Set the input tensor
interpreter.set_tensor(input_details[0]['index'], test_sample)

# Run the model
interpreter.invoke()

# Get the output tensor (predictions)
output_data = interpreter.get_tensor(output_details[0]['index'])
print(f"TensorFlow Lite model predictions: {output_data}")

Exercise 5: Using Model Checkpointing and Early Stopping

Task: Train a Keras model with model checkpointing and early stopping to save the best-performing model and stop training when the performance stops improving.

Solution:

from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping

# Define the ModelCheckpoint and EarlyStopping callbacks
checkpoint_callback = ModelCheckpoint(filepath='best_model.h5', save_best_only=True, monitor='val_accuracy', mode='max', verbose=1)
early_stopping_callback = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True, verbose=1)

# Train the model with both callbacks
model.fit(X_train, y_train, epochs=20, validation_data=(X_test, y_test), callbacks=[checkpoint_callback, early_stopping_callback])

In this solution:

  • ModelCheckpoint saves the best model based on validation accuracy.
  • EarlyStopping halts training when validation loss stops improving for 3 consecutive epochs.

These exercises reinforce the key concepts from Chapter 3, including saving and loading Keras models, deploying them in production environments using TensorFlow Serving and Flask, converting models to TensorFlow Lite for mobile applications, and using model checkpointing and early stopping for efficient training. These tasks are essential for transitioning models from research to real-world use.

Practical Exercises Chapter 3

Exercise 1: Saving and Loading a Keras Model

Task: Train a simple neural network on the MNIST dataset, save the model using the SavedModel format, and then load the saved model to make predictions on the test set.

Solution:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import mnist

# Load the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Normalize the data
X_train, X_test = X_train / 255.0, X_test / 255.0

# Define a simple Sequential model
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(X_train, y_train, epochs=5)

# Save the model in SavedModel format
model.save('saved_model')

# Load the saved model
loaded_model = tf.keras.models.load_model('saved_model')

# Use the loaded model to make predictions
predictions = loaded_model.predict(X_test)
print(f"Predictions for the first test sample: {predictions[0]}")

Exercise 2: Deploying a Keras Model with TensorFlow Serving

Task: Train a neural network, save it in the SavedModel format, and prepare the model for deployment using TensorFlow Serving. Write a client-side script that sends a request to the model’s API for predictions.

Solution:

Step 1: Save the model for TensorFlow Serving.

# Save the model in SavedModel format for TensorFlow Serving
model.save('serving_model/keras_served_model')

Step 2: Run TensorFlow Serving using Docker.

docker pull tensorflow/serving
docker run -p 8501:8501 --name tf_serving \\
  --mount type=bind,source=$(pwd)/serving_model/keras_served_model,target=/models/keras_served_model \\
  -e MODEL_NAME=keras_served_model -t tensorflow/serving

Step 3: Send a request to TensorFlow Serving.

import requests
import json
import numpy as np

# Prepare input data
input_data = np.expand_dims(X_test[0], axis=0).tolist()

# Define the API URL for TensorFlow Serving
url = '<http://localhost:8501/v1/models/keras_served_model:predict>'

# Send a POST request to TensorFlow Serving
response = requests.post(url, json={"instances": input_data})

# Parse the response
predictions = response.json()['predictions']
print(f"Predictions: {predictions}")

Exercise 3: Deploying a Keras Model with Flask

Task: Create a Flask web app that loads a trained Keras model and serves it via an API for making predictions. Write a client script to send a request to the Flask app.

Solution:

Step 1: Create the Flask app.

from flask import Flask, request, jsonify
from tensorflow.keras.models import load_model
import numpy as np

# Initialize the Flask app
app = Flask(__name__)

# Load the trained Keras model
model = load_model('saved_model')

# Define an API route for predictions
@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json(force=True)
    input_data = np.array(data['instances'])
    predictions = model.predict(input_data)
    return jsonify(predictions=predictions.tolist())

# Run the Flask app
if __name__ == '__main__':
    app.run(port=5000, debug=True)

Step 2: Client-side script to send a request to the Flask app.

import requests
import json
import numpy as np

# Prepare input data
input_data = np.expand_dims(X_test[0], axis=0).tolist()

# Define the Flask API URL
url = '<http://localhost:5000/predict>'

# Send a POST request to the Flask API
response = requests.post(url, json={"instances": input_data})

# Parse the response
predictions = response.json()['predictions']
print(f"Predictions: {predictions}")

Exercise 4: Converting a Keras Model to TensorFlow Lite

Task: Convert a trained Keras model to TensorFlow Lite format and save it. Use the TensorFlow Lite interpreter to run inference on the test dataset.

Solution:

Step 1: Convert the model to TensorFlow Lite.

# Convert the Keras model to TensorFlow Lite format
converter = tf.lite.TFLiteConverter.from_saved_model('saved_model')
tflite_model = converter.convert()

# Save the TensorFlow Lite model to a file
with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

Step 2: Load and run the model with TensorFlow Lite.

import tensorflow as tf
import numpy as np

# Load the TensorFlow Lite model
interpreter = tf.lite.Interpreter(model_path='model.tflite')
interpreter.allocate_tensors()

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

# Prepare input data (e.g., first test sample)
test_sample = np.expand_dims(X_test[0], axis=0).astype(np.float32)

# Set the input tensor
interpreter.set_tensor(input_details[0]['index'], test_sample)

# Run the model
interpreter.invoke()

# Get the output tensor (predictions)
output_data = interpreter.get_tensor(output_details[0]['index'])
print(f"TensorFlow Lite model predictions: {output_data}")

Exercise 5: Using Model Checkpointing and Early Stopping

Task: Train a Keras model with model checkpointing and early stopping to save the best-performing model and stop training when the performance stops improving.

Solution:

from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping

# Define the ModelCheckpoint and EarlyStopping callbacks
checkpoint_callback = ModelCheckpoint(filepath='best_model.h5', save_best_only=True, monitor='val_accuracy', mode='max', verbose=1)
early_stopping_callback = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True, verbose=1)

# Train the model with both callbacks
model.fit(X_train, y_train, epochs=20, validation_data=(X_test, y_test), callbacks=[checkpoint_callback, early_stopping_callback])

In this solution:

  • ModelCheckpoint saves the best model based on validation accuracy.
  • EarlyStopping halts training when validation loss stops improving for 3 consecutive epochs.

These exercises reinforce the key concepts from Chapter 3, including saving and loading Keras models, deploying them in production environments using TensorFlow Serving and Flask, converting models to TensorFlow Lite for mobile applications, and using model checkpointing and early stopping for efficient training. These tasks are essential for transitioning models from research to real-world use.

Practical Exercises Chapter 3

Exercise 1: Saving and Loading a Keras Model

Task: Train a simple neural network on the MNIST dataset, save the model using the SavedModel format, and then load the saved model to make predictions on the test set.

Solution:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import mnist

# Load the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Normalize the data
X_train, X_test = X_train / 255.0, X_test / 255.0

# Define a simple Sequential model
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(X_train, y_train, epochs=5)

# Save the model in SavedModel format
model.save('saved_model')

# Load the saved model
loaded_model = tf.keras.models.load_model('saved_model')

# Use the loaded model to make predictions
predictions = loaded_model.predict(X_test)
print(f"Predictions for the first test sample: {predictions[0]}")

Exercise 2: Deploying a Keras Model with TensorFlow Serving

Task: Train a neural network, save it in the SavedModel format, and prepare the model for deployment using TensorFlow Serving. Write a client-side script that sends a request to the model’s API for predictions.

Solution:

Step 1: Save the model for TensorFlow Serving.

# Save the model in SavedModel format for TensorFlow Serving
model.save('serving_model/keras_served_model')

Step 2: Run TensorFlow Serving using Docker.

docker pull tensorflow/serving
docker run -p 8501:8501 --name tf_serving \\
  --mount type=bind,source=$(pwd)/serving_model/keras_served_model,target=/models/keras_served_model \\
  -e MODEL_NAME=keras_served_model -t tensorflow/serving

Step 3: Send a request to TensorFlow Serving.

import requests
import json
import numpy as np

# Prepare input data
input_data = np.expand_dims(X_test[0], axis=0).tolist()

# Define the API URL for TensorFlow Serving
url = '<http://localhost:8501/v1/models/keras_served_model:predict>'

# Send a POST request to TensorFlow Serving
response = requests.post(url, json={"instances": input_data})

# Parse the response
predictions = response.json()['predictions']
print(f"Predictions: {predictions}")

Exercise 3: Deploying a Keras Model with Flask

Task: Create a Flask web app that loads a trained Keras model and serves it via an API for making predictions. Write a client script to send a request to the Flask app.

Solution:

Step 1: Create the Flask app.

from flask import Flask, request, jsonify
from tensorflow.keras.models import load_model
import numpy as np

# Initialize the Flask app
app = Flask(__name__)

# Load the trained Keras model
model = load_model('saved_model')

# Define an API route for predictions
@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json(force=True)
    input_data = np.array(data['instances'])
    predictions = model.predict(input_data)
    return jsonify(predictions=predictions.tolist())

# Run the Flask app
if __name__ == '__main__':
    app.run(port=5000, debug=True)

Step 2: Client-side script to send a request to the Flask app.

import requests
import json
import numpy as np

# Prepare input data
input_data = np.expand_dims(X_test[0], axis=0).tolist()

# Define the Flask API URL
url = '<http://localhost:5000/predict>'

# Send a POST request to the Flask API
response = requests.post(url, json={"instances": input_data})

# Parse the response
predictions = response.json()['predictions']
print(f"Predictions: {predictions}")

Exercise 4: Converting a Keras Model to TensorFlow Lite

Task: Convert a trained Keras model to TensorFlow Lite format and save it. Use the TensorFlow Lite interpreter to run inference on the test dataset.

Solution:

Step 1: Convert the model to TensorFlow Lite.

# Convert the Keras model to TensorFlow Lite format
converter = tf.lite.TFLiteConverter.from_saved_model('saved_model')
tflite_model = converter.convert()

# Save the TensorFlow Lite model to a file
with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

Step 2: Load and run the model with TensorFlow Lite.

import tensorflow as tf
import numpy as np

# Load the TensorFlow Lite model
interpreter = tf.lite.Interpreter(model_path='model.tflite')
interpreter.allocate_tensors()

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

# Prepare input data (e.g., first test sample)
test_sample = np.expand_dims(X_test[0], axis=0).astype(np.float32)

# Set the input tensor
interpreter.set_tensor(input_details[0]['index'], test_sample)

# Run the model
interpreter.invoke()

# Get the output tensor (predictions)
output_data = interpreter.get_tensor(output_details[0]['index'])
print(f"TensorFlow Lite model predictions: {output_data}")

Exercise 5: Using Model Checkpointing and Early Stopping

Task: Train a Keras model with model checkpointing and early stopping to save the best-performing model and stop training when the performance stops improving.

Solution:

from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping

# Define the ModelCheckpoint and EarlyStopping callbacks
checkpoint_callback = ModelCheckpoint(filepath='best_model.h5', save_best_only=True, monitor='val_accuracy', mode='max', verbose=1)
early_stopping_callback = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True, verbose=1)

# Train the model with both callbacks
model.fit(X_train, y_train, epochs=20, validation_data=(X_test, y_test), callbacks=[checkpoint_callback, early_stopping_callback])

In this solution:

  • ModelCheckpoint saves the best model based on validation accuracy.
  • EarlyStopping halts training when validation loss stops improving for 3 consecutive epochs.

These exercises reinforce the key concepts from Chapter 3, including saving and loading Keras models, deploying them in production environments using TensorFlow Serving and Flask, converting models to TensorFlow Lite for mobile applications, and using model checkpointing and early stopping for efficient training. These tasks are essential for transitioning models from research to real-world use.

Practical Exercises Chapter 3

Exercise 1: Saving and Loading a Keras Model

Task: Train a simple neural network on the MNIST dataset, save the model using the SavedModel format, and then load the saved model to make predictions on the test set.

Solution:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import mnist

# Load the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Normalize the data
X_train, X_test = X_train / 255.0, X_test / 255.0

# Define a simple Sequential model
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(X_train, y_train, epochs=5)

# Save the model in SavedModel format
model.save('saved_model')

# Load the saved model
loaded_model = tf.keras.models.load_model('saved_model')

# Use the loaded model to make predictions
predictions = loaded_model.predict(X_test)
print(f"Predictions for the first test sample: {predictions[0]}")

Exercise 2: Deploying a Keras Model with TensorFlow Serving

Task: Train a neural network, save it in the SavedModel format, and prepare the model for deployment using TensorFlow Serving. Write a client-side script that sends a request to the model’s API for predictions.

Solution:

Step 1: Save the model for TensorFlow Serving.

# Save the model in SavedModel format for TensorFlow Serving
model.save('serving_model/keras_served_model')

Step 2: Run TensorFlow Serving using Docker.

docker pull tensorflow/serving
docker run -p 8501:8501 --name tf_serving \\
  --mount type=bind,source=$(pwd)/serving_model/keras_served_model,target=/models/keras_served_model \\
  -e MODEL_NAME=keras_served_model -t tensorflow/serving

Step 3: Send a request to TensorFlow Serving.

import requests
import json
import numpy as np

# Prepare input data
input_data = np.expand_dims(X_test[0], axis=0).tolist()

# Define the API URL for TensorFlow Serving
url = '<http://localhost:8501/v1/models/keras_served_model:predict>'

# Send a POST request to TensorFlow Serving
response = requests.post(url, json={"instances": input_data})

# Parse the response
predictions = response.json()['predictions']
print(f"Predictions: {predictions}")

Exercise 3: Deploying a Keras Model with Flask

Task: Create a Flask web app that loads a trained Keras model and serves it via an API for making predictions. Write a client script to send a request to the Flask app.

Solution:

Step 1: Create the Flask app.

from flask import Flask, request, jsonify
from tensorflow.keras.models import load_model
import numpy as np

# Initialize the Flask app
app = Flask(__name__)

# Load the trained Keras model
model = load_model('saved_model')

# Define an API route for predictions
@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json(force=True)
    input_data = np.array(data['instances'])
    predictions = model.predict(input_data)
    return jsonify(predictions=predictions.tolist())

# Run the Flask app
if __name__ == '__main__':
    app.run(port=5000, debug=True)

Step 2: Client-side script to send a request to the Flask app.

import requests
import json
import numpy as np

# Prepare input data
input_data = np.expand_dims(X_test[0], axis=0).tolist()

# Define the Flask API URL
url = '<http://localhost:5000/predict>'

# Send a POST request to the Flask API
response = requests.post(url, json={"instances": input_data})

# Parse the response
predictions = response.json()['predictions']
print(f"Predictions: {predictions}")

Exercise 4: Converting a Keras Model to TensorFlow Lite

Task: Convert a trained Keras model to TensorFlow Lite format and save it. Use the TensorFlow Lite interpreter to run inference on the test dataset.

Solution:

Step 1: Convert the model to TensorFlow Lite.

# Convert the Keras model to TensorFlow Lite format
converter = tf.lite.TFLiteConverter.from_saved_model('saved_model')
tflite_model = converter.convert()

# Save the TensorFlow Lite model to a file
with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

Step 2: Load and run the model with TensorFlow Lite.

import tensorflow as tf
import numpy as np

# Load the TensorFlow Lite model
interpreter = tf.lite.Interpreter(model_path='model.tflite')
interpreter.allocate_tensors()

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

# Prepare input data (e.g., first test sample)
test_sample = np.expand_dims(X_test[0], axis=0).astype(np.float32)

# Set the input tensor
interpreter.set_tensor(input_details[0]['index'], test_sample)

# Run the model
interpreter.invoke()

# Get the output tensor (predictions)
output_data = interpreter.get_tensor(output_details[0]['index'])
print(f"TensorFlow Lite model predictions: {output_data}")

Exercise 5: Using Model Checkpointing and Early Stopping

Task: Train a Keras model with model checkpointing and early stopping to save the best-performing model and stop training when the performance stops improving.

Solution:

from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping

# Define the ModelCheckpoint and EarlyStopping callbacks
checkpoint_callback = ModelCheckpoint(filepath='best_model.h5', save_best_only=True, monitor='val_accuracy', mode='max', verbose=1)
early_stopping_callback = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True, verbose=1)

# Train the model with both callbacks
model.fit(X_train, y_train, epochs=20, validation_data=(X_test, y_test), callbacks=[checkpoint_callback, early_stopping_callback])

In this solution:

  • ModelCheckpoint saves the best model based on validation accuracy.
  • EarlyStopping halts training when validation loss stops improving for 3 consecutive epochs.

These exercises reinforce the key concepts from Chapter 3, including saving and loading Keras models, deploying them in production environments using TensorFlow Serving and Flask, converting models to TensorFlow Lite for mobile applications, and using model checkpointing and early stopping for efficient training. These tasks are essential for transitioning models from research to real-world use.