SlideShare a Scribd company logo
1 of 38
Download to read offline
Deploying
Machine Learning Models
To Production
1
Anass BENSRHIR
11-05-2017
Outline
2
•Typical ML Flow
•Different Strategies to deploy Machine Learning Models
•Verdict and Comparison
Who am I ?
• Founder et Senior Data Scientist
@Bolddata
• Big Data Project Leader
@Schlumberger
• Msc Advanced Systems &
Machine Learning @CentraleParis
anass@bolddata.net
@anassbensrhir
@abensrhir
3
Typical Machine Learning Flow
4
Source : http://blog.cloudera.com/blog/2016/02/how-to-predict-telco-churn-with-apache-spark-mllib/
Typical ML Model
5
from sklearn.datasets import load_iris
import numpy as np
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
iris = load_iris()
# Create a dataframe with the four feature variables
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
df['is_train'] = np.random.uniform(0, 1, len(df)) <= .75
train, test = df[df['is_train']==True], df[df['is_train']==False]
features = df.columns[:4]
y = pd.factorize(train['species'])[0]
model = RandomForestClassifier(n_jobs=2)
model.fit(train[features], y)
model.predict(test[features])
One Problem
6
“Might Work well for KAGGLE!
But Kaggle isn’t real world Machine learning!”
>>>Interpretability + Low Complexity
+ Speed
Accuracy = 0.81
Speed = 30ms
Accuracy
Accuracy = 0.91
Speed = 3s
Life Cycle of Real World ML in Production
Deployment
Management
Evaluation
Monitoring
Pickling
9
import cPickle as Pickle
with open(“mymodel.pkl”, “wb”) as mymodelfile:
Pickle.dump(model, mymodelfile)
with open(“mymodel.pkl”, “rb”) as mymodelfile:
thenewmodel = Pickle.load(mymodelfile)
thenewmodel.predict(newvector)
With Sklearn’s Joblib
10
from sklearn.externals import joblib
joblib.dump(model,"model.joblib", compress=1) # compression into 1 file
thenewmodel = joblib.load(“model.joblib")
thenewmodel.predict(newvector)
Pickle Vs Joblib Performance
TimetoLoadtheModel
0s
0.2s
0.4s
0.6s
0.8s
Loading The Model
0.72s
0.23s
Joblib Pickle
ModelFileSize
0kb
12.5kb
25kb
37.5kb
50kb
File Size
48 kb
4.7 kb
Joblib Pickle
*The Same Model
Verdict
12
GOOD BAD
• Consistant way to save time
and reuse the same model
everywhere.
• Fast !
• Might not work if Sklearn and
python versions are different
from saving to loading
environments.
• DevOPS nightmare.
The END ?
Popularity of programming languages index
(TIOBE)
14
Source : https://www.tiobe.com/tiobe-index/
5
Cost vs Technological Benefit Tradeoff
15
Cost $
Technological Benefit
Native Java / C++ ..
models
Rebuild the whole
stack to Python
API Powered
Model
Hybrid Approach
PMML
Native Java
/ C++ Models
16
Native Libraries
• Mostly used on legacy systems (Old CRM’s, Banking….) or High
Frequency Trading strategies.
• If used correctly, they are Fast
• Entire List of Libraries : https://github.com/josephmisiti/awesome-machine-learning
• C++ :
• LightGBM (https://github.com/Microsoft/LightGBM)
• MLPack (http://www.mlpack.org/)
• Caffe/CUDA (deeplearning) (http://caffe.berkeleyvision.org/)
• Java :
• Aerosolve (https://github.com/airbnb/aerosolve)
• H2O (https://github.com/h2oai/h2o-3)
• Weka (http://www.cs.waikato.ac.nz/ml/weka/)
Native Java/C++ Verdict
18
GOOD BAD
• Used on High Frequency
Trading floors where speed
trumps usability and agility.
• Faster !!!
• No use of Scikit-learn / pandas
Data science libraries
• Limitation of available algorithms
• Difficult and Costly ($$)
• Does anybody know a Data
scientist who works exclusively on
Java or C++ ? (they are all in New
York)
Model Export
“Pmml”
19
PMML (Predictive Model Markup)
PMML stands for "Predictive Model Markup Language". It is the de facto
standard to represent predictive solutions. A PMML file may contain a myriad
of data transformations (pre- and post-processing) as well as one or more
predictive models.
Because it is a standard, PMML allows for different statistical and data mining
tools to speak the same language. In this way, a predictive solution can be
easily moved among different tools and applications without the need for
custom coding. For example, it may be developed in one application and
directly deployed on another.
20
Pmml XML File
PMML Pipeline
Scikit-learn
model
PMML
File
Export as PMML
sklearn2pmml
PMML
File
Import as PMML
Knime
Weka
R
SAS
C++
Java
General
Purpose
APP
Use the Model
sklearn2pmml : https://github.com/jpmml/sklearn2pmml
Java PMML Library : https://github.com/jpmml
Apache Spark PMML : https://github.com/jpmml/jpmml-spark
22
model.pmml
model.pmml
Python Code (Simplified)
23
import pandas
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn2pmml import PMMLPipeline
iris = load_iris()
# Create a dataframe with the four feature variables
iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_pipeline = PMMLPipeline([
("classifier", RandomForestClassifier())
])
iris_pipeline.fit(iris_df[iris_df.columns.difference(["Species"])], iris_df["Species"])
from sklearn2pmml import sklearn2pmml
sklearn2pmml(iris_pipeline, “RandomForestClassifier_Iris.pmml”)
Java Code (So much Simplified)
24
// Load the file using our simple util function.
PMML pmml = JPMMLUtils.loadModel( “RandomForestClassifier_Iris.pmml” );
// Now we need a prediction evaluator for the loaded model
PMMLManager mgr = new PMMLManager( pmml );
ModelEvaluator modelEvaluator = (ModelEvaluator) mgr.getModelManager(modelName,
ModelEvaluatorFactory.getInstance());
Evaluator evaluator = modelEvaluator;
…
…
…
Map results = evaluator.evaluate( features ); // prediction happens here
Use PMML in Spark
25
spark-submit --master local --class org.jpmml.spark.EvaluationExample example-1.0-SNAPSHOT.jar
RandomForestClassifier_Iris.pmml Iris.csv /tmp/output/
example-1.0-SNAPSHOT.jar contains a java code to import the CSV and the pmml model
Verdict
26
GOOD BAD
• Interoperable
• Use Python data science
stack and deploy everywhere
• No Agility nor sustainability
• Not Every ML algorithm is
available
• PMML Files Are BIG
(Gigabytes…)
• Need to use unit tests and match
python output with new output =
Slow deployment
Api Powered
Model
27
Flask - Scikit-learn Model
28
Scikit-learn
model FLASK Nginx
Features Vector
(x)
Predicted Value
(y)
Request
Response
Request : x {a = 1, b=3.4, c=3}
Response : status = 200 , y {predicted= “setosa”}
Web Request/response
Application / Webapp / Mobile App
Json POST Request
29
curl -H "Content-Type: application/json”
-X POST
-d ‘{“a":1,"b":2, “c”:4}’
http://localhost:5000/api/1.0/predict
Better With Security Enabled
curl -H "Content-Type: application/json”
-H "Authorization: Bearer <ACCESS_TOKEN>"
-X POST
-d ‘{“a":1,"b":2, “c”:4}’
http://localhost:5000/api/1.0/predict
Flask Code (Simplified)
30
from flask import Flask, request, jsonify
from config import VERSION
from mycustommodel import model2 as model
app = Flask(__name__)
@app.route('/api/{version}/predict'.format(version=VERSION), methods=['POST'])
def predict():
request = request.get_json(silent=True)
a = request.get('a')
b = request.get('b')
c = request.get('c')
prediction = model.predict([a, b, c])
response = dict(status="ok", prediction=prediction)
return jsonify(response)
Python Web Frameworks Benchmark
31
• Results for Loading and
returning a json object.
• Falcon and Flask have the
best Speed/Usability
Tradeoff
32
A RealWorld Architecture
MongoDB Document
33
{
"_id" : ObjectId("4f693d40e4b04cde19f17205"),
"hostname" : “ec2-203-0-113-25.compute-1.amazonaws.com",
“user_id" : “19846”,
“prediction_id" : “3f6dcfe0-f0ac-4e94-ac46-35c1ce8d59f8”, #For traceability
"model_version": "1.0",
"request_features" : {
"a": 1.5,
"b": 0,
"c": 3.2
},
"response_prediction" {
"class": "setosa",
"probability": 0.701
},
"requested_at": ISODate("2017-03-10T10:50:42.389Z"),
"predicted_at": ISODate("2017-03-10T10:50:43.132Z"),
}
Nginx Config File
34
# Define your "upstream" servers - the
# servers request will be sent to
upstream app_example {
least_conn; # Use Least Connections strategy
server 192.168.1.19:5000; # Flask Server 1
server 192.168.1.19:5001; # Flask Server 1 Model 2
server 192.168.1.20:5000; # Flask Server 2
server 192.168.1.21:5000; # Flask Server 3
}
server {
listen 80;
server_name model.example.com
# pass the request to Flask Gunicorn server
# with some correct headers for proxy-awareness
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
}
2 strategies
Model 1
Model 2
Sample 1
Sample 2
3000 visits
10% conversion
3000 visits
40% conversion
Use
Model 2
Everywhere
Strategie 1 : Use different models as they are, and update the models with new data afterwards
Strategie 2 : Use A/B Testing strategy to serve the best performing model to the whole sample.
Real-Time Monitoring with Grafana
Verdict
37
GOOD BAD
• SCALABLE
• Interoperable (can be used both by
backend and frontend languages think :
javascript !)
• Agile : models can be put on production
very fast and with no code change, simple
as launching a new server instance
• As Fast as you need it to be (add new
servers or docker containers)
• Did i say Agile ?
• The Infrastructure can
become overwhelming and
costly over time.
Other Options ?
• PredictionIO (http://prediction.io)
• Seldon (https://www.seldon.io/) Interesting !
• Oryx (http://oryx.io) Built for Hadoop

More Related Content

What's hot

Machine Learning with PyCarent + MLflow
Machine Learning with PyCarent + MLflowMachine Learning with PyCarent + MLflow
Machine Learning with PyCarent + MLflowDatabricks
 
MLOps - The Assembly Line of ML
MLOps - The Assembly Line of MLMLOps - The Assembly Line of ML
MLOps - The Assembly Line of MLJordan Birdsell
 
MLOps Using MLflow
MLOps Using MLflowMLOps Using MLflow
MLOps Using MLflowDatabricks
 
DataOps: An Agile Method for Data-Driven Organizations
DataOps: An Agile Method for Data-Driven OrganizationsDataOps: An Agile Method for Data-Driven Organizations
DataOps: An Agile Method for Data-Driven OrganizationsEllen Friedman
 
Vertex AI: Pipelines for your MLOps workflows
Vertex AI: Pipelines for your MLOps workflowsVertex AI: Pipelines for your MLOps workflows
Vertex AI: Pipelines for your MLOps workflowsMárton Kodok
 
Apache Kafka® and the Data Mesh
Apache Kafka® and the Data MeshApache Kafka® and the Data Mesh
Apache Kafka® and the Data MeshConfluentInc1
 
AI and Big Data Analytics
AI and Big Data AnalyticsAI and Big Data Analytics
AI and Big Data AnalyticsInData Labs
 
Simplifying Model Management with MLflow
Simplifying Model Management with MLflowSimplifying Model Management with MLflow
Simplifying Model Management with MLflowDatabricks
 
Machine Learning & Amazon SageMaker
Machine Learning & Amazon SageMakerMachine Learning & Amazon SageMaker
Machine Learning & Amazon SageMakerAmazon Web Services
 
MLflow: A Platform for Production Machine Learning
MLflow: A Platform for Production Machine LearningMLflow: A Platform for Production Machine Learning
MLflow: A Platform for Production Machine LearningMatei Zaharia
 
Introduction to Knowledge Graphs and Semantic AI
Introduction to Knowledge Graphs and Semantic AIIntroduction to Knowledge Graphs and Semantic AI
Introduction to Knowledge Graphs and Semantic AISemantic Web Company
 
From Data Science to MLOps
From Data Science to MLOpsFrom Data Science to MLOps
From Data Science to MLOpsCarl W. Handlin
 
Machine Learning Models in Production
Machine Learning Models in ProductionMachine Learning Models in Production
Machine Learning Models in ProductionDataWorks Summit
 
Data Engineering.pdf
Data Engineering.pdfData Engineering.pdf
Data Engineering.pdfDatacademy.ai
 
Architect’s Open-Source Guide for a Data Mesh Architecture
Architect’s Open-Source Guide for a Data Mesh ArchitectureArchitect’s Open-Source Guide for a Data Mesh Architecture
Architect’s Open-Source Guide for a Data Mesh ArchitectureDatabricks
 
Ml ops intro session
Ml ops   intro sessionMl ops   intro session
Ml ops intro sessionAvinash Patil
 
MLOps Bridging the gap between Data Scientists and Ops.
MLOps Bridging the gap between Data Scientists and Ops.MLOps Bridging the gap between Data Scientists and Ops.
MLOps Bridging the gap between Data Scientists and Ops.Knoldus Inc.
 
Introducing MlFlow: An Open Source Platform for the Machine Learning Lifecycl...
Introducing MlFlow: An Open Source Platform for the Machine Learning Lifecycl...Introducing MlFlow: An Open Source Platform for the Machine Learning Lifecycl...
Introducing MlFlow: An Open Source Platform for the Machine Learning Lifecycl...DataWorks Summit
 
Koalas: How Well Does Koalas Work?
Koalas: How Well Does Koalas Work?Koalas: How Well Does Koalas Work?
Koalas: How Well Does Koalas Work?Databricks
 

What's hot (20)

Machine Learning with PyCarent + MLflow
Machine Learning with PyCarent + MLflowMachine Learning with PyCarent + MLflow
Machine Learning with PyCarent + MLflow
 
MLOps for production-level machine learning
MLOps for production-level machine learningMLOps for production-level machine learning
MLOps for production-level machine learning
 
MLOps - The Assembly Line of ML
MLOps - The Assembly Line of MLMLOps - The Assembly Line of ML
MLOps - The Assembly Line of ML
 
MLOps Using MLflow
MLOps Using MLflowMLOps Using MLflow
MLOps Using MLflow
 
DataOps: An Agile Method for Data-Driven Organizations
DataOps: An Agile Method for Data-Driven OrganizationsDataOps: An Agile Method for Data-Driven Organizations
DataOps: An Agile Method for Data-Driven Organizations
 
Vertex AI: Pipelines for your MLOps workflows
Vertex AI: Pipelines for your MLOps workflowsVertex AI: Pipelines for your MLOps workflows
Vertex AI: Pipelines for your MLOps workflows
 
Apache Kafka® and the Data Mesh
Apache Kafka® and the Data MeshApache Kafka® and the Data Mesh
Apache Kafka® and the Data Mesh
 
AI and Big Data Analytics
AI and Big Data AnalyticsAI and Big Data Analytics
AI and Big Data Analytics
 
Simplifying Model Management with MLflow
Simplifying Model Management with MLflowSimplifying Model Management with MLflow
Simplifying Model Management with MLflow
 
Machine Learning & Amazon SageMaker
Machine Learning & Amazon SageMakerMachine Learning & Amazon SageMaker
Machine Learning & Amazon SageMaker
 
MLflow: A Platform for Production Machine Learning
MLflow: A Platform for Production Machine LearningMLflow: A Platform for Production Machine Learning
MLflow: A Platform for Production Machine Learning
 
Introduction to Knowledge Graphs and Semantic AI
Introduction to Knowledge Graphs and Semantic AIIntroduction to Knowledge Graphs and Semantic AI
Introduction to Knowledge Graphs and Semantic AI
 
From Data Science to MLOps
From Data Science to MLOpsFrom Data Science to MLOps
From Data Science to MLOps
 
Machine Learning Models in Production
Machine Learning Models in ProductionMachine Learning Models in Production
Machine Learning Models in Production
 
Data Engineering.pdf
Data Engineering.pdfData Engineering.pdf
Data Engineering.pdf
 
Architect’s Open-Source Guide for a Data Mesh Architecture
Architect’s Open-Source Guide for a Data Mesh ArchitectureArchitect’s Open-Source Guide for a Data Mesh Architecture
Architect’s Open-Source Guide for a Data Mesh Architecture
 
Ml ops intro session
Ml ops   intro sessionMl ops   intro session
Ml ops intro session
 
MLOps Bridging the gap between Data Scientists and Ops.
MLOps Bridging the gap between Data Scientists and Ops.MLOps Bridging the gap between Data Scientists and Ops.
MLOps Bridging the gap between Data Scientists and Ops.
 
Introducing MlFlow: An Open Source Platform for the Machine Learning Lifecycl...
Introducing MlFlow: An Open Source Platform for the Machine Learning Lifecycl...Introducing MlFlow: An Open Source Platform for the Machine Learning Lifecycl...
Introducing MlFlow: An Open Source Platform for the Machine Learning Lifecycl...
 
Koalas: How Well Does Koalas Work?
Koalas: How Well Does Koalas Work?Koalas: How Well Does Koalas Work?
Koalas: How Well Does Koalas Work?
 

Similar to Deploying Machine Learning Models to Production

Analytics Zoo: Building Analytics and AI Pipeline for Apache Spark and BigDL ...
Analytics Zoo: Building Analytics and AI Pipeline for Apache Spark and BigDL ...Analytics Zoo: Building Analytics and AI Pipeline for Apache Spark and BigDL ...
Analytics Zoo: Building Analytics and AI Pipeline for Apache Spark and BigDL ...Databricks
 
Our Puppet Story (GUUG FFG 2015)
Our Puppet Story (GUUG FFG 2015)Our Puppet Story (GUUG FFG 2015)
Our Puppet Story (GUUG FFG 2015)DECK36
 
Azure machine learning service
Azure machine learning serviceAzure machine learning service
Azure machine learning serviceRuth Yakubu
 
Open erp technical_memento_v0.6.3_a4
Open erp technical_memento_v0.6.3_a4Open erp technical_memento_v0.6.3_a4
Open erp technical_memento_v0.6.3_a4openerpwiki
 
Smart Data Conference: DL4J and DataVec
Smart Data Conference: DL4J and DataVecSmart Data Conference: DL4J and DataVec
Smart Data Conference: DL4J and DataVecJosh Patterson
 
I want my model to be deployed ! (another story of MLOps)
I want my model to be deployed ! (another story of MLOps)I want my model to be deployed ! (another story of MLOps)
I want my model to be deployed ! (another story of MLOps)AZUG FR
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
Introduction to Apache Amaterasu (Incubating): CD Framework For Your Big Data...
Introduction to Apache Amaterasu (Incubating): CD Framework For Your Big Data...Introduction to Apache Amaterasu (Incubating): CD Framework For Your Big Data...
Introduction to Apache Amaterasu (Incubating): CD Framework For Your Big Data...DataWorks Summit
 
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scala
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scalaAutomate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scala
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scalaChetan Khatri
 
mongoDB Performance
mongoDB PerformancemongoDB Performance
mongoDB PerformanceMoshe Kaplan
 
ETL with SPARK - First Spark London meetup
ETL with SPARK - First Spark London meetupETL with SPARK - First Spark London meetup
ETL with SPARK - First Spark London meetupRafal Kwasny
 
MongoDB World 2019: Terraform New Worlds on MongoDB Atlas
MongoDB World 2019: Terraform New Worlds on MongoDB Atlas MongoDB World 2019: Terraform New Worlds on MongoDB Atlas
MongoDB World 2019: Terraform New Worlds on MongoDB Atlas MongoDB
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profilerIhor Bobak
 
Trenowanie i wdrażanie modeli uczenia maszynowego z wykorzystaniem Google Clo...
Trenowanie i wdrażanie modeli uczenia maszynowego z wykorzystaniem Google Clo...Trenowanie i wdrażanie modeli uczenia maszynowego z wykorzystaniem Google Clo...
Trenowanie i wdrażanie modeli uczenia maszynowego z wykorzystaniem Google Clo...Sotrender
 
Drupal Efficiency - Coding, Deployment, Scaling
Drupal Efficiency - Coding, Deployment, ScalingDrupal Efficiency - Coding, Deployment, Scaling
Drupal Efficiency - Coding, Deployment, Scalingsmattoon
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performanceEngine Yard
 
RivieraJUG - MySQL Indexes and Histograms
RivieraJUG - MySQL Indexes and HistogramsRivieraJUG - MySQL Indexes and Histograms
RivieraJUG - MySQL Indexes and HistogramsFrederic Descamps
 
Using Deep Learning on Apache Spark to Diagnose Thoracic Pathology from Chest...
Using Deep Learning on Apache Spark to Diagnose Thoracic Pathology from Chest...Using Deep Learning on Apache Spark to Diagnose Thoracic Pathology from Chest...
Using Deep Learning on Apache Spark to Diagnose Thoracic Pathology from Chest...Databricks
 
Buildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbBuildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbMongoDB APAC
 
[2C2]PredictionIO
[2C2]PredictionIO[2C2]PredictionIO
[2C2]PredictionIONAVER D2
 

Similar to Deploying Machine Learning Models to Production (20)

Analytics Zoo: Building Analytics and AI Pipeline for Apache Spark and BigDL ...
Analytics Zoo: Building Analytics and AI Pipeline for Apache Spark and BigDL ...Analytics Zoo: Building Analytics and AI Pipeline for Apache Spark and BigDL ...
Analytics Zoo: Building Analytics and AI Pipeline for Apache Spark and BigDL ...
 
Our Puppet Story (GUUG FFG 2015)
Our Puppet Story (GUUG FFG 2015)Our Puppet Story (GUUG FFG 2015)
Our Puppet Story (GUUG FFG 2015)
 
Azure machine learning service
Azure machine learning serviceAzure machine learning service
Azure machine learning service
 
Open erp technical_memento_v0.6.3_a4
Open erp technical_memento_v0.6.3_a4Open erp technical_memento_v0.6.3_a4
Open erp technical_memento_v0.6.3_a4
 
Smart Data Conference: DL4J and DataVec
Smart Data Conference: DL4J and DataVecSmart Data Conference: DL4J and DataVec
Smart Data Conference: DL4J and DataVec
 
I want my model to be deployed ! (another story of MLOps)
I want my model to be deployed ! (another story of MLOps)I want my model to be deployed ! (another story of MLOps)
I want my model to be deployed ! (another story of MLOps)
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Introduction to Apache Amaterasu (Incubating): CD Framework For Your Big Data...
Introduction to Apache Amaterasu (Incubating): CD Framework For Your Big Data...Introduction to Apache Amaterasu (Incubating): CD Framework For Your Big Data...
Introduction to Apache Amaterasu (Incubating): CD Framework For Your Big Data...
 
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scala
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scalaAutomate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scala
Automate ml workflow_transmogrif_ai-_chetan_khatri_berlin-scala
 
mongoDB Performance
mongoDB PerformancemongoDB Performance
mongoDB Performance
 
ETL with SPARK - First Spark London meetup
ETL with SPARK - First Spark London meetupETL with SPARK - First Spark London meetup
ETL with SPARK - First Spark London meetup
 
MongoDB World 2019: Terraform New Worlds on MongoDB Atlas
MongoDB World 2019: Terraform New Worlds on MongoDB Atlas MongoDB World 2019: Terraform New Worlds on MongoDB Atlas
MongoDB World 2019: Terraform New Worlds on MongoDB Atlas
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profiler
 
Trenowanie i wdrażanie modeli uczenia maszynowego z wykorzystaniem Google Clo...
Trenowanie i wdrażanie modeli uczenia maszynowego z wykorzystaniem Google Clo...Trenowanie i wdrażanie modeli uczenia maszynowego z wykorzystaniem Google Clo...
Trenowanie i wdrażanie modeli uczenia maszynowego z wykorzystaniem Google Clo...
 
Drupal Efficiency - Coding, Deployment, Scaling
Drupal Efficiency - Coding, Deployment, ScalingDrupal Efficiency - Coding, Deployment, Scaling
Drupal Efficiency - Coding, Deployment, Scaling
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
 
RivieraJUG - MySQL Indexes and Histograms
RivieraJUG - MySQL Indexes and HistogramsRivieraJUG - MySQL Indexes and Histograms
RivieraJUG - MySQL Indexes and Histograms
 
Using Deep Learning on Apache Spark to Diagnose Thoracic Pathology from Chest...
Using Deep Learning on Apache Spark to Diagnose Thoracic Pathology from Chest...Using Deep Learning on Apache Spark to Diagnose Thoracic Pathology from Chest...
Using Deep Learning on Apache Spark to Diagnose Thoracic Pathology from Chest...
 
Buildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbBuildingsocialanalyticstoolwithmongodb
Buildingsocialanalyticstoolwithmongodb
 
[2C2]PredictionIO
[2C2]PredictionIO[2C2]PredictionIO
[2C2]PredictionIO
 

Recently uploaded

Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 

Recently uploaded (20)

Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 

Deploying Machine Learning Models to Production

  • 1. Deploying Machine Learning Models To Production 1 Anass BENSRHIR 11-05-2017
  • 2. Outline 2 •Typical ML Flow •Different Strategies to deploy Machine Learning Models •Verdict and Comparison
  • 3. Who am I ? • Founder et Senior Data Scientist @Bolddata • Big Data Project Leader @Schlumberger • Msc Advanced Systems & Machine Learning @CentraleParis anass@bolddata.net @anassbensrhir @abensrhir 3
  • 4. Typical Machine Learning Flow 4 Source : http://blog.cloudera.com/blog/2016/02/how-to-predict-telco-churn-with-apache-spark-mllib/
  • 5. Typical ML Model 5 from sklearn.datasets import load_iris import numpy as np from sklearn.ensemble import RandomForestClassifier import pandas as pd iris = load_iris() # Create a dataframe with the four feature variables df = pd.DataFrame(iris.data, columns=iris.feature_names) df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names) df['is_train'] = np.random.uniform(0, 1, len(df)) <= .75 train, test = df[df['is_train']==True], df[df['is_train']==False] features = df.columns[:4] y = pd.factorize(train['species'])[0] model = RandomForestClassifier(n_jobs=2) model.fit(train[features], y) model.predict(test[features])
  • 7. “Might Work well for KAGGLE! But Kaggle isn’t real world Machine learning!” >>>Interpretability + Low Complexity + Speed Accuracy = 0.81 Speed = 30ms Accuracy Accuracy = 0.91 Speed = 3s
  • 8. Life Cycle of Real World ML in Production Deployment Management Evaluation Monitoring
  • 9. Pickling 9 import cPickle as Pickle with open(“mymodel.pkl”, “wb”) as mymodelfile: Pickle.dump(model, mymodelfile) with open(“mymodel.pkl”, “rb”) as mymodelfile: thenewmodel = Pickle.load(mymodelfile) thenewmodel.predict(newvector)
  • 10. With Sklearn’s Joblib 10 from sklearn.externals import joblib joblib.dump(model,"model.joblib", compress=1) # compression into 1 file thenewmodel = joblib.load(“model.joblib") thenewmodel.predict(newvector)
  • 11. Pickle Vs Joblib Performance TimetoLoadtheModel 0s 0.2s 0.4s 0.6s 0.8s Loading The Model 0.72s 0.23s Joblib Pickle ModelFileSize 0kb 12.5kb 25kb 37.5kb 50kb File Size 48 kb 4.7 kb Joblib Pickle *The Same Model
  • 12. Verdict 12 GOOD BAD • Consistant way to save time and reuse the same model everywhere. • Fast ! • Might not work if Sklearn and python versions are different from saving to loading environments. • DevOPS nightmare.
  • 14. Popularity of programming languages index (TIOBE) 14 Source : https://www.tiobe.com/tiobe-index/ 5
  • 15. Cost vs Technological Benefit Tradeoff 15 Cost $ Technological Benefit Native Java / C++ .. models Rebuild the whole stack to Python API Powered Model Hybrid Approach PMML
  • 16. Native Java / C++ Models 16
  • 17. Native Libraries • Mostly used on legacy systems (Old CRM’s, Banking….) or High Frequency Trading strategies. • If used correctly, they are Fast • Entire List of Libraries : https://github.com/josephmisiti/awesome-machine-learning • C++ : • LightGBM (https://github.com/Microsoft/LightGBM) • MLPack (http://www.mlpack.org/) • Caffe/CUDA (deeplearning) (http://caffe.berkeleyvision.org/) • Java : • Aerosolve (https://github.com/airbnb/aerosolve) • H2O (https://github.com/h2oai/h2o-3) • Weka (http://www.cs.waikato.ac.nz/ml/weka/)
  • 18. Native Java/C++ Verdict 18 GOOD BAD • Used on High Frequency Trading floors where speed trumps usability and agility. • Faster !!! • No use of Scikit-learn / pandas Data science libraries • Limitation of available algorithms • Difficult and Costly ($$) • Does anybody know a Data scientist who works exclusively on Java or C++ ? (they are all in New York)
  • 20. PMML (Predictive Model Markup) PMML stands for "Predictive Model Markup Language". It is the de facto standard to represent predictive solutions. A PMML file may contain a myriad of data transformations (pre- and post-processing) as well as one or more predictive models. Because it is a standard, PMML allows for different statistical and data mining tools to speak the same language. In this way, a predictive solution can be easily moved among different tools and applications without the need for custom coding. For example, it may be developed in one application and directly deployed on another. 20
  • 22. PMML Pipeline Scikit-learn model PMML File Export as PMML sklearn2pmml PMML File Import as PMML Knime Weka R SAS C++ Java General Purpose APP Use the Model sklearn2pmml : https://github.com/jpmml/sklearn2pmml Java PMML Library : https://github.com/jpmml Apache Spark PMML : https://github.com/jpmml/jpmml-spark 22 model.pmml model.pmml
  • 23. Python Code (Simplified) 23 import pandas from sklearn.datasets import load_iris from sklearn.ensemble import RandomForestClassifier from sklearn2pmml import PMMLPipeline iris = load_iris() # Create a dataframe with the four feature variables iris_df = pd.DataFrame(iris.data, columns=iris.feature_names) iris_pipeline = PMMLPipeline([ ("classifier", RandomForestClassifier()) ]) iris_pipeline.fit(iris_df[iris_df.columns.difference(["Species"])], iris_df["Species"]) from sklearn2pmml import sklearn2pmml sklearn2pmml(iris_pipeline, “RandomForestClassifier_Iris.pmml”)
  • 24. Java Code (So much Simplified) 24 // Load the file using our simple util function. PMML pmml = JPMMLUtils.loadModel( “RandomForestClassifier_Iris.pmml” ); // Now we need a prediction evaluator for the loaded model PMMLManager mgr = new PMMLManager( pmml ); ModelEvaluator modelEvaluator = (ModelEvaluator) mgr.getModelManager(modelName, ModelEvaluatorFactory.getInstance()); Evaluator evaluator = modelEvaluator; … … … Map results = evaluator.evaluate( features ); // prediction happens here
  • 25. Use PMML in Spark 25 spark-submit --master local --class org.jpmml.spark.EvaluationExample example-1.0-SNAPSHOT.jar RandomForestClassifier_Iris.pmml Iris.csv /tmp/output/ example-1.0-SNAPSHOT.jar contains a java code to import the CSV and the pmml model
  • 26. Verdict 26 GOOD BAD • Interoperable • Use Python data science stack and deploy everywhere • No Agility nor sustainability • Not Every ML algorithm is available • PMML Files Are BIG (Gigabytes…) • Need to use unit tests and match python output with new output = Slow deployment
  • 28. Flask - Scikit-learn Model 28 Scikit-learn model FLASK Nginx Features Vector (x) Predicted Value (y) Request Response Request : x {a = 1, b=3.4, c=3} Response : status = 200 , y {predicted= “setosa”} Web Request/response Application / Webapp / Mobile App
  • 29. Json POST Request 29 curl -H "Content-Type: application/json” -X POST -d ‘{“a":1,"b":2, “c”:4}’ http://localhost:5000/api/1.0/predict Better With Security Enabled curl -H "Content-Type: application/json” -H "Authorization: Bearer <ACCESS_TOKEN>" -X POST -d ‘{“a":1,"b":2, “c”:4}’ http://localhost:5000/api/1.0/predict
  • 30. Flask Code (Simplified) 30 from flask import Flask, request, jsonify from config import VERSION from mycustommodel import model2 as model app = Flask(__name__) @app.route('/api/{version}/predict'.format(version=VERSION), methods=['POST']) def predict(): request = request.get_json(silent=True) a = request.get('a') b = request.get('b') c = request.get('c') prediction = model.predict([a, b, c]) response = dict(status="ok", prediction=prediction) return jsonify(response)
  • 31. Python Web Frameworks Benchmark 31 • Results for Loading and returning a json object. • Falcon and Flask have the best Speed/Usability Tradeoff
  • 33. MongoDB Document 33 { "_id" : ObjectId("4f693d40e4b04cde19f17205"), "hostname" : “ec2-203-0-113-25.compute-1.amazonaws.com", “user_id" : “19846”, “prediction_id" : “3f6dcfe0-f0ac-4e94-ac46-35c1ce8d59f8”, #For traceability "model_version": "1.0", "request_features" : { "a": 1.5, "b": 0, "c": 3.2 }, "response_prediction" { "class": "setosa", "probability": 0.701 }, "requested_at": ISODate("2017-03-10T10:50:42.389Z"), "predicted_at": ISODate("2017-03-10T10:50:43.132Z"), }
  • 34. Nginx Config File 34 # Define your "upstream" servers - the # servers request will be sent to upstream app_example { least_conn; # Use Least Connections strategy server 192.168.1.19:5000; # Flask Server 1 server 192.168.1.19:5001; # Flask Server 1 Model 2 server 192.168.1.20:5000; # Flask Server 2 server 192.168.1.21:5000; # Flask Server 3 } server { listen 80; server_name model.example.com # pass the request to Flask Gunicorn server # with some correct headers for proxy-awareness location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; } }
  • 35. 2 strategies Model 1 Model 2 Sample 1 Sample 2 3000 visits 10% conversion 3000 visits 40% conversion Use Model 2 Everywhere Strategie 1 : Use different models as they are, and update the models with new data afterwards Strategie 2 : Use A/B Testing strategy to serve the best performing model to the whole sample.
  • 37. Verdict 37 GOOD BAD • SCALABLE • Interoperable (can be used both by backend and frontend languages think : javascript !) • Agile : models can be put on production very fast and with no code change, simple as launching a new server instance • As Fast as you need it to be (add new servers or docker containers) • Did i say Agile ? • The Infrastructure can become overwhelming and costly over time.
  • 38. Other Options ? • PredictionIO (http://prediction.io) • Seldon (https://www.seldon.io/) Interesting ! • Oryx (http://oryx.io) Built for Hadoop