Machine Learning in Production: Master the art of delivering robust Machine Learning solutions with MLOps 9355518102, 9789355518101

Deploy, manage, and scale Machine Learning models with MLOps effortlessly Key Features ● Explore several ways to

346 62 39MB

English Pages 458 [462] Year 2023

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Book title
Inner title
Copyright
Dedicated
About the Author
About the Reviewer
Acknowledgement
Preface
Code Bundle and Coloured Images
Piracy
Table of Contents
Chapter 1: Python101
Introduction
Structure
Objectives
Install Python
On Windows and Mac OS
On Linux
Install Anaconda
Install code editor
Hello World!
Execution of Python file
Data structures
Common data structures
Control statements and loops
if…else
for loop
while loop
pass statement
Functions
Object Oriented Programming (OOP)
Numerical Python (NumPy)
Reshaping array
Pandas
loc - selection by label
iloc - selection by position
Save and load CSV files
Save and load Excel files
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Key terms
Chapter 2: Git and GitHub Fundamentals
Introduction
Structure
Objectives
Git concepts
Git + Hub = GitHub
Common Git workflow
Install Git and create a GitHub account
Linux (Debian/Ubuntu)
Git for all platforms
GUI clients
Create a GitHub account
Common Git commands
Setup
New repository
Update
Changes
Revert
Let’s Git
Configuration
Initialize the Git repository
Check Git status
Add a new file
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 3: Challenges in ML Model
Deployment
Introduction
Structure
Objectives
ML life cycle
Business impact
Data collection
Data preparation
Feature engineering
Build and train the model
Test and evaluate
Model deployment
Monitoring and optimization
Types of model deployment
Batch predictions
Web service/REST API
Mobile and edge devices
Real-time
Challenges in deploying models in the production environment
Team coordination
Data-related challenges
Portability
Scalability
Robustness
Security
MLOps
Benefits of MLOps
Efficient management of ML life cycle
Reproducibility
Automation
Tracking and feedback loop
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Key terms
Chapter 4: Packaging ML Models
Introduction
Structure
Objectives
Virtual environments
Requirements file
Serializing and de-serializing ML models
Testing Python code with pytest
pytest fixtures
Python packaging and dependency management
Modular programming
Module
Package
Developing, building, and deploying ML packages
Business problem
Data
Building the ML model
Developing the package
Set up environment variables and paths
Build the package
Install the package
Package usage with example
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Key terms
Chapter 5: MLflow-Platform to Manage the ML Life Cycle
Introduction
Structure
Objectives
Introduction to MLflow
Set up your environment and install MLflow
MLflow components
MLflow tracking
Log data into the run
MLflow projects
MLflow models
MLflow registry
Set up the MySQL server for MLflow
Start the MLflow server
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 6: Docker for
ML
Introduction
Structure
Objectives
Introduction to Docker
It works on my machine!
Long setup
Setting up your environment and installing Docker
Docker installation
Install Docker Engine
Docker compose
Hello World with Docker
Docker objects
Create a Dockerfile
Build a Docker image
Run a Docker container
Dockerize and deploy the ML model
Common Docker commands
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 7: Build ML Web Apps Using API
Introduction
Structure
Objectives
REST APIs
FastAPI
Streamlit
Flask
Gunicorn
NGINX
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 8: Build Native
ML Apps
Introduction
Structure
Objectives
Introduction to Tkinter
Hello World app using Tkinter
Build an ML-based app using Tkinter
Tkinter app
Convert Python app into Windows EXE file
Build an ML-based app using kivy and kivyMD
KivyMD app
Convert the Python app into an Android app
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 9: CI/CD for
ML
Introduction
Structure
Objectives
CI/CD pipeline for ML
Continuous Integration (CI)
Continuous Delivery/Deployment (CD)
Continuous Training (CT)
Introduction to Jenkins
Installation
Build CI/CD pipeline using GitHub, Docker, and Jenkins
Develop codebase
Create a Personal Access Token (PAT) on GitHub
Create a webhook on the GitHub repository
Configure Jenkins
Create CI/CD pipeline using Jenkins
Stage 1: 1-GitHub-to-container
Stage 2: 2-training
Stage 3: 3-testing
Stage 4: 4-deployment-status-email
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 10: Deploying ML Models on
Heroku
Introduction
Structure
Objectives
Heroku
Setting up Heroku
Deployment with Heroku Git
Deployment with GitHub repository integration
REVIEW APPS
STAGING
PRODUCTION
Heroku Pipeline flow
Deployment with Container Registry
GitHub Actions
Configuration
CI/CD pipeline using GitHub Actions and Heroku
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 11: Deploying ML Models on
Microsoft Azure
Introduction
Structure
Objectives
Azure
Set up an Azure account
Deployment using GitHub Actions
Infrastructure setup
GitHub Actions
Service principal
Configure Azure App Service to use GitHub Actions for CD
Deployment using Azure DevOps and Azure ML
Azure Machine Learning (AML) service
Workspace
Experiments
Runs
Configure CI pipeline
Configure CD pipeline
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 12: Deploying ML Models on Google Cloud Platform
Introduction
Structure
Objectives
Google Cloud Platform (GCP)
Set up the GCP account
Cloud Source Repositories
Cloud Build
Container Registry
Kubernetes
Google Kubernetes Engine (GKE)
Deployment using Cloud Shell – Manual Trigger
CI/CD pipeline using Cloud Build
Create a trigger in Cloud Build
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 13: Deploying ML Models on Amazon Web Services
Introduction
Structure
Objectives
Introduction to Amazon Web Services (AWS)
AWS compute services
Amazon Elastic Compute Cloud (EC2)
Amazon Elastic Container Service (ECS)
Amazon Elastic Kubernetes Service (EKS)
Amazon Elastic Container Registry (ECR)
AWS Fargate
AWS Lambda
Amazon SageMaker
Set up an AWS account
AWS CodeCommit
Continuous Training
Amazon Elastic Container Registry (ECR)
Docker Hub rate limit
AWS CodeBuild
Attach container registry access to CodeBuild’s service role
Amazon Elastic Container Service (ECS)
AWS ECS deployment models
Task definition
Load balancing
Service
CI/CD pipeline using CodePipeline
AWS CodePipeline
Monitoring
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 14: Monitoring and Debugging
Introduction
Structure
Objectives
Importance of monitoring
Fundamentals of ML monitoring
Metrics for monitoring your ML system
Drift in ML
Types of drift in ML
Techniques to detect the drift in ML
Addressing the drift in ML
Operational monitoring with Prometheus and Grafana
ML model monitoring with whylogs and WhyLabs
whylogs
Constraints for data quality validation
WhyLabs
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Chapter 15: Post-Productionizing ML Models
Introduction
Structure
Objectives
Bridging the gap between the ML model and the creation of business value
Model security
Adversarial attack
Data poisoning attack
Distributed Denial of Service attack (DDoS)
Data privacy attack
Mitigate the risk of model attacks
A/B testing
MLOps is the future
Conclusion
Points to remember
Multiple choice questions
Answers
Questions
Index
back title
Recommend Papers

Machine Learning in Production: Master the art of delivering robust Machine Learning solutions with MLOps
 9355518102, 9789355518101

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview



i



Machine Learning in Production Master the art of delivering robust Machine Learning solutions with MLOps

Suhas Pote

www.bpbonline.com

i

ii



Copyright © 2023 BPB Online All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor BPB Online or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. BPB Online has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, BPB Online cannot guarantee the accuracy of this information.

First published: 2023 Published by BPB Online WeWork 119 Marylebone Road London NW1 5PU UK | UAE | INDIA | SINGAPORE ISBN 978-93-55518-101

www.bpbonline.com



Dedicated to My parents:

I am so grateful for your belief in me, and

for the many sacrifices you have made throughout

my life. Your love and support mean the world to me.

iii

iv



About the Author Suhas Pote has over eight years of multidisciplinary experience in data science, playing central roles in numerous projects as a technical leader and data scientist, delivering projects using open-source technologies for big companies, including successful projects in South America, Europe, and the United States. He is experienced in client engagement and working collaboratively with different teams. Currently, he is a process manager at Eclerx and is an accomplished postgraduate, having completed a degree in Data Science, Business Analytics, and Big Data. He holds a Bachelor's degree focused on Electronics and Telecommunication Engineering. In the meantime, he successfully got many certifications in data science and related tools. Furthermore, the author participates as a speaker in Data Science conferences and writes technical articles on machine learning and related topics. He also contributes to technical communities worldwide, such as Stack Overflow.



v

About the Reviewer Karan Vijay Singh is a passionate Big Data / ML Engineer with extensive professional experience in the domain of Cloud Cost Optimization. With his optimisation skills, he has saved his current organization 0.5M dollar a year in cost. He also has co-authored a research paper in 2019 in the field of cloud computing which has over 270 citations worldwide. Karan did his Master’s in Mathematics (Computer Science) from University of Waterloo in Canada. He is currently working as a Data Engineer at Lightspeed Commerce in Canada.

vi



Acknowledgement I want to express my deepest gratitude to my family and friends for their unwavering support and encouragement throughout this book's writing, especially my wife, Dr. Archana and my son, Eshansh. I am also grateful to BPB Publications for their guidance and expertise in bringing this book to fruition. It was a long journey of revising this book, with valuable participation and collaboration of reviewers, technical experts, and editors. I would also like to acknowledge the valuable contributions of my colleagues and co-worker during many years working in the tech industry, who have taught me so much and provided valuable feedback on my work. Finally, I would like to thank all the readers who have taken an interest in my book and for their support in making it a reality. Your encouragement has been invaluable.



vii

