I found that I can register the model using Mlflow, but I don't know how to register it in ONNX format.
I found out that the model is registered using Mlflow.
But I don't know how to convert AutoML models to ONNX format and register them with Mlflow.
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
from azureml.train.automl import AutoMLConfig
from azureml.core import Workspace, Dataset
from azureml.core.experiment import Experiment
from azureml.core.model import Model
from azureml.core.authentication import ServicePrincipalAuthentication
from azureml.automl.runtime.onnx_convert import OnnxConverter
from random import random
from mlflow.tracking import MlflowClient
import mlflow
import mlflow.onnx
import os
import azureml.mlflow
auth = ServicePrincipalAuthentication(
tenant_id="",
service_principal_id="",
service_principal_password="")
subscription_id = ''
resource_group = ''
workspace_name = ''
ml_client = MLClient(credential=auth,
subscription_id=subscription_id,
resource_group_name=resource_group)
azure_mlflow_uri = ml_client.workspaces.get(workspace_name).mlflow_tracking_uri
mlflow.set_tracking_uri(azure_mlflow_uri)
ws = Workspace(subscription_id, resource_group, workspace_name, auth=auth)
train_data = Dataset.get_by_name(ws, name='iris')
label = "class"
automl_settings = {
"primary_metric": 'AUC_weighted',
"n_cross_validations": 2
}
automl_classifier = AutoMLConfig(
task='classification',
blocked_models=['XGBoostClassifier'],
enable_onnx_compatible_models=True,
experiment_timeout_minutes=30,
training_data=train_data,
label_column_name=label,
**automl_settings
)
experiment_name = 'experimetn_with_mlflow'
mlflow.set_experiment(experiment_name)
experiment = Experiment(ws, experiment_name)
with mlflow.start_run() as mlflow_run:
mlflow.log_metric("iris_metric", random())
mlflow_run = experiment.submit(automl_classifier, show_output=True)
description = 'iris_Description'
model = mlflow_run.register_model(description=description,
model_name='iris_Model')
best_run, onnx_mdl = mlflow_run.get_output(return_onnx_model=True)
onnx_fl_path = "./best_model.onnx"
OnnxConverter.save_onnx_model(onnx_mdl, onnx_fl_path)
model = Model.register(workspace=ws,
description=description,
model_name='iris_onnx_model',
model_path=onnx_fl_path)
client = MlflowClient()
finished_mlflow_run = MlflowClient().get_run(mlflow_run.run_id)
metrics = finished_mlflow_run.data.metrics
tags = finished_mlflow_run.data.tags
params = finished_mlflow_run.data.params
model_path = "best_model"
model_uri = 'runs:/{}/{}'.format(mlflow_run.run_id, model_path)
mlflow.register_model(model_uri, 'iris_onnx_mlflow_model')