Package and deploy models outside Azure Machine Learning (preview)
You can deploy models outside of Azure Machine Learning for online serving by creating model packages (preview). Azure Machine Learning allows you to create a model package that collects all the dependencies required for deploying a machine learning model to a serving platform. You can move a model package across workspaces and even outside of Azure Machine Learning. To learn more about model packages, see Model packages for deployment (preview).
Important
This feature is currently in public preview. This preview version is provided without a service-level agreement, and we don't recommend it for production workloads. Certain features might not be supported or might have constrained capabilities.
For more information, see Supplemental Terms of Use for Microsoft Azure Previews.
In this article, you learn how package a model and deploy it to an Azure App Service.
Prerequisites
Before following the steps in this article, make sure you have the following prerequisites:
An Azure subscription. If you don't have an Azure subscription, create a free account before you begin. Try the free or paid version of Azure Machine Learning.
An Azure Machine Learning workspace. If you don't have one, use the steps in the How to manage workspaces article to create one.
Note
Private link enabled workspaces don't support packaging models for deployment outside of Azure Machine Learning.
Azure role-based access controls (Azure RBAC) are used to grant access to operations in Azure Machine Learning. To perform the steps in this article, your user account must be assigned the owner or contributor role for the Azure Machine Learning workspace, or a custom role. For more information, see Manage access to an Azure Machine Learning workspace.
Prepare your system
Follow these steps to prepare your system.
The example in this article is based on code samples contained in the azureml-examples repository. To run the commands locally without having to copy/paste YAML and other files, first clone the repo and then change directories to the folder:
git clone https://github.com/Azure/azureml-examples --depth 1 cd azureml-examples/cli
This article uses the example in the folder endpoints/online/deploy-with-packages/mlflow-model.
Connect to the Azure Machine Learning workspace where you'll do your work.
Packages require the model to be registered in either your workspace or in an Azure Machine Learning registry. In this example, there's a local copy of the model in the repository, so you only need to publish the model to the registry in the workspace. You can skip this step if the model you're trying to deploy is already registered.
Deploy a model package to the Azure App Service
In this section, you package the previously registered MLflow model and deploy it to the Azure App Service.
Deploying a model outside of Azure Machine Learning requires creating a package specification. To create a package that's completely disconnected from Azure Machine Learning, specify the
copy
mode in the model configuration. Thecopy
mode tells the package to copy the artifacts inside of the package. The following code shows how to specify the copy mode for the model configuration:Create a package YAML specification:
package-external.yml
$schema: http://azureml/sdk-2-0/ModelVersionPackage.json target_environment: heart-classifier-mlflow-pkg inferencing_server: type: azureml_online model_configuration: mode: copy
Tip
When you specify the model configuration using
copy
for the mode property, you guarantee that all the model artifacts are copied inside the generated docker image instead of downloaded from the Azure Machine Learning model registry, thereby allowing true portability outside of Azure Machine Learning. For a full specification about all the options when creating packages see Create a package specification.Start the package operation.
The result of the package operation is an environment in Azure Machine Learning. The advantage of having this environment is that each environment has a corresponding docker image that you can use in an external deployment. Images are hosted in the Azure Container Registry. The following steps show how you get the name of the generated image:
Go to the Azure Machine Learning studio.
Select the Environments section.
Select the Custom environments tab.
Look for the environment named heart-classifier-mlflow-package, which is the name of the package you just created.
Copy the value that's in the Azure container registry field.
Now, deploy this package in an App Service.
Go to the Azure portal and create a new App Service resource.
In the creation wizard, select the subscription and resource group you're using.
In the Instance details section, give the app a name.
For Publish, select Docker container.
For Operating System, select Linux.
Configure the rest of the page as needed and select Next.
Go to the Docker tab.
For Options, select Single Container.
For Image Source, select Azure Container Registry.
Configure the Azure container registry options as follows:
For Registry, select the Azure Container Registry associated with the Azure Machine Learning workspace.
For Image, select the image that you found in step 3(e) of this tutorial.
For Tag, select latest.
Configure the rest of the wizard as needed.
Select Create. The model is now deployed in the App Service you created.
The way you invoke and get predictions depends on the inference server you used. In this example, you used the Azure Machine Learning inferencing server, which creates predictions under the route
/score
. For more information about the input formats and features, see the details of the package azureml-inference-server-http.Prepare the request payload. The format for an MLflow model deployed with Azure Machine Learning inferencing server is as follows:
sample-request.json
{ "input_data": { "columns": [ "age", "sex", "cp", "trestbps", "chol", "fbs", "restecg", "thalach", "exang", "oldpeak", "slope", "ca", "thal" ], "index": [1], "data": [ [1, 1, 4, 145, 233, 1, 2, 150, 0, 2.3, 3, 0, 2] ] } }
Test the model deployment to see if it works.
cat -A sample-request.json | curl http://heart-classifier-mlflow-pkg.azurewebsites.net/score \ --request POST \ --header 'Content-Type: application/json' \ --data-binary @-