Skip to content

ErkanHatipoglu/nd00333_AZMLND_C2

 
 

Repository files navigation

Operationalizing Machine Learning

In this project, we are working with the Bank Marketing dataset. We use Azure to configure a cloud-based machine learning production model using AutoML, deploy it, and consume it. We are also creating, publishing, and consuming a pipeline.

This dataset is about a phone call marketing campaign. The original data can be found @UC Irvine Machine Learning Repository. The dataset can be used (as we are using) to predict if the client will subscribe to a term deposit or not. The target variable is y.

The lab environment provided by Udacity will not be used for this project. Instead, a local development environment along with a Microsoft Azure account will be used.

Architectural Diagram

In this project, We are following the below steps:

  1. Authentication
  2. Automated ML Experiment
  3. Deploy the Best Model
  4. Enable Logging
  5. Swagger Documentation
  6. Consume Model Endpoints
  7. Create and Publish a Pipeline
  8. Documentation

Main Steps

Image by Udacity

Key Steps

  1. Authentication

    Authentication is crucial for the continuous flow of operations. Continuous Integration and Delivery system (CI/CD) rely on uninterrupted flows. When authentication is not set properly, it requires human interaction and thus, the flow is interrupted. An ideal scenario is that the system doesn't stop waiting for a user to input a password. So whenever possible, it's good to use authentication with automation.

    A “Service Principal” is a user role with controlled permissions to access specific resources. Using a service principal is a great way to allow authentication while reducing the scope of permissions, which enhances security.

    We will use local environment for Authentication.

    Main operations in the Authentication step are as follows:

    • Use Git Bash to sign in Microsoft account using the az login command.

    Git Bash screen showing the result 'az login' command

    Authentication_rm_2.png

    • Ensure the az command-line tool is installed along with the ml using the az extension add -n azure-cli-ml command.

    Git Bash screen showing the result 'az extension add -n azure-cli-ml' command

    Authentication_rm_3.png

    • Create the Service Principal with az after login in using the az ad sp create-for-rbac --sdk-auth --name ml-auth command.

    Git Bash screen showing the result 'az ad sp create-for-rbac --sdk-auth --name ml-auth' command

    Authentication_rm_4.png

    • Capture the "objectId" using the clientID. Use the following command:

    az ad sp show --id xxxxxxxx-3af0-4065-8e14-xxxxxxxxxxxx

    Git Bash screen showing the result 'az ad sp show --id xxxxxxxx-3af0-4065-8e14-xxxxxxxxxxxx' command

    Authentication_rm_5.png

    • Assign the role to the new Service Principal for the given Workspace, Resource Group, and User objectId. You will need to match your workspace, subscription, and ID. There should be no error in the output. Use the following command:

    az ml workspace share -w xxx -g xxx --user xxxxxxxx-cbdb-4cfd-089f-xxxxxxxxxxxx --role owner

    Git Bash screen showing the result 'az ml workspace share -w xxx -g xxx --user xxxxxxxx-cbdb-4cfd-089f-xxxxxxxxxxxx --role owner' command

    Authentication_rm_6.png

  2. Automated ML Experiment

    In this step, we will create an experiment using Automated ML, configure a compute cluster, and use that cluster to run the experiment. We will use Azure portal for this purpose.

    We will use the Bank Marketing dataset described above.

    In the following section we will deploy the best model of this AutoML experiment.

    We need to configure a compute cluster for this AutoML experiment. To do that we can either use an existing cluster or create a new one. We will create a new cluster with the following configuration:

    • Region: eastus2
    • Virtual machine priority: Low priority
    • Virtual machine type: CPU
    • Virtual machine size: Standart_DS12_v2
    • Compute name: auto-ml
    • Minimum number of nodes: 1
    • Maximum number of nodes: 2

    Run configuration for the autoML experiment is as follows:

    • Experiment name (Create new): ml-experiment-1
    • Target column: y
    • Compute cluster name: auto-ml
    • Primary metric: Accuracy
    • Explain best model: Selected
    • Exit criterion:
      • Training job time (hours): 1
    • Concurrency:
      • Max concurrent iterations: 2

    Main operations in the Automated ML Experiment step are as follows:

    • Upload the bankmarketing_train.csv to Azure Machine Learning Studio so that it can be used when training the model.

    Bank Marketing dataset

    automl_rm_dataset_1.png

    • Create a new AutoML run and select Bankmarketting Dataset

    New AutoML Run

    automl_1.png

    • Create a new AutoML experiment

    A New Experiment

    AutoML_4.png

    • Configure a new compute cluster

    Create Compute Cluster

    AutoML_3.png

    • Run the experiment using classification, ensure 'Explain best model' is checked

    Additional Configurations

    AutoML_5.png

    AutoML Run with status 'Running'

    AutoML_8.png

    • Wait for the experiment to finish and explore the best model

    AutoML Run with Status 'Completed'

    AutoML_9.png

    Automated ML Tab Showing the Completed Experiment

    AutoML_10.png

    Trained Models for Each Run

    AutoML_11.png

    Best Model: VotingEnsemble

    AutoML_12.png

  3. Deploy the Best Model

    The best model in the previous step was a VotingEnsemble. Deploying the Best Model will allow to interact with the HTTP API service and interact with the model by sending data over POST requests.

    We will use Azure portal for deployment.

    Main operations in Deploy the best model step are as follows:

    • Select the best model for deployment

    Best Model Selected (Deploy status: No deployment yet)

    Deploy_1.png

    • Deploy the model and enable Authentication

    Best Model Selected (Deploy status: Running)

    Deploy_2.png

    • Deploy the model using Azure Container Instance

    Deployed Model (Endpoint) With a Healthy Deployment State

    Deploy_5.png

  4. Enable Logging

    We can now enable Application Insights and retrieve logs. Application Insights is a special Azure service that provides key facts about an application. It is a very useful tool to detect anomalies and visualize performance.

    We will work on local environment in this step. But first, we need to download config.json from Azure portal.

    Main operations in the Enable Logging step are as follows:

    • Download config.json from ML Studio

    How to download 'config.json'

    App-In_8.png

    • Write and run the code (logs.py) to enable Application insights

    logs.py output in Git Bash

    App-In_5.png

    'Application Insights url' in Model Endpoint

    App-In_6.png

    • Explore Application insights

    Application Insights

    App-In_7.png

  5. Swagger Documentation

    In this step, we will consume the deployed model using Swagger. Swagger is a tool that helps build, document, and consume RESTful web services like the ones we are deploying in Azure ML Studio.

    We will work on local environment in this step. But first, we need to download swagger.json from Azure portal. We can download swagger.json using endpoints tab in Azure ML Studio.

    Main operations in the Swagger Documentation step are as follows:

    • Download the swagger.json file.

    Azure provides a swagger.json that is used to create a web site that documents the HTTP endpoint for a deployed model. We can find swagger.json URI in the Endpoints section.

    Swagger URI

    Swagger_8.png

    We need to download the swagger.json to the swagger folder. There should be 3 files in the swagger folder.

    Local Directory

    Swagger_2.png

    • Run the swagger.sh in git bash.

    'swagger.sh' Output

    Swagger_3.png

    • Run the serve.py in another git bash.

    'serve.py' Output

    Swagger_4.png

    • Interact with the swagger instance running with the documentation for the HTTP API for the model.

    To do that we will first use the http://localhost/ in our browser to interact with the swagger instance running with the documentation for the HTTP API of the model then we will use http://localhost:8000/swagger.json to display the contents of the API for the model.

    Swagger Page

    Swagger_5.png

    More on Swagger Page

    Swagger_6.png

    More on Swagger Page

    Swagger_7.png

  6. Consume Model Endpoints

    We can consume a deployed service via an HTTP API. An HTTP API is an URL that is exposed over the network so that interaction with a trained model can happen via HTTP requests.

    Users can initiate an input request, usually via an HTTP POST request. HTTP POST is a request method that is used to submit data. The HTTP GET is another commonly used request method. HTTP GET is used to retrieve information from a URL. The allowed request methods and the different URLs exposed by Azure create a bi-directional flow of information.

    The APIs exposed by Azure ML will use JSON (JavaScript Object Notation) to accept data and submit responses. It served as a bridge language among different environments.

    We will work on local environment in this step. But first, we need to get scoring_uri and the keyfrom Azure portal.

    Main operations in the Consume Model Endpoints step are as follows:

    • Modify both the scoring_uri and the key (in endpoint.py) to match the key for our service and the URI that was generated after deployment.

    scoring_uri and the key can be found on the 'consume' tab of the model endpoint.

    Model Endpoint (Consume Tab)

    CME_2.png

    • Run endpoint.py

    'endpoint.py' Output

    CME_3.png

    Benchmarking

    A benchmark is used to create a baseline or acceptable performance measure. Benchmarking HTTP APIs is used to find the average response time for a deployed model.

    One of the most significant metrics is the response time since Azure will timeout if the response times are longer than sixty seconds.

    Apache Benchmark is an easy and popular tool for benchmarking HTTP services.

    We will work on local environment in this step.

    Main operations in the Benchmarking step are as follows:

    • Make sure the Apache Benchmark command-line tool is installed and available in your path.

    • In the endpoint.py, replace the key and URI again

    • Run endpoint.py. A data.json file should appear

    • Run the benchmark.sh file.

    'benchmark.sh' Output

    bm_0.png

    More on 'benchmark.sh' Output

    bm_1.png

  7. Create and Publish a Pipeline

    For this part of the project, we will use the Jupyter Notebook provided in the starter files folder and the Azure portal.

    Main operations in the Create and Publish a Pipeline step are as follows:

    • Upload the Jupyter Notebook

    Jupyter Notebook on Azure Machine Learning Studio

    pipeline_1.png

    • Update all the variables that are noted to match the environment and make sure that a config.json has been downloaded and is available in the current working directory.

    Directory Structure

    pipeline_2.png

    • Run through the cells

    Jupyter Notebook Output - Dataset

    pipeline_3.png

    Below you can find some important screenshots for this stage:

    Running Pipeline on Azure Machine Learning Studio

    pipeline_6.png

    Running and Completed Pipelines on Azure Machine Learning Studio

    pipeline_s_1.png

    Pipeline Endpoints on Azure Machine Learning Studio

    pipeline_s_2.png

    'Pipeline run overview' for Run 61 on Azure Machine Learning Studio

    pipeline_14.png

    'Published pipeline overview' for Bankmarketting Dataset on Azure Machine Learning Studio

    pipeline_s_3.png

    Run Details Output on Jupyter Notebook

    pipeline_8.png

    More Run Details Output on Jupyter Notebook

    pipeline_9.png

    More Run Details Output on Jupyter Notebook

    pipeline_12.png

    More Run Details Output on Jupyter Notebook

    pipeline_13.png

    Published Pipeline on Jupyter Notebook

    pipeline_19.png

    More Run Details Output on Jupyter Notebook

    pipeline_s_5.png

    'Experiment Run status' on Azure Machine Learning Studio

    pipeline_10.png

    'Azure Machine Learning Studio Home Page with Runs and Computes

    pipeline_22.png

    'Experiment Run status' on Azure Machine Learning Studio

    pipeline_s_9.png

Screen Recording

Project 2 Operationalizing Machine Learning Screencast

Note: You can refer to Screencast_text.txt if you have difficulty understanding screencast audio.

Future Work

Improvement for future experiments

The dataset is imbalanced. Although AutoML seems to handle imbalanced data we can try to handle it manually.

TensorFlow LinearClassifier and TensorFlow DNN seem to be blocked since they are not supported by AutML. We can try some HyperDrive runs with these estimators and see their performance.

Deep learning is disabled. We can include Deep learning and run a new AutoML pipeline.

We can select a smaller size of predictors using the feature importance visualizations generated by the model explainability feature of Azure AutoML and run a new AutoML pipeline. By doing this we may get better performance for our model.

We can try new HyperDrive runs with the best performing estimator (VotingEnsemble) to get a better score.

We can monitor the endpoint using Application Insights and detect performance anomalies and visualize performance. We can easily evaluate performance and identify pitfalls since we have already started benchmarking.

References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 94.6%
  • Python 3.0%
  • Shell 2.4%