Preface Productionizing the machine learning model is a complex task that requires a comprehensive understanding of the latest technologies and CI/CD pipeline. MLOps become increasingly popular in the field of Data Science. This book is designed to provide a comprehensive guide to building and deploying ML applications with MLOps. It covers a wide range of topics, including the basics of Python programming, Git, Machine Learning life cycle, Docker, and advanced concepts such as packaging Python code for ML models, monitoring, model security, Kubernetes, testing using pytest and the use of CI/CD pipeline for building and deploying robust and scalable ML applications on cloud platforms, including Azure, GCP, and AWS. Throughout the book, you will learn about the MLOps, various tools, and techniques to deploy ML models. You will also learn how to use them to productionize ML models and applications that are efficient, scalable, and easy to maintain. Additionally, you will learn about best practices and design patterns for MLOps. This book is intended for data scientists, software developers, data engineers, data analysts, and managers who are new to MLOps and want to learn how to productionize ML models. It is also helpful for experienced data scientists and ML engineers who want to expand their knowledge of these technologies and improve their skills in deploying ML models in production. With this book, you will gain the knowledge and skills to become proficient in the field of MLOps. I hope you will find this book informative and helpful. Chapter 1: Python 101 – explains the Python fundamental concepts needed for the reader to equip with the prerequisites required for this book, including Python installation, data structures, control statements, loops, functions, and data manipulation using pandas. This is a quick refresher for those who have worked with Python. If you are a beginner, this chapter will cover the most common Python commands needed to build and deploy ML models in production. It allows the reader to learn fundamental concepts related to the Object-Oriented Programming paradigm using Python. Chapter 2: Git and GitHub Fundamentals – presents a detailed overview of Git workflow, including common Git commands with practical examples. This is

viii



essential content for the entire book, as this chapter covers fundamental aspects of Git and GitHub that influence technical decisions to build the CI/CD pipelines to deploy ML models in the production environment. Chapter 3: Challenges in ML Model Deployment – covers the various stages of the ML life cycle, including details on the challenges of each stage. It also covers the common challenges in deploying models in the production environment and how MLOps can help to overcome them. Additionally, the chapter discusses different approaches to deploying ML models in production. Chapter 4: Packaging ML Models – allows the reader to learn fundamental concepts related to modular programming using the Python language, including Python packaging, dependency management, and good practices of software development to develop stable, readable, and extensible code for robust enterprise applications. Furthermore, the chapter explains the virtual environment, testing code using pytest, serializing, and deserializing ML models. It covers packaging ML models, code, and dependencies so that the package can be installed and consumed on another machine or server. Chapter 5: MLflow-Platform to Manage the ML Life Cycle – gives special attention to streamlining machine learning development, including tracking experiments, packaging code into reproducible runs, and sharing and deploying models. It demonstrates how to train, deploy, and reuse ML models using MLflow through practical examples based on the use case. This chapter explains the role of MLflow in an ML life cycle. Chapter 6: Docker for ML – shows the basic concepts of Docker and provides practical examples of common Docker commands with a use case for the reader. Learning these commands allows the reader to package ML code with its dependencies and run the application inside Docker containers. This chapter includes practical examples of Docker objects such as Docker images and containers. Chapter 7: Build ML Web Apps Using API – explains in detail the most commonly used frameworks for building web-based ML apps using the Python language, including FastAPI, Streamlite, and Flask. It also allows the reader to learn the basics of Gunicorn, NGINX, and APIs, including an explanation of REST APIs, and much more.



ix

Chapter 8: Build Native ML Apps – is dedicated to building native ML applications in Python to give the reader more familiarity with converting Python apps into Windows and Android apps and ways to consume them. This chapter covers practical examples of working with Tkinter, kivy, kivyMD, pyinstaller, and buildozer. Chapter 9: CI/CD for ML – allows the reader to learn and implement the different stages of the CI/CD pipeline using GitHub and Jenkins, including committing, building, testing, and deploying. This chapter explains the GitHub and Jenkins integrations to build an automated CI/CD pipeline for deploying ML apps using Python and Docker. Chapter 10: Deploying ML Models on Heroku – will guide the reader in configuring and building a CI/CD pipeline using GitHub Actions to deploy a web-based ML app on the Heroku platform. This chapter also explains three methods for deploying the web app on Heroku: Heroku Git, GitHub integration, and Container registry. It covers practical examples for creating workflow (YAML) files for GitHub Actions, including using tox and pytest for testing the code, and an automated Heroku pipeline for faster deployments. Chapter 11: Deploying ML Models on Microsoft Azure – explains the step-bystep approach to building CI/CD pipeline using GitHub Actions to deploy webbased ML apps to Azure web services. In the other approach, the reader will learn to build a CI/CD pipeline and deploy web-based scalable ML apps to Azure Container Instances (ACI) and Azure Kubernetes Service (AKS) using Azure DevOps and Azure Machine Learning (AML) Service. Chapter 12: Deploying ML Models on Google Cloud Platform – Shows how to build an automated CI/CD pipeline to deploy ML models on Google Kubernetes Engine (GKE), without the need to integrate any external tool, service, or platform. This chapter allows the reader to learn and implement Kubernetes functionality to run scalable ML apps using Google Kubernetes Engine (GKE). Chapter 13: Deploying ML Models on Amazon Web Services – presents an overview of various cloud compute services offered by Amazon Web Services (AWS). This chapter allows the reader to learn and implement an automated CI/CD pipeline with Continuous Training (CT) to deploy scalable enterprise ML apps on Amazon Elastic Container Service (ECS). Additionally, it covers the integration of Application Load Balancer (ALB), Amazon Virtual Private Cloud

