How to Register a Model in Azure Machine Learning

A comprehensive guide to packaging and registering your trained machine learning models.

Introduction

Registering your trained machine learning models in Azure Machine Learning (Azure ML) is a crucial step in the MLOps lifecycle. It allows you to version your models, manage them effectively, and deploy them for inference. This guide will walk you through the process using both the Azure ML Python SDK and the Azure ML Studio.

Prerequisites

Using the Azure ML Python SDK v2

The Python SDK provides a programmatic way to register models. This is ideal for integration into automated workflows and scripts.

Step 1: Authenticate and Initialize

First, you need to connect to your Azure ML workspace.


from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential

# Authenticate using Azure CLI or environment variables
credential = DefaultAzureCredential()

# Specify your workspace details
subscription_id = "YOUR_SUBSCRIPTION_ID"
resource_group = "YOUR_RESOURCE_GROUP"
workspace_name = "YOUR_WORKSPACE_NAME"

# Connect to your workspace
ml_client = MLClient(
    credential=credential,
    subscription_id=subscription_id,
    resource_group=resource_group,
    workspace_name=workspace_name
)

print(f"Connected to workspace: {ml_client.workspace_name}")
                

Step 2: Prepare Your Model Files

Ensure your model files are accessible. Often, models are saved in a specific directory. For demonstration, let's assume your model is saved as model.pkl in a folder named outputs.

Note: For production scenarios, it's best practice to store your model artifacts in an Azure ML registered datastore.

Step 3: Define and Register the Model

You can register a model directly from local files or from an MLflow run.

Option A: Registering from Local Files

This method registers a model artifact from a local path.


from azure.ai.ml.entities import Model
from azure.ai.ml.constants import AssetTypes

# Define the model
model_local_path = "outputs/model.pkl" # Path to your model file
model_name = "my-sklearn-model"
model_version = "1"
model_description = "A scikit-learn model registered from local file."
model_tags = {"area": "classification", "framework": "sklearn"}

registered_model = Model(
    path=model_local_path,
    name=model_name,
    version=model_version,
    description=model_description,
    tags=model_tags,
    type=AssetTypes.CUSTOM_MODEL # Or specify framework like MLFLOW_MODEL, TRITON_MODEL etc.
)

# Register the model
created_model = ml_client.models.create_or_update(registered_model)
print(f"Model '{created_model.name}' version '{created_model.version}' registered successfully.")
                

Option B: Registering from an MLflow Run

If you logged your model using MLflow during training, you can register it directly from the run ID.


from azure.ai.ml.entities import Model
from azure.ai.ml.constants import AssetTypes

# Assume 'run_id' is the ID of your MLflow experiment run
run_id = "YOUR_MLFLOW_RUN_ID"
model_name_mlflow = "my-mlflow-model"
model_description_mlflow = "An MLflow model registered from a run."
model_tags_mlflow = {"area": "regression", "framework": "mlflow"}

# Register the model from an MLflow run artifact
# The 'artifact_path' is the relative path within the run's artifacts
# where the MLflow model is saved (e.g., 'model' if you logged with mlflow.log_model(model, 'model'))
registered_model_mlflow = Model(
    path=f"azureml://runs/{run_id}/artifacts/path/to/your/mlflow/model/folder",
    name=model_name_mlflow,
    description=model_description_mlflow,
    tags=model_tags_mlflow,
    type=AssetTypes.MLFLOW_MODEL # Specify MLFLOW_MODEL for MLflow models
)

created_model_mlflow = ml_client.models.create_or_update(registered_model_mlflow)
print(f"MLflow Model '{created_model_mlflow.name}' version '{created_model_mlflow.version}' registered successfully.")
                

Using Azure Machine Learning Studio

The Azure ML Studio provides a user-friendly graphical interface for managing your models.

Step 1: Navigate to Models

Log in to your Azure ML workspace in the Azure portal, then navigate to the Azure Machine Learning studio. On the left-hand navigation menu, click on Models under the Assets section.

Step 2: Register a New Model

Click the Register button. You will see a form with several options:

Step 3: Upload Model Files

Based on your selected source, you will either upload your model file(s) directly or provide a path/run ID.

If registering from local files, you can upload a single file or a ZIP archive containing all your model assets.

Step 4: Confirmation

After filling in the details and uploading the files, click Register. Your model will appear in the Models list.

Tip: For models trained with MLflow, registering from an MLflow run is often the most streamlined approach in Studio.

Best Practices

Next Steps

Once your model is registered, you can: