Introduction to AI Azure Machine Learning for Beginners

Introduction to AI Azure Machine Learning for Beginners

Lionel

1. What is AI?

image

Artificial Intelligence (AI) is the simulation of human intelligence and capabilities by computer software.

2. What is Machine Learning?

image

Machine Learning is a subcategory of AI where a computer software is "taught" to draw conclusions and make predictions from data.

3. What is Azure Machine Learning?

image

Azure Machine Learning is a cloud service for accelerating and managing the machine learning project lifecycle.

3.1. Azure Machine Learning Studio

image

The Azure Machine Learning studio offers multiple authoring experiences depending on the type of project and the level of your past ML experience, without having to install anything.

  • Notebooks: write and run your own code in managed Jupyter Notebook servers that are directly integrated in the studio.
  • Visualize run metrics: analyze and optimize your experiments with visualization.
  • Azure Machine Learning designer: use the designer to train and deploy machine learning models without writing any code. Drag and drop datasets and components to create ML pipelines.
  • Automated machine learning UI: Learn how to create automated ML experiments with an easy-to-use interface.
  • Data labeling: Use Azure Machine Learning data labeling to efficiently coordinate image labeling or text labeling projects.

3.2. Machine learning project workflow

Project lifecycle

While the project lifecycle can vary by project, it will often look like this:

image

A workspace organizes a project and allows for collaboration for many users all working toward a common objective. Users in a workspace can easily share the results of their runs from experimentation in the studio user interface or use versioned assets for jobs like environments and storage references.

Models can be deployed to the managed inferencing solution, for both real-time and batch deployments, abstracting away the infrastructure management typically required for deploying models.

Train models

In Azure Machine Learning, you can run your training script in the cloud or build a model from scratch.
Data scientists can use models in Azure Machine Learning that they've created in common Python frameworks, such as:

  • PyTorch
  • TensorFlow
  • scikit-learn
  • XGBoost
  • LightGBM

Other languages and frameworks are supported as well, including:

  • R
  • .NET

Deploy models

To bring a model into production, it's deployed. Azure Machine Learning's managed endpoints abstract the required infrastructure for both batch or real-time (online) model scoring (inferencing).

Batch scoring, or batch inferencing, involves invoking an endpoint with a reference to data. The batch endpoint runs jobs asynchronously to process data in parallel on compute clusters and store the data for further analysis.

Real-time scoring, or online inferencing, involves invoking an endpoint with one or more model deployments and receiving a response in near-real-time via HTTPs.

3.3. MLOps: DevOps for machine learning

DevOps for machine learning models, often called MLOps, is a process for developing models for production. A model's lifecycle from training to deployment must be auditable if not reproducible.

ML model lifecycle

image

MLOps is based on DevOps principles and practices that increase the efficiency of workflows. Examples include continuous integration, delivery, and deployment. MLOps applies these principles to the machine learning process, with the goal of:

  • Faster experimentation and development of models.
  • Faster deployment of models into production.
  • Quality assurance and end-to-end lineage tracking.

4. An example using Azure Machine Learning - Image Classification using DenseNet

Create an example by using Image Classification using DenseNet in Designer step by step as below

4.1. Prepare a dataset on local PC

We need to prepare a dataset with a list of singer faces.

Singer list

image

Singer faces list (Example: Den Vau)

image

We also need one or more singer face(s) for testing (Example: Den Vau)

image

4.2. Create Azure Machine Learning workspace

  • Enter Microsoft Azure Portal site.
  • At Search box: "Azure Machine Learning" > Create > New workspace
  • Input required information into create form same as creating other resources.
  • After completed, the result is as below.
image
Azure Machine Learning Workspace
  • Next, launch Azure ML Studio by clicking on Launch studio button
image
Azure Machine Learning Studio

4.3. Create Image Classification using DenseNet

In Azure ML studio, we need to create our own dataset (e.g., lioneldemo-dataset) firstly.

Create own dataset on Azure ML cloud

  • Data > Create
  • Data type > Type: File
  • Data source > From local files
  • Storage type > Datastore type: Azure Blob Storage
  • File or folder selection > Upload (Select folder singer_data from local PC to upload to the blob on cloud)
image
  • Review the result after uploaded images to the blob
image

Create Image Classification using DenseNet in Designer

  • Designer > Image Classification using DenseNet
image
  • Replace the default dataset by our dataset (lioneldemo-dataset) and submit

   - Before submitting, we need to complete settings include creating Azure ML compute instance for processing train model

image

 - Completed creating compute resource

