Cloud Services / Machine Learning

Build and Deploy a Machine Learning Model with Azure ML Service

18 Jan 2019 3:00am, by

This article is a post in a series on bringing continuous integration and deployment (CI/CD) practices to machine learning. Check back to The New Stack for future installments. For the background and context, we strongly recommend you to read the previous article on the rise of ML PaaS followed by the article on the overview of Azure ML service.

In this tutorial, we will build and deploy a machine model to predict the salary from the Stackoverflow dataset. By the end of this, you will be able to invoke a RESTful web service to get the predictions.

Since the objective to demonstrate the workflow, we will use a simple two-column dataset with years of experience and salary for the experiment. For the details on the dataset, refer to my previous article on linear regression.

Prerequisites

  1. Basic knowledge of Python and Scikit-learn
  2. Active Microsoft Azure Subscription
  3. Anaconda or Miniconda

Configuring the Development Environment
Configure a virtual environment with the Azure ML SDK. Run the below commands to install the Python SDK, and launching a Jupyter Notebook. Start a new Python 3 kernel from Jupyter.

Initializing Azure ML Environment

Let’s start by importing all the required Python modules, which include standard Scikit-learn modules and the Azure ML modules.

We need to create an Azure ML Workspace that acts as the logical boundary for our experiment. A Workspace creates a Storage Account for storing the dataset, a Key Vault for secrets, a Container Registry for maintaining the image repositories, and Application Insights for logging the metrics.

Don’t forget to replace the placeholder with your subscription id.

After a few minutes, we will see the resources created within the Workspace.

We can now create an Experiment to start logging the metrics. Since we don’t have many parameters to log, we are capturing the start time of the training process.

Training and Testing the Scikit-learn ML Model

We will now proceed to train and test the model through Scikit-learn.

The trained model will be serialized as a pickle file in the outputs directory. Azure ML automatically copies the content of the outputs directory to the cloud.

Let’s complete the experiment by logging the slope, intercept, and the end time of the training job.

We can track the metrics and the execution time from the Azure Dashboard.

Registering and Serving the Trained Model

Each time we freeze the model, it can be registered with Azure ML with a unique version. This gives us the ability to easily switch between different models when serving.

Let’s register the salary model from the above training job by pointing the SDK to the location of the PKL file. We are also adding some additional metadata to the model in the form of tags.

Check the Models section of the Workspace to ensure that our model is registered.

It’s time for us to package and deploy the model as a container image which will be exposed as a web service.

For the container image to get created, we need to tell Azure ML about the environment needed by the model. We will then pass a Python script that includes code to predict the values based on an inbound data point.

Azure ML API provides handy methods for both. Let’s first create the environment file, salenv.yaml, which tells the runtime to include Scikit-learn in the container image.

The below snippet, when executed from the Jupyter Notebook, creates a file called score.py that contains the inference logic for the model.

Now. let’s connect the dots by passing the inference file and environment configuration to the image.

This eventually results in the creation of a container image which shows up in the Images section of the Workspace.

We are all set to create the deployment configuration that defines the target environment and launching it as web service hosted in Azure Container Instance as a single-vm container. We may also choose AKS or an IoT Edge environment as the deployment target.

The Azure Resource Group now has an Azure Container Instance running the inference for the model.

We can get the URL of the inference service from the below method:

Let’s go ahead and invoke with the web service through cURL. We can do this from the same Jupyter Notebook.

You can access the dataset and Jupyter Notebook from the Github repo.

The uniqueness of this approach is that we could perform all the tasks from a Python kernel running inside the Jupyter Notebook. Developers can do everything it takes to train and deploy ML models from code. This is the real value of using an ML PaaS like Azure ML Service.

Feature image by Ricardo Gomez Angel on Unsplash.


A digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.