Unit-2
Machinelearning Model Development and Deployment
Best Practices in Machine learning model Deployment:
What is Model Deployment in Machine Learning?
What Does Deployment Mean?
In machine learning, deployment means making your model available so others can
use it. For example, sharing your project on GitHub allows others to download it, run
the model, and use it.
Deploying machine learning (ML) models into production environments is crucial
for making their predictive capabilities accessible to users or other systems. This
guide provides an in-depth look at the essential steps, strategies, and best practices
for ML model deployment.
Importance of Model Deployment
Deployment transforms theoretical models into practical tools that can generate
insights and drive decisions in real-world applications. For example, a deployed
fraud detection model can analyze transactions in real-time to prevent fraudulent
activities.
Key Steps in Model Deployment
Pre-Deployment Preparation
To prepare for model deployment, it's crucial to select a model that meets both
performance and production requirements.
1. Pre-Deployment Preparation
2. Model Packaging
3. Model Integration
4. Model Monitoring and Management
This entails ensuring that the model achieves the desired metrics, such as accuracy
and speed, and is scalable, reliable, and maintainable in a production environment.
Thorough evaluation and testing of the model using validation data are essential to
address any issues before deployment. Additionally, setting up the production
environment is key, including ensuring the availability of necessary hardware
resources like CPUs and GPUs, installing required software dependencies, and
configuring environment settings to match deployment requirements. Implementing
security measures, monitoring tools, and backup procedures is also vital to protect
the model and data, track performance, and recover from failures. Documentation of
the setup and configuration is recommended for future reference and
troubleshooting.
Deployment Strategies
Mainly we used to need to focus these strategies:
1. Shadow Deployment
2. Canary Deployment
3. A/B Testing
Shadow Deployment involves running the new model alongside the existing one
without affecting production traffic. This allows for a comparison of their
performances in a real-world setting. It helps to ensure that the new model meets the
required performance metrics before fully deploying it.
Canary Deployment is a strategy where the new model is gradually rolled out to a
small subset of users, while the majority of users still use the existing model. This
allows for monitoring the new model's performance in a controlled environment
before deploying it to all users. It helps to identify any issues or performance issues
early on.
A/B Testing involves deploying different versions of the model to different user
groups and comparing their performance. This allows for evaluating which version
performs better in terms of metrics such as accuracy, speed, and user satisfaction. It
helps to make informed decisions about which model version to deploy for all users.
Challenges in Model Deployment
1. Scalability Issues
2. Latency Constraints
3. Model Retraining and Updating
To address scalability issues, ensure that the model architecture is designed to
handle increased traffic and data volume. Consider using distributed computing
techniques such as parallel processing and data partitioning, and use scalable
infrastructure such as cloud services that can dynamically allocate resources based
on demand. For meeting latency constraints, optimize the model for inference speed
by using efficient algorithms and model architectures. Deploy the model on
hardware accelerators like GPUs or TPUs and use caching and pre-computation
techniques to reduce latency for frequently requested data. To manage model
retraining and updating, implement an automated pipeline for continuous integration
and continuous deployment (CI/CD). This pipeline should include processes for data
collection, model retraining, evaluation, and deployment. Use version control for
models and data to track changes and roll back updates if necessary. Monitor the
performance of the updated model to ensure it meets the required metrics.
Machine Learning System Architecture for Model Deployment
A typical machine learning system architecture for model deployment involves
several key components. Firstly, the data pipeline is responsible for collecting,
preprocessing, and transforming data for model input. Next, the model training
component uses this data to train machine learning models, which are then evaluated
and validated. Once a model is trained and ready for deployment, it is deployed
using a serving infrastructure such as Kubernetes or TensorFlow Serving.
The serving infrastructure manages the deployment of models, handling requests
from clients and returning model predictions. Monitoring and logging components
track the performance of deployed models, capturing metrics such as latency,
throughput, and model accuracy. These metrics are used to continuously improve
and optimize the deployed models.
Additionally, a model management component manages the lifecycle of models,
including versioning, rollback, and A/B testing. This allows for the seamless
deployment of new models and the ability to compare the performance of different
model versions.
Overall, this architecture enables the efficient deployment and management of
machine learning models in production, ensuring scalability, reliability, and
performance.
Tools and Platforms for Model Deployment
Here are some poplur tools for deployement:
1. Kubernetes.
2. Kubeflow
3. MLflow
4. TensorFlow Serving
Kubernetes is a container orchestration platform that manages containerized
applications, ensuring scalability and reliability by automating the deployment,
scaling, and management of containerized applications.
Kubeflow is a machine learning toolkit built on top of Kubernetes that provides a
set of tools for deploying, monitoring, and managing machine learning models in
production. It simplifies the process of deploying and managing ML models on
Kubernetes.
MLflow is an open-source platform for managing the end-to-end machine learning
lifecycle. It provides tools for tracking experiments, packaging code, and managing
models, enabling reproducibility and collaboration in ML projects.
TensorFlow Serving is a flexible and efficient serving system for deploying
TensorFlow models in production. It allows for easy deployment of TensorFlow
models as microservices, with support for serving multiple models simultaneously
and scaling based on demand.
Best Practices For Machine learning deployment
1. Automated Testing.
2. Version Control
3. Security Measures
How to Deploy ML Models
Develop and Create a Model in a Training Environment:
Build your model in an offline training environment using training data. ML teams
often create multiple models, but only a few make it to deployment.
Optimize and Test Code:Ensure that your code is of high quality and can be
deployed. Clean and optimize the code as necessary, and test it thoroughly to ensure
it functions correctly in a live environment.
Prepare for Container Deployment:
Containerize your model before deployment. Containers are predictable, repeatable,
and easy to coordinate, making them ideal for deployment. They simplify
deployment, scaling, modification, and updating of ML models.
Plan for Continuous Monitoring and Maintenance:
Implement processes for continuous monitoring, maintenance, and governance of
your deployed model. Continuously monitor for issues such as data drift,
inefficiencies, and bias. Regularly retrain the model with new data to keep it
effective over time.
Deploy a Machine Learning Model using Streamlit Library
Machine Learning:
A computer is able to learn from experience without being explicitly programmed.
Machine Learning is one of the top fields to enter currently and top companies all
over the world are using it for improving their services and products. But there is no
use of a Machine Learning model which is trained in your Jupyter Notebook. And
so we need to deploy these models so that everyone can use them. In this article, we
will first train an Iris Species classifier and then deploy the model using Streamlit
which is an open-source app framework used to deploy ML models easily.
Streamlit Library:
Streamlit lets you create apps for your machine learning project using simple python
scripts. It also supports hot-reloading, so that your app can update live as you edit
and save your file. An app can be built in a few lines of code only(as we will see
below) using the Streamlit API. Adding a widget is the same as declaring a variable.
There is no need to write a backend, define different routes or handle HTTP
requests. It is easy to deploy and manage. More information can be found on their
website – https://www.streamlit.io/ (check this website)
So first we will train our model. We will not do much preprocessing as the main aim
of this article is not to make an accurate ML model but to show its deployment.
Firstly we need to install the following –
pip install pandas
pip install numpy
pip install sklearn
pip install streamlit
Code:
import pandas as pd
import numpy as np
df = pd.read_csv('iris.csv')
df.head()
Output:
Now we drop the Id column first as it is not important for classifying the Iris
species. Then we will split the dataset into training and testing dataset and will use a
Random Forest Classifier. You can use any other classifier of your choice, for
example, logistic regression, support vector machine, etc.
Code:
# Dropping the Id column
df.drop('Id', axis = 1, inplace = True)
# Renaming the target column into numbers to aid training of the model
df['Species']= df['Species'].map({'Iris-setosa':0, 'Iris-versicolor':1, 'Iris-virginica':2})
# splitting the data into the columns which need to be trained(X) and the target
column(y)
X = df.iloc[:, :-1]
y = df.iloc[:, -1]
# splitting data into training and testing data with 30 % of data as testing data
respectively
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state
= 0)
# importing the random forest classifier model and training it on the dataset
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier()
classifier.fit(X_train, y_train)
# predicting on the test dataset
y_pred = classifier.predict(X_test)
# finding out the accuracy
from sklearn.metrics import accuracy_score
score = accuracy_score(y_test, y_pred)
We get an accuracy of 95.55% which is pretty good.
Now, in order to use this model to predict other unknown data, we need to save it.
We can save it by using pickle, which is used for serializing and deserializing a
Python object structure.
Code:
# pickling the model
import pickle
pickle_out = open("classifier.pkl", "wb")
pickle.dump(classifier, pickle_out)
pickle_out.close()
There will be a new file created called “classifier.pkl” in the same directory. Now
we can get down to using Streamlit to deploy the model –
Paste the below code into another python file.
Code:
import pandas as pd
import numpy as np
import pickle
import streamlit as st
from PIL import Image
# loading in the model to predict on the data
pickle_in = open('classifier.pkl', 'rb')
classifier = pickle.load(pickle_in)
def welcome():
return 'welcome all'
# defining the function which will make the prediction using
# the data which the user inputs
def prediction(sepal_length, sepal_width, petal_length, petal_width):
prediction = classifier.predict(
[[sepal_length, sepal_width, petal_length, petal_width]])
print(prediction)
return prediction
# this is the main function in which we define our webpage
def main():
# giving the webpage a title
st.title("Iris Flower Prediction")
# here we define some of the front end elements of the web page like
# the font and background color, the padding and the text to be displayed
html_temp = """
<div style ="background-color:yellow;padding:13px">
<h1 style ="color:black;text-align:center;">Streamlit Iris Flower Classifier
ML App </h1>
</div>
"""
# this line allows us to display the front end aspects we have
# defined in the above code
st.markdown(html_temp, unsafe_allow_html = True)
# the following lines create text boxes in which the user can enter
# the data required to make the prediction
sepal_length = st.text_input("Sepal Length", "Type Here")
sepal_width = st.text_input("Sepal Width", "Type Here")
petal_length = st.text_input("Petal Length", "Type Here")
petal_width = st.text_input("Petal Width", "Type Here")
result =""
# the below line ensures that when the button called 'Predict' is clicked,
# the prediction function defined above is called to make the prediction
# and store it in the variable result
if st.button("Predict"):
result = prediction(sepal_length, sepal_width, petal_length, petal_width)
st.success('The output is {}'.format(result))
if __name__=='__main__':
main()
You can run this by typing the following command in the terminal –
streamlit run app.py
app.py is the name of the file where we wrote the Streamlit code.
The website will open in your browser and then you can test it. This
method can be used to deploy other machine and deep learning models too.