I want to register the model learned by AutoML in Azure Machine learning in ONNX format and call it in Azure Synapse Analitics.

保史 細見 21 Reputation points
2022-09-29T00:28:29.103+00:00

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')
Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
3,334 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ramr-msft 17,826 Reputation points
    2022-09-29T12:05:27.3+00:00

    @保史 細見 Thanks for the question. Can you please share document/sample that you are trying. In order to save trained model download (and score) as the ONNX model you have here a few code examples.
    MLflow model registry will enable Synapse to run ONNX models is in preview.

    Here is the ONNX prediction section in the sample notebook.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. 保史 細見 21 Reputation points
    2022-09-29T13:28:40.59+00:00

    Thanks for the reply.
    I made it with the code you gave me.
    I was able to save the model in ONNX format in Azure Machine learning(See image file 2) using the following code, but could not reference it from Azure Synapse Analytics. See image file 1.

    from azureml.automl.runtime.onnx_convert import OnnxConverter

    onnx_fl_path = "./best_model.onnx"
    OnnxConverter.save_onnx_model(onnx_mdl, onnx_fl_path)
    246076-registed-model.png
    246121-%E5%9B%B31.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.