My Brain Cells

Easiest (and best) learning materials for anyone with a curiosity for machine learning and artificial intelligence, Deep learning, Programming, and other fun life hacks.

How To Deploy a ML Model with Azure ML Service

Prerequisites:

  • Python and Scikit-learn installed
  • Active Microsoft Azure Subscription
  • Anaconda or Miniconda

First things first we need to configure a virtual environment with Azure ML SDK

For that, we need to open a new jupyter notebook and install python through the terminal. by following commands

$ conda create -n demo -y python=3.6
$ conda activate demo
$ conda install nb_conda
$ pip install azureml-sdk[notebooks]
$ jupyter notebook

A new jupyter notebook is create and server on your default browser. now we need to initialize the Azure ML environment

Azure ML Environment

# demo.ipynb

import datetime
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Linear Regression
from sklearn.externals import joblib
import azureml.core
from azureml.core import Workspace
from azureml.core.model import Model
from azureml.core import Experiment
from azureml.core.webservice import Webservice
from azureml.core.image import Container Image
from azureml.core.webservice import AciWebservice
from azureml.core.conda_dependencies import CondaDependencies

We need to create a workspace for the Azure ML workspace which acts as a logical bound for our environment.

Don’t forget to add your subscription id

WS=Workspace.create(name='salary',
                      subscription_id='{your id}',
                      resource_group='mi2',
                      create_resource_group=True,
                      location='southeastasia
                    )

Wait for a few minutes and you will see that resources are created within the workspace

Workspace

Now we can create an experiment to start logging the metrics by

exp Experiment(workspace=ws,name='salexp')
run=exp.start_logging()
run.log("Experiment start time",str(datetime.datetime.now()))

Training a ML Model

We train and test the model through scikit-learn

sal=pd.read_csv('data/sal.csv',header=0,index_col=None)
X=sal[['x']]
y=sal['y']
X_train,X_test,y_train,y_test=train_test_split(x,y,test_size=0.25,random_state= 1)
1m=Linear Regression()
lm.fit(X_train,y_train)

Azure ML automatically copies the content of the outputs directory to the cloud.

After fitting the model we have to save the model as a .pkl file

filename = 'outputs/sal_model.pkl'
joblib.dump(lm, filename)

We can also log the slope, intercept, and the end time of the training job.

run.log('Intercept:',lm.intercept_)
run.log('Slope:',lm.coef_[0])
run.log("Experiment end time",str(datetime.datetime.now()))
run.complete()

Registering and serving the ML Model

We can register a model with azure ml for every fgreeze model which gives us a unique version. This gives us the ability to easily switch between different models when serving.

Now let’s register our salary model from above PKL file.

model=Model.register(model_path="outputs/sal_model.pkl",
                       model_name="sal_model",
                       tags={"key":"1"},
                       description="Salary Prediction",
                       workspace=ws)
The workspace of our model is registered

Finally, we have to pack and deeply the model as container images which will be exposed as a web service.

creating a container image, let’s first create the environment file, salenv.yaml

# salenv.yaml

salenv=CondaDependencies()
salenv.add_conda_package("scikit-learn")
with open("salenv.yml","w")asf:
    f.write(salenv.serialize_to_string())
with open("salenv.yml","r")asf:
    print(f.read())

More important part, the below snippet or notebook, creates a file called score.py that contains the inference logic for the model.

%% writefile score.py

import json
import numpy as np
import os
import pickle
from sklearn.externals import joblib
from sklearn.linear_model import LogisticRegression

from azureml.core.model import Model

def init():
    global model
    #retrieve the path to the model file using the model name
    model_path=Model.get_model_path('sal_model')
    model=joblib.load(model_path)

def run(raw_data):
    data=np.array(json.loads(raw_data)['data'])
    #make prediction
    y_hat=model.predict(data)
    return json.dumps(y_hat.tolist())

now connect inference file and the environment configuration to the image.

%% time
image_config Container Image.image_configuration(execution_script="score.py",
                                                    runtime="python",
                                                    conda_file="salenv.yml")

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.

aciconfig = Aciwebservice.deploy_configuration(cpu_cores = 1,
                                               memeory_gb = 1,
                                         tags={"data": "salary",                      "method": "GET", description='Perdict')

service= Webservice.deploy_from_model(workspace=ws,
                             name='salary-svc',
                             deployment_config=aciconfig,
                             models=[model],
                             image_config=image_config)

service.wait_for_deployment(show_output=True)

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

Get the URL of the inference service form the below method:

invoke with the web service through cURL

you can access the datasets and the code from the github

Anthony

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top