Saving Models • TensorFlow Lite • Flask API • Real-World Examples
Building a deep learning model is only the first step. Once a model is trained, the next task is to deploy it so real people can use it. Deployment means making your model available inside an app, website, mobile device, or another system. This chapter covers three essential deployment skills: saving models, using models with TensorFlow Lite, and creating a simple Flask API.
Why Save a Model?
- Saves time (no need to retrain)
- Makes deployment possible
- Helps share models with others
- Useful for backups
How Models Are Saved
TensorFlow/Keras allows you to save:
- Model architecture
- Weights
- Optimizer settings
Code Example: Saving a Model
# Save the entire model
model.save("my_model.h5")Loading the Model Later
from tensorflow.keras.models import load_model
model = load_model("my_model.h5")Real Example
A model that predicts handwritten digits can be trained once and reused anytime to classify new digits without learning again.
What Is TensorFlow Lite?
TensorFlow Lite (TFLite) allows you to run deep learning models on:
- Mobile phones
- Tablets
- IoT devices
- Microcontrollers
This Is Great For Apps Like
- Face unlock
- Voice recognition
- Smart home devices
- Offline AI apps
Why TensorFlow Lite?
- Faster on mobile devices
- Smaller file size
- Low power usage
A. Converting a Model to TensorFlow Lite
import tensorflow as tf
# Load your trained model
model = tf.keras.models.load_model("my_model.h5")
# Convert it to TFLite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the converted model
with open("model.tflite", "wb") as f:
f.write(tflite_model)Explanation: Now your model is ready for mobile apps.
B. Using a TFLite Model in Mobile Apps
Android Apps
Developers load model.tflite in Java/Kotlin and run predictions on phone images or voice input.
iOS Apps
Developers use Swift and TensorFlow Lite libraries.
Example Use Cases
- An AI plant disease detection app
- A mobile handwritten digit recognition app
- Offline translation apps
What Is Flask?
Flask is a lightweight Python web framework. It allows you to create simple APIs to expose your model through the internet or a local network.
Why Use Flask?
- Easy to learn
- Runs locally or on servers
- Lets apps send data to your model
- Helps build AI-powered websites
What Is an API?
API stands for Application Programming Interface. It allows different systems to talk to each other.
Real Example
A website uploads an image → your Flask API → model predicts → sends back the result.
A. Simple Flask API Code
Step 1: Save Your Model
model.save("my_model.h5")Step 2: Create app.py
from flask import Flask, request, jsonify
from tensorflow.keras.models import load_model
import numpy as np
app = Flask(__name__)
model = load_model("my_model.h5")
@app.route("/predict", methods=["POST"])
def predict():
data = request.json["input"] # Get input from user
data = np.array(data).reshape(1, -1)
prediction = model.predict(data)
return jsonify({"prediction": prediction.tolist()})
if __name__ == "__main__":
app.run(debug=True)How This Works
- User sends data (e.g., numbers representing features)
- API sends data to the model
- Model predicts the output
- API returns prediction
- Works on websites, mobile apps, or other systems
B. Testing the API
Use a POST Request with JSON
{
"input": [5.1, 3.4, 1.5, 0.2]
}The API Responds With
{
"prediction": [[0.89]]
}Explanation: This means the model predicts the result with 89% confidence.
Complete Deployment Process
- Train the model using TensorFlow, Keras, or PyTorch
- Save the model for reuse and deployment
- Convert to TensorFlow Lite (optional) if running on mobile or small devices
- Create a Flask API (optional) if deploying on websites or servers
- Host your model on local computer, cloud (AWS, Google Cloud, Azure), or mobile app
- Users send data → model predicts
A. Medical AI App
- Model predicts eye diseases
- Doctors upload retinal images
- API returns prediction instantly
B. Voice Assistant
- Model runs on a phone using TensorFlow Lite
- Predicts commands like "play," "pause," or "call mom"
C. E-commerce Website
- Flask API recommends products
- Users browse items → API suggests similar ones