x



(Amazon VPC), and security groups into Amazon Elastic Container Service (ECS) for building robust and secure enterprise ML apps. Chapter 14: Monitoring and Debugging – is dedicated to monitoring ML and operational metrics, including servers, cost of services, drifts in ML, input data, ML models, and much more. This chapter shows the importance and fundamental concepts of monitoring in the ML life cycle. It also covers practical examples using whylogs and WhyLabs for ML model monitoring, and Prometheus and Grafana for operational monitoring. Chapter 15: Post-Productionizing ML Models – presents a detailed overview of adversarial machine learning, including different types of adversarial attacks and how to mitigate them. It also covers fundamental concepts of A/B testing and the future scope of MLOps in the industry.



xi

Code Bundle and Coloured Images Please follow the link to download the Code Bundle and the Coloured Images of the book:

https://rebrand.ly/vlv0nzp The code bundle for the book is also hosted on GitHub at https://github.com/bpbpublications/Machine-Learning-in-Production. In case there's an update to the code, it will be updated on the existing GitHub repository. We have code bundles from our rich catalogue of books and videos available at https://github.com/bpbpublications. Check them out!

Errata We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors, if any, that may have occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at : [email protected] Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family. Did you know that BPB offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.bpbonline.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at : [email protected] for more details. At www.bpbonline.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on BPB books and eBooks.

xii



Piracy If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit www.bpbonline.com. We have worked with thousands of developers and tech professionals, just like you, to help them share their insights with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Reviews Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions. We at BPB can understand what you think about our products, and our authors can see your feedback on their book. Thank you! For more information about BPB, please visit www.bpbonline.com.

Join our book's Discord space

Join the book's Discord Workspace for Latest updates, Offers, Tech happenings around the world, New Release and Sessions with the Authors: https://discord.bpbonline.com



xiii

Table of Contents 1. Python 101...................................................................................................................... 1 Introduction............................................................................................................. 1 Structure................................................................................................................... 1 Objectives................................................................................................................. 2

Install Python.......................................................................................................... 2



On Linux............................................................................................................ 2



On Windows and Mac OS................................................................................. 2



Install Anaconda..................................................................................................... 2



Hello World!............................................................................................................ 3



Install code editor................................................................................................... 3 Execution of Python file...................................................................................... 3



Data structures........................................................................................................ 4



Control statements and loops............................................................................... 6



Object Oriented Programming (OOP)................................................................. 9



Common data structures.................................................................................... 4

Functions.................................................................................................................. 8

Numerical Python (NumPy)............................................................................... 10

Pandas.................................................................................................................... 12 Conclusion............................................................................................................. 15

Points to remember.............................................................................................. 15 Multiple choice questions.................................................................................... 16

Answers............................................................................................................ 16 Questions............................................................................................................... 16

Key terms............................................................................................................... 16

2. Git and GitHub Fundamentals................................................................................ 17 Introduction........................................................................................................... 17 Structure................................................................................................................. 17 Objectives............................................................................................................... 18

Git concepts........................................................................................................... 18

xiv



 Git + Hub = GitHub......................................................................................... 19



Common Git workflow........................................................................................ 19



Linux (Debian/Ubuntu)................................................................................... 20



Install Git and create a GitHub account............................................................ 20 Git for all platforms.......................................................................................... 20 GUI clients........................................................................................................ 20 Create a GitHub account.................................................................................. 20



Common Git commands...................................................................................... 21



New repository.................................................................................................. 21

Setup................................................................................................................. 21 Update............................................................................................................... 21 Changes............................................................................................................. 21 Revert................................................................................................................ 22

Let’s Git.................................................................................................................. 22



Initialize the Git repository............................................................................... 22



Configuration.................................................................................................... 22 Check Git status................................................................................................ 22 Add a new file................................................................................................... 23

Conclusion............................................................................................................. 26

Points to remember.............................................................................................. 26 Multiple choice questions ................................................................................... 26

Answers............................................................................................................ 27 3. Challenges in ML Model Deployment.................................................................. 29 Introduction........................................................................................................... 29 Structure................................................................................................................. 29 Objectives............................................................................................................... 30

ML life cycle........................................................................................................... 30



Data collection.................................................................................................. 31



Business impact................................................................................................ 31 Data preparation............................................................................................... 32 Feature engineering.......................................................................................... 32 Build and train the model................................................................................. 33



xv



Test and evaluate............................................................................................... 34



Monitoring and optimization........................................................................... 35



Model deployment............................................................................................. 34



Types of model deployment................................................................................ 35



