How to Deploy a Local ML Model as a Web Service on Azure Machine Learning Studio
By: Rahul Banerjee
Tags: Ml Model Deployment, Azure Machine Learning, Deployment, Azure Services, Azure

How to Deploy a Local ML Model as a Web Service on Azure Machine Learning Studio

Azure Machine Learning Studio (MLS) is a service provided by Microsoft which lets you deploy your models as a web service and consume it as a REST endpoint. It is really useful when you are trying to integrate your web app/ API with Machine Learning models Rahul Banerjee Dec 16, 2020·6 min read

Photo by Caspar Camille Rubin on Unsplash

Introduction

In this tutorial, I will show you how to deploy your model as a web service. First, we will create a simple KNN model on the iris-dataset and then deploy it. I will also show you how to consume the deployed model and mention some points to keep in mind while deploying the model.

You can find the repository for this article over here

You will need a Microsoft Azure account for this tutorial.

Setup Virtual Environment

pip install virtualenv /* Install virtual environment */

virtualenv venv /* Create a virtual environment */

venv/Scripts/activate /* Activate the virtual environment */

Create your Model

For this tutorial, we won’t be creating a fancy model, we will be creating a simple KNN model to use on the iris dataset provided by the sklearn library

Import Libraries

import pandas as pd

from sklearn.preprocessing import MinMaxScaler

from sklearn.datasets import load_iris

from sklearn.neighbors import KNeighborsClassifier

from sklearn.model_selection import train_test_split

import joblib

import pickle

Load Dataset

data = load_iris()

df = pd.DataFrame(data.data , columns = data.feature_names)

print("Loaded Data Frame")

scaler = MinMaxScaler()

df = scaler.fit_transform(df)

X_train , X_test, y_train, y_test = train_test_split(df , data.target , test_size = 0.2)

print("Created Train/Test Split")

Since we are using a KNN, which uses Euclid Distances, we will need to scale the data before using it

Then we will split the data into a test/training set

Create Model

train_score , test_score = [] , []

for k in range(1,50):

model = KNeighborsClassifier(n_neighbors = k)

model.fit(X_train , y_train)

test_score.append(model.score(X_test , y_test))

train_score.append(model.score(X_train, y_train))

Chose an appropriate value for k based on the values in train and test scores

After you have selected a value for k, try predicting with your own input values

print ( model.predict(scaler.fit_transform([[1,1,1,1]]))[0])

Save the Model

# To dump

f = 'model/knn.pkl'

with open(f, 'wb') as file:

pickle.dump(model, file) # To load

loaded_model = joblib.load(f)

Make sure your model works as expected by predicting the classification for custom input

loaded_model.predict(scaler.fit_transform( [[2,0,2,10]]))

Now we will move on to the deployment

Azure ML Studio

We will be using the azureml sdk for Python for deployment, you can check our this resource by Microsoft for reference

Install Required Libraries

pip install azureml , azureml-core

Import the Libraries

from azureml.core import Workspace

from azureml.core.webservice import AciWebservice

from azureml.core.webservice import Webservice

from azureml.core.model import InferenceConfig

from azureml.core.environment import Environment

from azureml.core import Workspace

from azureml.core.model import Model

from azureml.core.conda_dependencies import CondaDependencies

If you get an error ‘ azureml has no module .core’ or ‘Module azureml.core not found’, try installing the following library