Publishing a trained model is an integral step in the deployment of an AI solution. After training a model to make predictions, using tools such as Azure Machine Learning or any other ML frameworks, the next step is to make this model available for others to use in applications or services. A trained model is of no use if it cannot be accessed by end-users, and this is where Microsoft Azure comes to the rescue. It provides a robust and well-structured platform for hosting and managing your trained models allowing them to be used in real-world applications.
With Azure, you can deploy your model in three standard ways:
- Azure Kubernetes Service (AKS): Suitable for high-scale production deployments. It gives you more control over the inference server.
- Azure Container Instances (ACI): Perfect for testing or development.
- Azure Machine Learning Compute Instances: Used for deploying models for batch inference.
Before you proceed with the deployment, ensure that you have registered your trained model with Azure Machine Learning. Registering your model in Azure not only stores the model, but it also allows you to track its versions and retrieve them whenever necessary.
Steps to publish a trained model
Let’s examine the steps to publish a trained model, assuming this case using Azure ML SDK for Python.
Step 1: Import necessary libraries
from azureml.core import Workspace, Model, Run
Step 2: Identify workspace and register the model
ws = Workspace.from_config()
model = Model.register(workspace=ws,
model_name='Your-Model-Name',
model_path='Your-Model-Path',
description='Your-Model-Description')
Step 3: Define inference configuration
The inference configuration describes how to configure the model to make predictions.
from azureml.core.model import InferenceConfig
from azureml.core.environment import Environment
from azureml.core.conda_dependencies import CondaDependencies
env = Environment('Your-Environment-Name')
cd = CondaDependencies.create(pip_packages=['azureml-dataset-runtime[pandas,fuse]', 'azureml-defaults'], conda_packages = ['scikit-learn==0.22.1'])
env.python.conda_dependencies = cd
inference_config = InferenceConfig(entry_script='score.py',
environment=env)
Step 4: Select the compute target
The compute target is the compute resource where the service will be deployed.
from azureml.core.compute import ComputeTarget, AksCompute
aks_target = ComputeTarget(workspace=ws, name='Your-Aks-Compute')
Step 5: Deploy the model
The next step is to deploy the model as a web service.
from azureml.core.webservice import AksWebservice
deployment_config = AksWebservice.deploy_configuration()
service = Model.deploy(ws, 'Your-Service-Name', [model], inference_config, deployment_config, aks_target)
service.wait_for_deployment(show_output = True)
After the deployment is successful, you can test your model’s endpoint with the Azure SDK function service.run() or consume the endpoint in your client applications.
To summarize, publishing a trained model in Azure involves registering the model, defining an inference configuration, specifying a deployment configuration, and finally, deploying the model on your chosen compute target. The Azure platform ensures a seamless and efficient way to manage your trained models while providing options to scale at any time, making it a top choice for machine learning deployments.
Practice Test
When publishing a trained model, you must specify the compute type that deployment will use.
- True
- False
Answer: True
Explanation: When you publish a model, you need to define the compute resource that will host it. The type of compute resource you select when deploying your model will affect the scalability, cost, and speed of running predictions.
Which of the following is NOT a step in publishing a trained model in Azure?
- Configuring the compute target
- Defining the deployment configuration
- Uploading the model to Azure Blob Storage
- Registering the model
Answer: Uploading the model to Azure Blob Storage
Explanation: The model does not need to be uploaded to Azure Blob Storage as a step in publishing. Rather, the model must be registered in Azure ML workspace.
You can only publish a trained model from Azure Machine Learning Studio.
- True
- False
Answer: False
Explanation: You can certainly utilize Azure Machine Learning Studio to publish a trained model, but you can also use Azure Machine Learning SDK or other compatible platforms.
Azure Kubernetes Service (AKS) cluster deployments are ideal for high-scale, production deployments.
- True
- False
Answer: True
Explanation: AKS deployments provide fast response times and autoscaling of the deployed service, making them ideal for high-scale, production deployments.
Model endpoints in Azure are automatically created when a model is registered.
- True
- False
Answer: False
Explanation: Model endpoints are created when a model is deployed, not just registered. Deployment involves providing an inference script and tying it to a compute resource.
It’s not possible to deploy a model to a local web service for testing.
- True
- False
Answer: False
Explanation: Models can be deployed to a local web service, an Azure container, an IoT module, etc. Deploying to a local web service can be useful for scenario testing.
Azure Machine Learning support both real-time and batch predictions?
- True
- False
Answer: True
Explanation: Azure Machine Learning supports both real-time inference (online) and batch inference (offline), making it a flexible tool for various deployment scenarios.
Which of the following services could be used to deploy a trained machine learning model in Azure?
- Azure Functions
- Azure Kubernetes Service
- Azure API Management
Answer: All of the above
Explanation: All the above-mentioned services can be used to deploy a trained model in Azure. The choice depends on the specific use case and requirements of the deployment.
Azure Container Instances (ACI) are recommended for dev-test deployments.
- True
- False
Answer: True
Explanation: ACI is a good target for testing deployments because it provides a fast, simplified platform for deploying containerized apps, without orchestrations services.
You can deploy your model using previously not registered model (directly from local files).
- True
- False
Answer: True
Explanation: You can directly deploy a model from local files without registering it first, but the best practice is to always register models to keep track of versions.
By deploying your trained model as a web service, you are exposing a REST API.
- True
- False
Answer: True
Explanation: When you deploy a model as a web service in Azure, it exposes a REST API which can be used to send data and retrieve predictions from the model.
Deploying models on Edge devices is not supported in Azure.
- True
- False
Answer: False
Explanation: Azure supports Edge deployments, which can be beneficial for scenarios with intermittent connectivity, or local low-latency predictions.
Azure Machine Learning only supports deployment of models trained on Azure.
- True
- False
Answer: False
Explanation: Azure Machine Learning supports deployment of models regardless of where and how they were trained. It includes models trained in Azure, on-premises, or other cloud platforms.
Azure Machine Learning supports auto scaling for models deployed as a web service.
- True
- False
Answer: True
Explanation: Azure Machine Learning supports auto scaling for deployed models to manage the compute resources as per the application demand.
Models registered with Azure Machine Learning are versioned.
- True
- False
Answer: True
Explanation: Every time you register the same model with Azure Machine Learning, the registry increments the version of the model to manage and track the models.
Interview Questions
What is the primary purpose of publishing a trained model in Azure AI?
The primary purpose of publishing a trained model in Azure AI is to make it available for real-time scoring of new data inputs. This allows developers to integrate the model’s capabilities into applications and services.
What is the Azure service used to publish a trained machine learning model?
The Azure service used to publish a trained machine learning model is Azure Machine Learning.
When you’re publishing a model in Azure AI, what types of resources do you need to prepare?
Before you can publish a model in Azure AI, you’ll need computation resources (for example, an Azure Machine Learning Compute instance) and a workspace in Azure Machine Learning.
What are the steps involved in deploying a trained model with Azure Machine Learning?
The steps involved in deploying a trained model with Azure Machine Learning includes: Register the model, Prepare to deploy (includes defining inference configuration and deployment configuration), and Deploy the model using these configurations.
What role does the inference configuration play during publishing a trained model in Azure?
The inference configuration describes: how to set up the web service for predictions and what code to run when data come in. It includes the scoring script and environment (dependencies needed to run the model).
What Azure service can be used to automate machine learning workflows, which includes publishing models?
Azure Machine Learning pipelines can be used to automate machine learning workflows, including model training, validation, and deployment.
What are real-time endpoints in the context of deploying a trained model?
Real-time endpoints in Azure AI allow your model to provide real-time predictions. They listen for data over REST API calls, run that data through your trained model, and return a prediction.
How can updates be made to a published model in Azure AI?
Updates to a published model can be made by creating a new endpoint or updating an existing endpoint. The updated model is then re-published to make it available for apps and services.
What is the Azure Kubernetes service in relation to model deployment?
Azure Kubernetes service is a managed service that can be used to deploy containerized machine learning models at scale. It provides scalable, high-availability deployment options for your trained models.
What is the role of Azure Container Instances in publishing a trained model?
Azure Container Instances provides a straightforward, serverless option to host containers. When publishing a trained model, Azure Container Instances can be used for quick deployment testing and low-scale CPU-based workloads.
What are the benefits of using Azure Machine Learning SDK for Python in deploying models?
Using Azure Machine Learning SDK for Python provides the ability to automate every aspect of the machine learning lifecycle, including model training, validation, and deployment.
How do you secure a real-time endpoint in Azure AI?
Real-time endpoints in Azure AI are secured using key-based authentication or token-based authentication.
Can we roll back to a previously deployed model version if any issue occurs in Azure Machine Learning?
Yes, Azure Machine Learning provides functionality to revert to previous versions of a model if issues arise with the currently deployed version.
What is the purpose of Azure Machine Learning designer?
Azure Machine Learning designer is an interactive, visual workspace where developers can build, test, and deploy machine learning models without having to write code.
How do we choose between Azure Kubernetes Service (AKS) and Azure Container Instances (ACI) for model deployment?
The choice between AKS and ACI depends on the needs for scale, cost, and management overhead. For high-scale, production deployments, AKS is the recommended choice. For testing or low-scale, CPU-based workloads, ACI is often more suitable.