Module 13: Model Deployment
Learn to serve machine learning models through a local or containerized API. Cloud and reverse-proxy topics are optional extensions, not a claim that you ran a full production site.
What You'll Learn
- Model Serialization
- REST APIs with Flask/FastAPI
- Docker for ML
- Optional cloud or PaaS deploy paths
- Model Monitoring basics
- Serve-path practices (validation, logging, versioning)
Topics Covered
1. Model Serialization
- Pickle: Python's native serialization
- Joblib: Better for NumPy arrays
- H5/HDF5: For Keras models
- ONNX: Cross-platform format
- Saving: Architecture + weights
2. REST APIs
- Flask: Simple web framework
- Creating endpoints
- Request/response handling
- Error handling
- FastAPI: Modern, fast framework
- Automatic documentation
- Type hints
- Async support
- API Design: Clear request/response contracts
3. Docker for ML
- Containerization: Package model + dependencies
- Dockerfile: Define container
- Docker Images: Build and run
- Docker Compose: Multi-container apps
- Benefits: Reproducibility, portability
4. Optional cloud or PaaS paths
- AWS: SageMaker, EC2, Lambda (overview + optional lab)
- Google Cloud: Vertex AI, Cloud Run
- Azure: Azure ML, Container Instances
- Heroku / similar: Simple staging deploy
- Choosing Platform: Based on needs and budget
5. Optional server setup (beyond local serve)
- NGINX Configuration: Reverse proxy, load balancing, SSL termination (survey or optional lab)
- SSL/TLS Setup: Let's Encrypt certificates, auto-renewal
- Domain Configuration: DNS setup, subdomain routing
- Security: Rate limiting, API authentication, input validation
- Error Handling: Structured error responses, logging
- AWS EC2 Setup: Instance configuration, systemd services, firewall (optional)
6. Model Serving
- Batch Inference: Process in batches
- Online Inference: Low-latency local or container API
- A/B Testing: Compare model versions (concepts and small demos)
- Canary ideas: Gradual rollout patterns (concepts)
7. Model Monitoring
- Performance Metrics: Track accuracy over time when labels arrive
- Data Drift: Detect distribution changes
- Model Drift: Performance degradation
- Logging: Track predictions and errors
- Alerts: Plan what you would notify on
Learning Objectives
By the end of this module, you should be able to:
- Serialize and load models
- Create a local REST API that scores inputs
- Containerize that API with Docker
- Optionally push the same image to a cloud or PaaS staging path
- Sketch reverse-proxy / TLS / auth patterns without claiming you hardened a live site
- Add basic validation, logging, and model version fields
- Outline monitoring signals for a served model
Projects
- Flask API: Serve a model locally with Flask
- FastAPI Service: Build a FastAPI scoring service
- Docker Container: Containerize the ML API
- Optional staging deploy: Push the container to AWS/GCP/Azure or a simple PaaS
- Monitoring notes: Log scores/latency and sketch a small dashboard
Key Concepts
- API Endpoints: Expose model as a local or container service
- Containerization: Package everything together
- Serve path: Handle requests with clear contracts
- Monitoring: Track model health on the path you actually run
- Versioning: Manage model versions
Documentation & Learning Resources
FastAPI:
Docker:
Flask:
MLflow:
Free Courses:
- FastAPI Course (YouTube): Free tutorial
- Docker Course (YouTube): Free comprehensive course
- ML Deployment (Coursera): Free audit available
Tutorials:
- Deploying ML Models (Real Python)
- Docker for Data Scientists
- ML Model Deployment Guide
- Setting up NGINX as Reverse Proxy (DigitalOcean)
- SSL Certificate Setup with Let's Encrypt (DigitalOcean)
- Production FastAPI Deployment (TestDriven.io)
NGINX:
SSL/TLS & Security:
- Let's Encrypt Documentation
- Certbot User Guide
- SSL/TLS Best Practices (Mozilla)
- OWASP API Security Top 10
Domain & DNS:
Cloud Platforms:
- AWS SageMaker Documentation
- AWS EC2 Documentation
- Google Cloud AI Platform
- Azure ML Documentation
- Heroku Deployment Guide
Video Tutorials:
- FastAPI Tutorial (Corey Schafer)
- Docker Tutorial (TechWorld with Nana)
- NGINX Tutorial (LearnLinuxTV)
- SSL/TLS Explained (PowerCert)
- ML Deployment (Sentdex)
Additional Resources:
- Advanced Topics: Advanced deployment patterns, Kubernetes, edge deployment
- Project Tutorial: Step-by-step model deployment project
- Quick Reference: Quick lookup guide for model deployment
Previous Module: 12-natural-language-processing
Next Module: 14-mlops-basics