Package: mlflavors
The MLflavors package adds MLflow support for some popular machine learning frameworks currently
not considered for inclusion as MLflow built-in flavors. Just like built-in flavors, you can use
this package to save your model as an MLflow artifact, load your model from MLflow for batch
inference, and deploy your model to a serving endpoint using MLflow deployment tools.The following open-source libraries are currently supported:FrameworkTutorialsCategoryOrbitMLflow-OrbitTime Series ForecastingSktimeMLflow-SktimeTime Series ForecastingStatsForecastMLflow-StatsForecastTime Series ForecastingPyODMLflow-PyODAnomaly DetectionSDVMLflow-SDVSynthetic Data GenerationThe MLflow interface for the supported frameworks closely follows the design of built-in flavors.
Particularly, the interface for utilizing the custom model loaded as apyfuncflavor
for generating predictions uses a single-row Pandas DataFrame configuration argument to expose the
parameters of the flavor’s inference API.DocumentationUsage examples for all flavors and the API reference can be found in the packagedocumenation.InstallationInstalling from PyPI:$pipinstallmlflavorsQuickstartThis example trains aPyODKNN model
using a synthetic dataset. Normal data is generated by a multivariate
Gaussian distribution and outliers are generated by a uniform distribution.
A new MLflow experiment is created to log the evaluation metrics and the trained
model as an artifact and anomaly scores are computed loading the
trained model in native flavor andpyfuncflavor:importjsonimportmlflowimportpandasaspdfrompyod.models.knnimportKNNfrompyod.utils.dataimportgenerate_datafromsklearn.metricsimportroc_auc_scoreimportmlflavorsARTIFACT_PATH="model"withmlflow.start_run()asrun:contamination=0.1# percentage of outliersn_train=200# number of training pointsn_test=100# number of testing pointsX_train,X_test,_,y_test=generate_data(n_train=n_train,n_test=n_test,contamination=contamination)# Train kNN detectorclf=KNN()clf.fit(X_train)# Evaluate modely_test_scores=clf.decision_function(X_test)metrics={"roc":roc_auc_score(y_test,y_test_scores),}print(f"Metrics:\n{json.dumps(metrics,indent=2)}")# Log metricsmlflow.log_metrics(metrics)# Log model using pickle serialization (default).mlflavors.pyod.log_model(pyod_model=clf,artifact_path=ARTIFACT_PATH,serialization_format="pickle",)model_uri=mlflow.get_artifact_uri(ARTIFACT_PATH)# Print the run id wich is used below for serving the model to a local REST API endpointprint(f"\nMLflow run id:\n{run.info.run_id}")Make a prediction loading the model from MLflow in native format:loaded_model=mlflavors.pyod.load_model(model_uri=model_uri)print(loaded_model.decision_function(X_test))Make a prediction loading the model from MLflow inpyfuncformat:loaded_pyfunc=mlflavors.pyod.pyfunc.load_model(model_uri=model_uri)# Create configuration DataFramepredict_conf=pd.DataFrame([{"X":X_test,"predict_method":"decision_function",}])print(loaded_pyfunc.predict(predict_conf)[0])To serve the model to a local REST API endpoint run the command below where you substitute
the run id printed above:mlflowmodelsserve-mruns:/<run_id>/model--env-managerlocal--host127.0.0.1Open a new terminal and run the below model scoring script to request a prediction from
the served model:importpandasaspdimportrequestsfrompyod.utils.dataimportgenerate_datacontamination=0.1# percentage of outliersn_train=200# number of training pointsn_test=100# number of testing points_,X_test,_,_=generate_data(n_train=n_train,n_test=n_test,contamination=contamination)# Define local host and endpoint urlhost="127.0.0.1"url=f"http://{host}:5000/invocations"# Convert to list for JSON serializationX_test_list=X_test.tolist()# Create configuration DataFramepredict_conf=pd.DataFrame([{"X":X_test_list,"predict_method":"decision_function",}])# Create dictionary with pandas DataFrame in the split orientationjson_data={"dataframe_split":predict_conf.to_dict(orient="split")}# Score modelresponse=requests.post(url,json=json_data)print(response.json())ContributingContributions from the community are welcome, I will be happy to support the inclusion
and development of new features and flavors. To open an issue or request a new feature, please
open a GitHub issue.VersioningVersions and changes are documented in thechangelog.DevelopmentTo set up your local development environment, create a virtual environment, such as:$condacreate-nmlflavors-devpython=3.9$sourceactivatemlflavors-devInstall project locally:$python-mpipinstall--upgradepip$pipinstall-e".[dev]"Install pre-commit hooks:$pre-commitinstallRun tests:$pytestBuild Sphinx docs:$cddocs$makehtml