/aml_registry

Primary LanguagePythonMIT LicenseMIT

How to use registries to share components, environments, and models

This project shows how you can use an Azure ML registry to share components, environments, and models with the users in your organization.

Blog post

To learn more about the code in this repo, check out the accompanying blog post: https://bea.stollnitz.com/blog/aml-registry/

Setup

  • You need to have an Azure subscription. You can get a free subscription to try it out.
  • Create a resource group.
  • Create a new machine learning workspace by following the "Create the workspace" section of the documentation. Keep in mind that you'll be creating a "machine learning workspace" Azure resource, not a "workspace" Azure resource, which is entirely different!
  • Install the Azure CLI by following the instructions in the documentation.
  • Install the ML extension to the Azure CLI by following the "Installation" section of the documentation.
  • Install and activate the conda environment by executing the following commands:
conda env create -f environment.yml
conda activate aml_registry
  • Within VS Code, go to the Command Palette clicking "Ctrl + Shift + P," type "Python: Select Interpreter," and select the environment that matches the name of this project.
  • In a terminal window, log in to Azure by executing az login --use-device-code.
  • Set your default subscription by executing az account set -s "<YOUR_SUBSCRIPTION_NAME_OR_ID>". You can verify your default subscription by executing az account show, or by looking at ~/.azure/azureProfile.json.
  • Set your default resource group and workspace by executing az configure --defaults group="<YOUR_RESOURCE_GROUP>" workspace="<YOUR_WORKSPACE>". You can verify your defaults by executing az configure --list-defaults or by looking at ~/.azure/config.
  • You can now open the Azure Machine Learning studio, where you'll be able to see and manage all the machine learning resources we'll be creating.
  • Install the Azure Machine Learning extension for VS Code, and log in to it by clicking on "Azure" in the left-hand menu, and then clicking on "Sign in to Azure."

Training and inference on your development machine

  • Under "Run and Debug" on VS Code's left navigation, choose the "Train locally" run configuration and press F5.
  • Repeat for the "Test locally" configuration.

You can look at the logs generated by the code in these files with the following command:

mlflow ui

Creating a registry

Create the registry with the following command:

cd aml_registry/cloud
az ml registry create --file registry.yml

Now let's give one other user read access to the contents of our registry. Go to the Azure portal, search for "registry-demo" (the name of our registry resource), and click on it. On the left navigation, click on "Access control (IAM)." Click on "Add" in the top menu, then "Add role assignment." Select a role (for example, "Reader"), click "Next," click on "Select members," enter the email of the user you'd like to give read permissions to, and then click on "Review + assign." You can verify your new role assignment under the "Role assignments" tab.

Registering an environment and components in the registry

Let's register the training environment in the registry:

az ml environment create --file environment-train.yml --registry-name registry-demo

Now let's register our two components (which use the environment we just created) also in the registry:

az ml component create --file train.yml --registry-name registry-demo
az ml component create --file test.yml --registry-name registry-demo

Training and deploying in the current workspace

Create the compute and data in the cloud, if you don't have them already created (from one of my other projects):

az ml compute create -f cluster-cpu.yml
az ml data create -f data.yml

Now let's train a model in the current workspace, using the components (and environment) from the registry.

run_id=$(az ml job create --file pipeline-job.yml --query name -o tsv)

Go to the Azure ML studio and wait for the job to complete. Once it completes, register the output model with the workspace:

az ml model create --name model-registry --path "azureml://jobs/$run_id/outputs/model_dir" --type mlflow_model

Deploy the job in the current workspace.

az ml online-endpoint create -f endpoint-workspace.yml
az ml online-deployment create -f deployment-workspace.yml --all-traffic

Invoke the endpoint.

az ml online-endpoint invoke --name endpoint-registry-workspace --request-file ../test_data/images_azureml.json

Copying the model to the registry and deploying it

If the endpoint works as expected in the current workspace, we can copy the model to the registry.

az ml model create --registry-name registry-demo --path azureml://models/model-registry/versions/1 --type mlflow_model

Now we can deploy the model from the registry:

az ml online-endpoint create -f endpoint-registry.yml
az ml online-deployment create -f deployment-registry.yml --all-traffic

Invoke the endpoint.

az ml online-endpoint invoke --name endpoint-registry --request-file ../test_data/images_azureml.json

Deleting the endpoints

Delete the two endpoints to avoid charges:

az ml online-endpoint delete --name endpoint-registry-workspace -y
az ml online-endpoint delete --name endpoint-registry -y

Related resources