Web service/REST API..................................................................................... 36



Batch predictions.............................................................................................. 35 Mobile and edge devices.................................................................................... 36

Real-time........................................................................................................... 37

Challenges in deploying models in the production environment................ 37



Data-related challenges..................................................................................... 38



Team coordination............................................................................................. 37

Portability......................................................................................................... 38 Scalability......................................................................................................... 38 Robustness........................................................................................................ 38 Security............................................................................................................. 39 MLOps.................................................................................................................... 39

Benefits of MLOps................................................................................................ 40 Efficient management of ML life cycle.............................................................. 41

Reproducibility................................................................................................. 41 Automation....................................................................................................... 41

Tracking and feedback loop............................................................................... 42



Points to remember.............................................................................................. 42

Conclusion............................................................................................................. 42

Multiple choice questions.................................................................................... 43

Answers............................................................................................................ 43 Questions............................................................................................................... 43

Key terms............................................................................................................... 43

4. Packaging ML Models............................................................................................... 45 Introduction........................................................................................................... 45 Structure................................................................................................................. 45 Objectives............................................................................................................... 46

Virtual environments........................................................................................... 46

xvi





Requirements file.................................................................................................. 47



Testing Python code with pytest........................................................................ 48



Serializing and de-serializing ML models........................................................ 48 pytest fixtures................................................................................................... 49

Python packaging and dependency management.......................................... 49 Modular programming..................................................................................... 50

Module.............................................................................................................. 50 Package............................................................................................................. 50

Developing, building, and deploying ML packages....................................... 51 Business problem.............................................................................................. 52

Data.................................................................................................................. 52

Building the ML model..................................................................................... 53



Set up environment variables and paths........................................................... 70



Install the package............................................................................................. 71



Developing the package..................................................................................... 53 Build the package.............................................................................................. 71 Package usage with example............................................................................. 73

Conclusion............................................................................................................. 74

Points to remember.............................................................................................. 74 Multiple choice questions.................................................................................... 74

Answers............................................................................................................ 75 Questions............................................................................................................... 75

Key terms............................................................................................................... 75

5. MLflow-Platform to Manage the ML Life Cycle.................................................. 77 Introduction........................................................................................................... 77 Structure................................................................................................................. 77 Objectives............................................................................................................... 78

Introduction to MLflow....................................................................................... 78



Miniconda installation........................................................................................... 79



Set up your environment and install MLflow.................................................. 79 MLflow components......................................................................................... 82

MLflow tracking................................................................................................... 83



xvii

Log data into the run........................................................................................ 84



MLflow projects.................................................................................................... 94



MLflow registry.................................................................................................. 100



MLflow models..................................................................................................... 97 Set up the MySQL server for MLflow............................................................ 101 Start the MLflow server.................................................................................. 103

Conclusion........................................................................................................... 109

Points to remember............................................................................................ 109 Multiple choice questions.................................................................................. 109

Answers.......................................................................................................... 110 Questions............................................................................................................. 110 6. Docker for ML........................................................................................................... 111

Introduction..........................................................................................................111 Structure................................................................................................................111

Objectives............................................................................................................. 112

Introduction to Docker....................................................................................... 112



Long setup....................................................................................................... 112



It works on my machine!................................................................................ 112 Setting up your environment and installing Docker..................................... 113 Docker installation.......................................................................................... 113 Uninstall old versions.......................................................................................... 113



Install Docker Engine..................................................................................... 114



Hello World with Docker.................................................................................. 116



Dockerfile.............................................................................................................. 117



Docker image........................................................................................................ 117



Docker containers................................................................................................. 118



Docker container networking............................................................................... 118



Create a Dockerfile............................................................................................. 119



Run a Docker container..................................................................................... 120





Docker compose............................................................................................... 115

Docker objects..................................................................................................... 117

Build a Docker image......................................................................................... 120

xviii



 Dockerize and deploy the ML model.............................................................. 122 Common Docker commands............................................................................ 127

Conclusion........................................................................................................... 127

Points to remember............................................................................................ 127 Multiple choice questions.................................................................................. 128

Answers.......................................................................................................... 128 Questions............................................................................................................. 128 7. Build ML Web Apps Using API............................................................................ 129 Introduction......................................................................................................... 129 Structure............................................................................................................... 129 Objectives............................................................................................................. 130

Rest APIs.............................................................................................................. 130

FastAPI................................................................................................................. 131 Streamlit............................................................................................................... 139 Flask...................................................................................................................... 143 Gunicorn......................................................................................................... 150 NGINX .......................................................................................................... 150 Conclusion........................................................................................................... 154

Points to remember............................................................................................ 154 Multiple choice questions.................................................................................. 155

Answers.......................................................................................................... 155 8. Build Native ML Apps............................................................................................ 157 Introduction......................................................................................................... 157 Structure............................................................................................................... 157 Objectives............................................................................................................. 158

Introduction to Tkinter...................................................................................... 158



Build an ML-based app using Tkinter............................................................. 160



Convert Python app into Windows EXE file.................................................. 167



KivyMD app................................................................................................... 173



Hello World app using Tkinter....................................................................... 159 Tkinter app...................................................................................................... 163

Build an ML-based app using kivy and kivyMD.......................................... 170



xix



Convert the Python app into an Android app............................................... 179



Points to remember............................................................................................ 181

Conclusion........................................................................................................... 180

Multiple choice questions.................................................................................. 181

Answers.......................................................................................................... 181 Questions............................................................................................................. 181 9. CI/CD for ML............................................................................................................ 183 Introduction......................................................................................................... 183 Structure............................................................................................................... 183 Objectives............................................................................................................. 184

CI/CD pipeline for ML...................................................................................... 184



Continuous Delivery/Deployment (CD)........................................................ 186



Continuous Integration (CI).............................................................................. 185 Continuous Training (CT)................................................................................. 186 Introduction to Jenkins...................................................................................... 187

Installation...................................................................................................... 187

Build CI/CD pipeline using GitHub, Docker, and Jenkins.......................... 189



Create a Personal Access Token (PAT) on GitHub......................................... 196



Develop codebase............................................................................................. 189 Create a webhook on the GitHub repository................................................... 197



Configure Jenkins............................................................................................... 199



Stage 1: 1-GitHub-to-container...................................................................... 203



Create CI/CD pipeline using Jenkins.............................................................. 202 Stage 2: 2-training.......................................................................................... 205 Stage 3: 3-testing............................................................................................ 207 Stage 4: 4-deployment-status-email............................................................... 210

Conclusion........................................................................................................... 217

Points to remember............................................................................................ 217 Multiple choice questions.................................................................................. 218

Answers.......................................................................................................... 218 Questions............................................................................................................. 218

xx



10. Deploying ML Models on Heroku....................................................................... 219 Introduction......................................................................................................... 219 Structure............................................................................................................... 219 Objectives............................................................................................................. 220 Heroku.................................................................................................................. 220

Setting up Heroku.............................................................................................. 221



Deployment with GitHub repository integration.......................................... 222



Deployment with Heroku Git........................................................................... 222 REVIEW APPS.............................................................................................. 223

STAGING....................................................................................................... 223 PRODUCTION............................................................................................. 224

Heroku Pipeline flow....................................................................................... 224



Deployment with Container Registry............................................................. 225



Configuration.................................................................................................. 226



GitHub Actions................................................................................................... 225



CI/CD pipeline using GitHub Actions and Heroku..................................... 227



Points to remember............................................................................................ 246

Conclusion........................................................................................................... 246

Multiple choice questions.................................................................................. 247

Answers.......................................................................................................... 247 Questions............................................................................................................. 247 11. Deploying ML Models on Microsoft Azure........................................................ 249 Introduction......................................................................................................... 249 Structure............................................................................................................... 249 Objectives............................................................................................................. 250 Azure.................................................................................................................... 250

Set up an Azure account.................................................................................... 251



Infrastructure setup........................................................................................ 263



Deployment using GitHub Actions................................................................. 251



Azure Container Registry.................................................................................... 263



Azure App Service................................................................................................ 267



GitHub Actions.............................................................................................. 270



xxi



Service principal............................................................................................. 273



Deployment using Azure DevOps and Azure ML........................................ 278



Configure Azure App Service to use GitHub Actions for CD....................... 274

Azure Machine Learning (AML) service......................................................... 279

Workspace....................................................................................................... 280 Experiments.................................................................................................... 280 Runs................................................................................................................ 280

Configure CI pipeline......................................................................................... 290 Configure CD pipeline....................................................................................... 292

Conclusion........................................................................................................... 299

Points to remember............................................................................................ 299 Multiple choice questions.................................................................................. 299

Answers.......................................................................................................... 300 Questions............................................................................................................. 300 12. Deploying ML Models on Google Cloud Platform........................................... 301 Introduction......................................................................................................... 301 Structure............................................................................................................... 301 Objectives............................................................................................................. 302

Google Cloud Platform (GCP).......................................................................... 302



Cloud Source Repositories................................................................................ 304



Container Registry.............................................................................................. 311



Set up the GCP account.................................................................................. 303

Cloud Build......................................................................................................... 309

Kubernetes........................................................................................................... 312

Google Kubernetes Engine (GKE).................................................................... 313



CI/CD pipeline using Cloud Build.................................................................. 317



Deployment using Cloud Shell – Manual Trigger......................................... 316 Create a trigger in Cloud Build...................................................................... 318

Conclusion........................................................................................................... 323

Points to remember............................................................................................ 323 Multiple choice questions.................................................................................. 323

Answers.......................................................................................................... 324

xxii



Questions............................................................................................................. 324 13. Deploying ML Models on Amazon Web Services............................................. 325 Introduction......................................................................................................... 325 Structure............................................................................................................... 325 Objectives............................................................................................................. 326

Introduction to Amazon Web Services (AWS)................................................ 326



