MLflow Présentation
MLflow Présentation
MLflow Models
INTRODUCTION TO MLFLOW
Weston Bassler
Senior MLOps Engineer
MLflow Models
Simplify ML library integration
Simplify Deployment
1 unsplash.com
INTRODUCTION TO MLFLOW
Built-In Flavors
Write custom tools from ML libraries
1 mlflow.org
INTRODUCTION TO MLFLOW
Autolog
# Automatically log model and metrics
mlflow.FLAVOR.autolog()
INTRODUCTION TO MLFLOW
Scikit-learn Flavor
# Import scikit-learn # Train the model
import mlflow lr = LinearRegression()
from sklearn.linear_model import \ lr.fit(X, y)
LinearRegression
Model will be logged automatically on
# Using auto-logging model.fit()
mlflow.sklearn.autolog()
INTRODUCTION TO MLFLOW
Common Metrics Common Parameters
Regression
MODEL.get_params()
mean squared error
r2 score
Classification
precision score
recall score
f1 score
accuracy score
INTRODUCTION TO MLFLOW
Common parameters
# Train the model
lr = LinearRegression()
lr.fit(X, y)
# Get params
params = lr.get_params(deep=True)
params
INTRODUCTION TO MLFLOW
Autolog parameters
# Model
lr = LinearRegression()
lr.fit(X, y)
INTRODUCTION TO MLFLOW
Autolog metrics
# Model
lr = LinearRegression()
lr.fit(X, y)
INTRODUCTION TO MLFLOW
Storage format
Directory structure for a model:
# Autolog
mlflow.sklearn.autolog()
model/
MLmodel
model.pkl
python_env.yaml
requirements.txt
INTRODUCTION TO MLFLOW
Contents of MLmodel
artifact_path: model
flavors:
python_function:
env:
virtualenv: python_env.yaml
loader_module: mlflow.sklearn
model_path: model.pkl
predict_fn: predict
python_version: 3.10.8
sklearn:
code: null
pickled_model: model.pkl
serialization_format: cloudpickle
sklearn_version: 1.1.3
INTRODUCTION TO MLFLOW
MLmodel
INTRODUCTION TO MLFLOW
Let's practice!
INTRODUCTION TO MLFLOW
Model API
INTRODUCTION TO MLFLOW
Weston Bassler
Senior MLOps Engineer
MLflow REST API
1 istock.com
INTRODUCTION TO MLFLOW
The Model API
Save
Log
Load
1 wikipedia.org
INTRODUCTION TO MLFLOW
Model API functions
# Save a model to the local filesystem
mlflow.sklearn.save_model(model, path)
INTRODUCTION TO MLFLOW
Load model
Local Filesystem - relative/path/to/local/model or /Users/me/path/to/local/model
MLflow Tracking - runs:/<mlflow_run_id>/run-relative/path/to/model
S3 Support - s3://my_bucket/path/to/model
INTRODUCTION TO MLFLOW
Save model
# Model
lr = LogisticRegression()
lr.fit(X, y)
ls local_path/
INTRODUCTION TO MLFLOW
Load local model
# Load model from local path
model = mlflow.sklearn.load_model("local_path")
# Show model
model
LogisticRegression()
INTRODUCTION TO MLFLOW
Log model
# Model
lr = LogisticRegression(n_jobs=n_jobs)
lr.fit(X, y)
# Log model
mlflow.sklearn.log_model(lr, "tracking_path")
INTRODUCTION TO MLFLOW
Tracking UI
INTRODUCTION TO MLFLOW
Last active run
# Format for runs
runs:/<mlflow_run_id>/run-relative/path/to/model
INTRODUCTION TO MLFLOW
Last active run id
# Get last active run
run = mlflow.last_active_run()
'8c2061731caf447e805a2ac65630e70c'
INTRODUCTION TO MLFLOW
Setting the run id
# Get last active run
run = mlflow.last_active_run()
# Set run_id variable
run_id = run.info.run_id
run_id
'8c2061731caf447e805a2ac65630e70c'
INTRODUCTION TO MLFLOW
Load model from MLflow Tracking
# Pass run_id as f-string literal
model = mlflow.sklearn.load_model(f"runs:/{run_id}/tracking_path")
# Show model
model
LogisticRegression()
INTRODUCTION TO MLFLOW
Let's Practice
INTRODUCTION TO MLFLOW
Custom models
INTRODUCTION TO MLFLOW
Weston Bassler
Senior MLOps Engineer
Example use cases
NLP - Tokenizer(s)
Classification - Label encoder
Pre/Post processing
1 unsplash.com
INTRODUCTION TO MLFLOW
Custom Python models
Built in Flavor - python_function
mlflow.pyfunc
save_model()
log_model()
load_model()
INTRODUCTION TO MLFLOW
Custom model class
Custom model class
MyClass(mlflow.pyfunc.PythonModel)
PythonModel class
load_context() - loads artifacts when mlflow.pyfunc.load_model() is called
INTRODUCTION TO MLFLOW
Python class
# Class # Create a new Object
class MyPythonClass: x = MyPythonClass()
# Function that prints Hello! # Excute my_func function
def my_func(): x.my_func
print("Hello!")
"Hello!"
INTRODUCTION TO MLFLOW
Example custom Class
import mlflow.pyfunc
INTRODUCTION TO MLFLOW
Saving and logging a custom model
# Save model to local filesystem
mlflow.pyfunc.save_model(path="custom_model", python_model=CustomPredict())
INTRODUCTION TO MLFLOW
Loading custom models
# Load model from local filesystem
mlflow.pyfunc.load_model("local")
INTRODUCTION TO MLFLOW
Model Evaluation
mlflow.evaluate() - Performance based on a dataset
INTRODUCTION TO MLFLOW
Evaluation Example
# Training Data # Dataset
X_train, X_test, y_train, y_test = \ eval_data = X_test
train_test_split(X, y, eval_data["test_label"] = y_test
train_size=0.7,random_state=0)
# Evaluate model with Dataset
# Linear Regression model mlflow.evaluate(
lr = LinearRegression() "runs:/run_id/model",
lr.fit(X_train, y_train) eval_data,
targets="test_label",
model_type="regressor"
)
INTRODUCTION TO MLFLOW
Tracking UI
1 shap.readthedocs.io
INTRODUCTION TO MLFLOW
Let's practice!
INTRODUCTION TO MLFLOW
Model serving
INTRODUCTION TO MLFLOW
Weston Bassler
Senior MLOps Engineer
MLflow Models
Standardize model packaging
Log models
Model Evaluation
INTRODUCTION TO MLFLOW
Model Deployment
1 datacamp.com
INTRODUCTION TO MLFLOW
REST API
/ping - for health checks
Port 5000
INTRODUCTION TO MLFLOW
Invocations endpoint
/invocations
{
"1": {
No,Name,Subject
"No": "1",
1,Bill Johnson,English
"Name": "Bill Johnson",
2,Gary Valentine,Mathematics
"Subject": "English"
},
Content-Type : application/json or
"2": {
application/csv
"No": "2",
"Name": "Gary Valentine",
"Subject": "Mathematics"
}
}
INTRODUCTION TO MLFLOW
CSV format JSON format
Pandas Dataframe dataframe_split - pandas DataFrame in
split orientation
pandas_df.to_csv()
dataframe_records - pandas DataFrame
in records orientation
INTRODUCTION TO MLFLOW
DataFrame split
# Dataframe split orientation
{
"dataframe_split": {
"columns": ["sex", "age", "weight"],
"data": [["male", 23, 160], ["female", 33, 120]]
}
}
INTRODUCTION TO MLFLOW
Serving Models
# MLflow serve command
mlflow models serve --help
Usage: mlflow models serve [OPTIONS]
INTRODUCTION TO MLFLOW
Serve uri
# Local Filesystem
mlflow models serve -m relative/path/to/local/model
# Run ID
mlflow models serve -m runs:/<mlflow_run_id>/artifacts/model
# AWS S3
mlflow models serve -m s3://my_bucket/path/to/model
INTRODUCTION TO MLFLOW
Serve example
# Serve model from run
mlflow models serve -m runs:/e84a122920de4bdeaedb54146deeb429/artifacts/model
INTRODUCTION TO MLFLOW
Invocations Request
# Send dataframe_split orientation payload to MLflow
curl http://127.0.0.1:5000/invocations -H 'Content-Type: application/json' -d '{
"dataframe_split": {
"columns": ["sex", "age", "weight"],
"data": [["male", 23, 160], ["female", 33, 120]]
}
}'
INTRODUCTION TO MLFLOW
Let's practice!
INTRODUCTION TO MLFLOW