image
  • Select Azure ML compute instance
image
  • After completed settings, click on Submit button to start train model processing
image

Train model

  • The train model in processing will be as below
image
  • After completed train model, we can check the result
  • The trained model will be outputted here Train PyTorch Model > Outputs + logs > trained_model_outputs
image

※ Note: You can download the trained model and deploy it to any platform that support AI Machine Learning.

  • The evaluation result is here Evaluate Model > Metrics
image

Deploy model

  • In order to deploy model, we need to create an endpoint
  • Firstly, create real-time inference pipeline by Jobs > [Job name] > Create inference pipeline > Create real-time inference pipeline
image
  • After the real-time inference pipeline was created, the Web Service Input and the Web Service Output were also added
image
  • Next, create a new pipeline job
image
  • Finally, click on Deploy to set up real-time endpoint

   - Compute type: Azure Container Instance

   - CPU reserve capacity: 1: (Recommended ≧ 1)

   - Memory reserve capacity: 3 (Recommended ≧  3)

image
  • The endpoint was created successfully, now we can use this endpoint
image

View the endpoint information and test with the endpoint

  • Tab Details
image
  • Tab Test
image
  • How to view the test result:

   - "Scored Probabilities_Den_Vau": 0.774731457233429 ← The accuracy is about 77%.

   - "Scored Labels": "Den_Vau" ← The trained model identifies the singer to be "Den_Vau".

  • Tab Consume
image

Test with python code

     - This source code was copied from Consumption types: Python, I also modified some codes for testing easily.

     - To execute this source code, you can use IDE Visual Studio Code or any text editor for editing and execute command python singer_test.py on console.

# singer_test.py
import urllib.request
import json
import os
import ssl
import base64 # [Lionel updated] Added for converting from image data to 64 bit string.

def allowSelfSignedHttps(allowed):
    # bypass the server certificate verification on client side
    if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None):
        ssl._create_default_https_context = ssl._create_unverified_context

allowSelfSignedHttps(True) # this line is needed if you use self-signed certificate in your scoring service.

# Request data goes here
# The example below assumes JSON formatting which may be updated
# depending on the format your endpoint expects.
# More information can be found here:
# https://docs.microsoft.com/azure/machine-learning/how-to-deploy-advanced-entry-script

# [Lionel updated] Use a singer face to test accuracy with the trained model.
# For testing easily, the test data folder "singer_test" will be put in the same folder with this source file.
pngFile = "singer_test\\Den_Vau\\15.png"
with open(pngFile, "rb") as img_file:
    b64_bin = base64.b64encode(img_file.read())
b64_string = b64_bin.decode('utf-8')
b64_string = "data:image/png;base64," + b64_string

# [Lionel updated] Write the 64 bit of image data into .txt file for another test if any.
f = open(pngFile + ".txt", "w")
f.write(b64_string)
f.close()

data =  {
  "Inputs": {
    "WebServiceInput0": [
      {
        "image": "\"" + b64_string + "\"", # [Lionel updated] Replaced the raw string by a string variable.
        "id": 0,
        "category": ""
      }
    ]
  },
  "GlobalParameters": {}
}

body = str.encode(json.dumps(data))

url = 'http://e419e3bf-c759-45d4-945e-3c1f7171d53d.australiaeast.azurecontainer.io/score'
api_key = 'mmr6IzFdMyt1Eps8b5MuQfIpQPr20w5T' # Replace this with the API key for the web service

# The azureml-model-deployment header will force the request to go to a specific deployment.
# Remove this header to have the request observe the endpoint traffic rules
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}

req = urllib.request.Request(url, body, headers)

try:
    response = urllib.request.urlopen(req)

    result = response.read()
    print(result.decode('utf-8'))
except urllib.error.HTTPError as error:
    print("The request failed with status code: " + str(error.code))

    # Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
    print(error.info())
    print(error.read().decode("utf8", 'ignore'))
  • Tab Deployment logs
image

5. Summary

This blog introduces the most basic knowledge about Azure AI Machine Learning (Azure AI ML) to help learners quickly grasp knowledge about Azure AI ML in the shortest time.

The example in this blog is just stopping at the level of using the built-in components in the form of drag and drop by the web interface to help learners visualize how Azure AI ML works quickly.

6. References

  1. What is Azure Machine Learning?
  2. AZ-900 Episode 16 | Azure Artificial Intelligence (AI) Services | Machine Learning Studio & Service
  3. Azure ML and Computer Vision Running on Azure Functions

Thank you!