Amazon Elastic Compute Cloud (EC2).......................................................... 327



AWS compute services.................................................................................... 326



Amazon Elastic Container Service (ECS)........................................................ 327



Amazon Elastic Container Registry (ECR)..................................................... 327



Amazon Elastic Kubernetes Service (EKS)..................................................... 327 AWS Fargate................................................................................................... 328 AWS Lambda.................................................................................................. 328



Amazon SageMaker........................................................................................... 328



AWS CodeCommit............................................................................................. 331



Set up an AWS account...................................................................................... 329 Continuous Training...................................................................................... 336



Amazon Elastic Container Registry (ECR)..................................................... 336



AWS CodeBuild.................................................................................................. 339



Amazon Elastic Container Service (ECS)........................................................ 346



EC2 instance........................................................................................................ 347



Docker Hub rate limit..................................................................................... 337 Attach container registry access to CodeBuild’s service role.......................... 345 AWS ECS deployment models........................................................................ 347

Fargate.................................................................................................................. 348



Task definition................................................................................................. 349



Running task with the task definition.................................................................. 350



Amazon VPC and subnets................................................................................... 351



Load balancing................................................................................................ 352



Target group......................................................................................................... 353



Security Groups................................................................................................... 356



Application Load Balancers (ALB)...................................................................... 358



xxiii

Service............................................................................................................. 363

CI/CD pipeline using CodePipeline............................................................... 369 AWS CodePipeline.......................................................................................... 371

Monitoring........................................................................................................... 378 Conclusion........................................................................................................... 379

Points to remember............................................................................................ 379 Multiple choice questions.................................................................................. 380

Answers.......................................................................................................... 380 Questions............................................................................................................. 380 14. Monitoring and Debugging................................................................................... 381 Introduction......................................................................................................... 381 Structure............................................................................................................... 381 Objectives............................................................................................................. 382

Importance of monitoring................................................................................. 382



Metrics for monitoring your ML system......................................................... 385



Fundamentals of ML monitoring..................................................................... 383 Drift in ML........................................................................................................... 386 Types of drift in ML........................................................................................ 386 Techniques to detect the drift in ML............................................................... 388 Addressing the drift in ML............................................................................. 389

Operational monitoring with Prometheus and Grafana............................... 390 ML model monitoring with whylogs and WhyLabs..................................... 402

whylogs........................................................................................................... 403

Constraints for data quality validation.......................................................... 404

WhyLabs......................................................................................................... 407 Conclusion........................................................................................................... 416

Points to remember............................................................................................ 416 Multiple choice questions.................................................................................. 417

Answers.......................................................................................................... 417 Questions............................................................................................................. 417

xxiv



15. Post-Productionizing ML Models......................................................................... 419 Introduction......................................................................................................... 419 Structure............................................................................................................... 419 Objectives............................................................................................................. 420

Bridging the gap between the ML model and the creation of business value..................................................................................................... 420



Adversarial attack........................................................................................... 421



Model security..................................................................................................... 420 Data poisoning attack..................................................................................... 422 Distributed Denial of Service attack (DDoS)................................................. 422 Data privacy attack......................................................................................... 422 Mitigate the risk of model attacks................................................................... 423

A/B testing.......................................................................................................... 423 MLOps is the future........................................................................................... 424

Conclusion........................................................................................................... 425

Points to remember............................................................................................ 425 Multiple choice questions.................................................................................. 425

Answers.......................................................................................................... 426 Questions............................................................................................................. 426 Index....................................................................................................................427-434

Python 101



1

Chapter 1

Python 101

Introduction

Python 101 guides you with everything from the installation of Python to data manipulation in Pandas DataFrame. In a nutshell, this chapter is designed to equip you with the prerequisites required for this book. It is a quick refresher for those who have worked with Python. And if you are a beginner, this chapter is going to cover the most common Python commands required to build and deploy machine models.

Structure

• This chapter covers the following topics: • Installation of Python • Hello World! • Data structures • Control statements and loops • Functions • OOPs • NumPy • Pandas

2



Machine Learning in Production

Objectives

After studying this chapter, you should be able to install Python on your machine, and store and manage data using common data structures in Python. You should be able to use Python's data processing packages for data manipulation, and you should also know how to create classes, methods, and objects.

Install Python

Make sure the Python version you are installing is compatible with the libraries and applications you will work on. Also, maintain the same Python version throughout the project to avoid any errors or exceptions. Here, you will install Python version 3.6.10.

On Windows and Mac OS Here’s the link to install Python 3.6.10:

https://www.python.org/downloads/release/python-3610/

On Linux

Ubuntu 16.10 and 17.04: sudo apt update sudo apt install python3.6

Ubuntu 17.10, 18.04 (Bionic) and onward: Ubuntu 17.10 and 18.04 already come with Python 3.6 as default. Just run python3 in the terminal to invoke it.

Install Anaconda

Anaconda Individual Edition contains conda and Anaconda Navigator, as well as Python and hundreds of scientific packages. When you install Anaconda, all of these will also get installed. https://docs.anaconda.com/anaconda/install/ Note: Review the system requirements listed on the site before installing Anaconda Individual Edition.

Python 101



3

Install code editor

Any of the following code editors can be chosen; however, the preferred one is Visual Studio Code: Visual Studio Code (https://code/visualstudio.com) Sublime Text (https://www.sublimetext.com) Notepad++ (https://notepad-plus-plus.org/downloads/)

Hello World!

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its simple, easy-to-learn syntax emphasizes readability and therefore, reduces the cost of program maintenance. Python supports modules and packages, which encourages modular programming and code reusability. It is developed under an OSI-approved open-source license, making it freely usable and distributable, even for commercial use. The Python Package Index (PyPI) hosts thousands of third-party modules for Python. Open the terminal and type a python to open the Python console. Now you are in the Python console. Example: 1.1 Hello world in Python 1. print("Hello World") 2. Hello World

To come out of the console you can use: 1. exit()

Execution of Python file

Let’s create a file named hello_world.py and add the print("Hello World") to it. Now, run the file in the terminal: python hello_world.py

You should get the following output: Hello World

4



Machine Learning in Production

Data structures

Data structures are nothing but particular ways of storing and managing data in memory so that it can be easily accessed and modified later. Python comes with a comprehensive set of data structures, which play an important role in programming because they are reusable, easily accessible, and manageable.

Common data structures

You will study some of the common data structures in this section.

Array Arrays are collections of homogeneous items. One can use the same data type in a single array. Example: 1.2 Array data type

1. import array as arr 2. 3. my_array = arr.array("i", (1, 2, 3, 4, 5)) 4. 5. print(my_array) 6. array('i', [1, 2, 3, 4, 5]) 7. 8. print(my_array[1]) 9. 2

Dictionary Dictionaries are defined as comma separated key:value pairs enclosed in curly braces. Example: 1.3 Dictionary data type 1. my_dict = {'name': 'Adam', 'emp_id': 3521, 'dept':'Marketing'} 2. print(my_dict['name']) 3. Adam

List It is a collection of heterogeneous items enclosed in square brackets. One can use the same or different data types in a list.

Python 101



5

Example: 1.4 List data type 1. my_list=['apple', 4, 'banana', 6] 2. print(my_list[0]) 3. apple

Set A set is a collection of unique (non-duplicate) elements enclosed in curly braces. A set can take heterogeneous elements. Example: 1.5 Set data type 1. my_set = {3,5,6} 2. print(my_set) 3. {3, 5, 6} 1. my_set = {1, 2.0, (3,5,6), 'Test'} 2. print(my_set) 3. {1, 2.0, 'Test', (3, 5, 6)}

String A string is used to store text data. It can be represented by single quotes ('') or double quotes (""). Example: 1.6 String data type 1. print('The cat was chasing the mouse') 2. The cat was chasing the mouse

Tuple Tuples are collections of elements surrounded by round brackets (optional), and they are immutable, that is, they cannot be changed. Example: 1.7 Tuple data type 1. my_tuple_1 = ('apple', 4, 'banana', 6) 2. print(my_tuple_1[0]) 3. apple

Tuple can be declared without round brackets, as follows: 1. my_tuple_2 = 1, 2.0, (3,5,6), 'Test' 2. print(my_tuple_2[0]) 3. 1

6



Machine Learning in Production

Control statements and loops

Python has several types of control statements and loops. Let’s look at them.

if…else

The if statement executes a block of code if the specified condition is true. Whereas, the else statement executes a block of code if the specified condition is false. Furthermore, you can use the elif (else if) statement to check the new condition if the first condition is false. Syntax:

if condition1: Code to be executed elif condition2: Code to be executed else: Code to be executed Example: 1.8 If…else control statement 1. x = 26 2. y = 17 3. 4. if(x > y): 5.

print('x is greater than y')

6. elif(x == y): 7.

print('x and y are equal')

8. else: 9.

print('y is greater than x')

10. 11. x is greater than y

for loop

It is used when you want to iterate over a sequence or iterable such as a string, list, dictionary, or tuple. It will execute a block of code a certain number of times. Syntax:

Python 101



7

for val in sequence: Code to be executed Example: 1.9 For loop 1. num = 2 2. for i in range(1, 11): 3.

print(num, 'X', i, '=', num*i)

4. 5. 2 X 1 = 2 6. 2 X 2 = 4 7. 2 X 3 = 6 8. 2 X 4 = 8 9. 2 X 5 = 10 10. 2 X 6 = 12 11. 2 X 7 = 14 12. 2 X 8 = 16 13. 2 X 9 = 18 14. 2 X 10 = 20

In the preceding example, the range() function generates a sequential set of numbers using start, stop, and step parameters.

while loop

It is used when you want to execute a block of code indefinitely until the specified condition is met. Furthermore, you can use an else statement to execute a block of code once when the first condition is false. Syntax: while condition: Code to be executed Example: 1.10 While loop 1. num = 1 2. while num