avatarRahul Banerjee

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

7464

Abstract

<span class="hljs-string">[[2,0,2,10]]</span>))</pre></div><p id="772c" type="7">Now we will move on to the deployment</p><h1 id="80b2">Azure ML Studio</h1><p id="1cdd">We will be using the azureml sdk for Python for deployment, you can check our this resource by Microsoft for reference</p><div id="1425" class="link-block"> <a href="https://docs.microsoft.com/en-us/python/api/overview/azure/ml/?view=azure-ml-py"> <div> <div> <h2>Azure Machine Learning SDK for Python - Azure Machine Learning Python</h2> <div><h3>Data scientists and AI developers use the Azure Machine Learning SDK for Python to build and run machine learning…</h3></div> <div><p>docs.microsoft.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*T-JMDltwjwsw5U5k)"></div> </div> </div> </a> </div><h2 id="ca81">Install Required Libraries</h2><div id="df3c"><pre>pip <span class="hljs-keyword">install</span> azureml , azureml-core</pre></div><h2 id="a057">Import the Libraries</h2><div id="5243"><pre><span class="hljs-title">from</span> azureml.core <span class="hljs-keyword">import</span> Workspace <span class="hljs-title">from</span> azureml.core.webservice <span class="hljs-keyword">import</span> AciWebservice <span class="hljs-title">from</span> azureml.core.webservice <span class="hljs-keyword">import</span> Webservice <span class="hljs-title">from</span> azureml.core.model <span class="hljs-keyword">import</span> InferenceConfig <span class="hljs-title">from</span> azureml.core.environment <span class="hljs-keyword">import</span> Environment <span class="hljs-title">from</span> azureml.core <span class="hljs-keyword">import</span> Workspace <span class="hljs-title">from</span> azureml.core.model <span class="hljs-keyword">import</span> Model <span class="hljs-title">from</span> azureml.core.conda_dependencies <span class="hljs-keyword">import</span> CondaDependencies</pre></div><p id="b605" type="7">If you get an error ‘ azureml has no module .core’ or ‘Module azureml.core not found’, try installing the following library</p><div id="4641"><pre>pip <span class="hljs-keyword">install</span> azureml.core</pre></div><p id="3c52">If you are using<a href="https://github.com/Azure/MachineLearningNotebooks/issues/1285"> Python3.9+ use — ignore-requires-python</a> when installing the packages.</p><h2 id="85be">Create Workspace</h2><p id="a702">We can either create the workspace manually on Azure or use Python to create it. If you create it manually, access the workspace and download the config.json file. Below is how you can create it in Python</p><div id="b1c4"><pre>ws = Workspace.create( <span class="hljs-attribute">name</span>=<span class="hljs-string">'myworkspace'</span>,
<span class="hljs-attribute">subscription_id</span>=<span class="hljs-string">'<azure-subscription-id>'</span>,
<span class="hljs-attribute">resource_group</span>=<span class="hljs-string">'myresourcegroup'</span>,
<span class="hljs-attribute">create_resource_group</span>=<span class="hljs-literal">True</span>,
<span class="hljs-attribute">location</span>=<span class="hljs-string">'eastus2'</span>
)</pre></div><ul><li>name: The name of your workspace</li><li>subscription_id: Your Azure Subscription ID</li><li>resource_group: The name of your resource Group</li><li>Set the create_resource_group to True if you want to create a new resource group. If you want to use an existing resource group, set it to False</li><li>Set the location</li></ul><p id="72ba">Once it is created, save the details of the workspace in a config.json file to use the workspace later.</p><div id="e1b6"><pre>ws<span class="hljs-selector-class">.write_config</span>()</pre></div><p id="be5e">Create a folder named ‘.azureml’ and save the json file inside it. Make sure you name the folder and json file correctly. Python will look for the config.json file inside this folder while loading up your workspace</p><p id="df82">You won’t need to create a workspace every time, in the future, you can use the following command to load the workspace</p><div id="7fc2"><pre><span class="hljs-attribute">ws</span> <span class="hljs-operator">=</span> Workspace.from_config()</pre></div><h2 id="339f">Register the Model</h2><p id="8311">We will use the pickle file we created earlier to register our model.</p><div id="c740"><pre><span class="hljs-keyword">model</span> = <span class="hljs-keyword">Model</span>.register(workspace = ws, model_path =<span class="hljs-string">"model/knn.pkl"</span>, model_name = <span class="hljs-string">"knn"</span>, tags = {<span class="hljs-string">"version"</span>: <span class="hljs-string">"1"</span>}, description = <span class="hljs-string">"Iris classification"</span>, )</pre></div><ul><li>workspace: The workspace object we created</li><li>model_path: the path to the pickle file</li><li>model_name: Name of the model on Azure MLS</li><li>tags: Although not necessary, you can add tags to your model</li><li>description: A description of your model</li></ul><h2 id="018f">Create an Environment</h2><p id="53a1">We will need to create an environment similar to our local virtual environment</p><div id="2f0b"><pre><span class="hljs-comment"># to install required packages</span> <span class="hljs-attr">env</span> = Environment(<span class="hljs-string">'env'</span>)</pre></div><div id="12f8"><pre>cd = CondaDependencies<span class="hljs-selector-class">.create</span>(pip_packages=<span class="hljs-selector-attr">[<span class="hljs-string">'pandas==1.1.5'</span>, <span class="hljs-string">'azureml-defaults'</span>,<span class="hljs-string">'joblib==0.17.0'</span>]</span>, conda_packages = <span class="hljs-selector-attr">[<span class="hljs-string">'scikit-learn==0.23.2'</span>]</span>) env<span class="hljs-selector-class">.python</span><span class="hljs-selector-class">.conda_dependencies</span> = cd</pre></div><div id="5890"><pre><span class="hljs-comment"># Register environment to re-use later</span> env.register(workspace = ws) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Registered Environment"</span>)</pre></div><ul><li>You can pass the name as a parameter to the Environment Instance</li><li>Add dependencies as needed</li><li>Register the environment so it can be used later on</li></ul><div id="d627"><pre>myenv = Environment.<span class="hljs-built_in">get</span>(<span class="hljs-attribute">workspace</span>=ws, <span class="hljs-attribute">name</span>=<span class="hljs-string">"env"</span>)</pre></div><p id="e62e">This get’s the environment you just created.</p><p id="f33c">Confirm that all the required libraries have been installed by creating a .yml based on the created environment. Ensure all the libraries are mentioned inside the .yml file</p><div id="30f6"><pre>myenv.save_to_directory(<span class="hljs-string">'./environ'</span>, <span class="hljs-attribute">overwrite</span>=<span class="hljs-literal">True</span>)</pre></div><p id="25f0">This will create a new folder called environ with a .yml and a .json file inside it</p><h2 id="

Options

15fc">Config Objects</h2><div id="8d14"><pre>aciconfig = AciWebservice.deploy_configuration( <span class="hljs-attribute">cpu_cores</span>=1, <span class="hljs-attribute">memory_gb</span>=1, tags={<span class="hljs-string">"data"</span>:<span class="hljs-string">"iris classifier"</span>}, <span class="hljs-attribute">description</span>=<span class="hljs-string">'iRIS cLASSIFICATION knn MODEL'</span>, )</pre></div><p id="0d28">Create a container instance and set the number of cpu_cores and memory_gb based on your requirements.</p><div id="d4dd"><pre>inference_config = InferenceConfig(<span class="hljs-attribute">entry_script</span>=<span class="hljs-string">"score.py"</span>, <span class="hljs-attribute">environment</span>=myenv)</pre></div><p id="a0f3">Create and InferenceConfig instance to link your environment and entry script. We will be creating an entry script below. The entry_script parameter should be set to the path to entry scrip.</p><h2 id="a708">Entry Script</h2><p id="cdc6">The entry script will two functions, an init function and a run function.</p><div id="4d30"><pre>def <span class="hljs-built_in">init</span>(): global model model_path = Model.<span class="hljs-built_in">get_model_path</span>(<span class="hljs-string">"knn"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Model Path is "</span>, model_path) model = joblib.<span class="hljs-built_in">load</span>(model_path)</pre></div><ul><li>Create a global variable called model</li><li>Load the model using the name of the model you registered earlier</li><li>Load the model from the path</li></ul><div id="cdab"><pre>def run(<span class="hljs-keyword">data</span>): <span class="hljs-keyword">try</span>: <span class="hljs-keyword">data</span> = json.loads(<span class="hljs-keyword">data</span>) result = model.predict(<span class="hljs-keyword">data</span>[<span class="hljs-string">'data'</span>]) <span class="hljs-keyword">return</span> {<span class="hljs-string">'data'</span> : result.tolist() , <span class="hljs-string">'message'</span> : <span class="hljs-string">"Successfully classified Iris"</span>} except Exception as e: error = str(e) <span class="hljs-keyword">return</span> {<span class="hljs-string">'data'</span> : error , <span class="hljs-string">'message'</span> : <span class="hljs-string">'Failed to classify iris'</span>}</pre></div><p id="4c6d">Save this file.</p><h2 id="d94e">Deploy the Model</h2><div id="1a3c"><pre><span class="hljs-attr">service</span> = Model.deploy(workspace=ws, <span class="hljs-attr">name</span>=<span class="hljs-string">'iris-model'</span>, <span class="hljs-attr">models</span>=[model], <span class="hljs-attr">inference_config</span>=inference_config, <span class="hljs-attr">deployment_config</span>=aciconfig, <span class="hljs-attr">overwrite</span> = <span class="hljs-literal">True</span>)</pre></div><div id="77ed"><pre>service.wait_for_deployment(<span class="hljs-attribute">show_output</span>=<span class="hljs-literal">True</span>) url = service.scoring_uri <span class="hljs-built_in">print</span>(url)</pre></div><p id="27e6">Finally, we will deploy the model by combining our config objects, workspace and model together. Deployment usually takes a few minutes.</p><p id="b15e">If your deployment is successful, the rest endpoint will be printed out. If it is unsuccessful, you can access the deployment logs</p><div id="ee9d"><pre>from azureml<span class="hljs-selector-class">.core</span> import Webservice service = <span class="hljs-built_in">Webservice</span>(ws,<span class="hljs-string">'iris-model-deployment'</span>) <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(service.get_logs()</span></span>)</pre></div><p id="6af8">Check the conclusion section for some things to keep in mind while deploying.</p><h1 id="8b60">Consume the Endpoint</h1><div id="3baf"><pre>import requests data = { <span class="hljs-string">'data'</span>: scaler<span class="hljs-selector-class">.fit_transform</span>(<span class="hljs-selector-attr">[[0.9,0.1,0.1,0.9]</span>])<span class="hljs-selector-class">.tolist</span>() } headers = {<span class="hljs-string">'Content-Type'</span>:<span class="hljs-string">'application/json'</span>} r = requests<span class="hljs-selector-class">.post</span>(url, str<span class="hljs-selector-class">.encode</span>(json<span class="hljs-selector-class">.dumps</span>(data)), headers = headers) <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(r.status_code)</span></span> <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(r.json()</span></span>)</pre></div><p id="3282">We will use the requests library to make a request to the endpoint.</p><h1 id="309f">Conclusion</h1><ul><li>If you get an error related to Buffer Mismatch while deploying, ensure that your pkl file is created using a 64-bit Python</li><li>If you get any error related to ‘Module not found’, check the .yml file generated for your environment and ensure that all required libraries are present</li><li>If needed, specify the version of the library to be installed</li><li>Try running your score.py file locally to ensure there are no syntax errors. You can also invoke the init and the run function to make a prediction. To run it locally, create a workspace object from your config.json file and while accessing the model pass the workspace as a parameter</li></ul><div id="0b39"><pre><span class="hljs-attr">model_path</span> = Model.get_model_path(<span class="hljs-string">"knn"</span> , workspace = ws)</pre></div><p id="7683">Let me know if you face any errors and I will try to help you out based on my experience :)</p><p id="7dc4">I recently created a blog using WordPress, I would love it if you could check it out 😃</p><div id="8edd" class="link-block"> <a href="https://realpythonproject.com/"> <div> <div> <h2>Python Project Tutorials — Improve your CV/Portfolio with these Python Project Tutorials.</h2> <div><h3>Deploy your Machine Learning Web App using Streamlit Sharing In my previous articles, I have talked about building a…</h3></div> <div><p>realpythonproject.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div> </div> </div> </a> </div><p id="826f">Connect with me on LinkedIn</p><div id="536c" class="link-block"> <a href="https://www.linkedin.com/in/rahulbanerjee2699/"> <div> <div> <h2>Rahul Banerjee — Product Engineering Intern — EY | LinkedIn</h2> <div><h3>View Rahul Banerjee’s profile on LinkedIn, the world’s largest professional community. Rahul has 4 jobs listed on their…</h3></div> <div><p>www.linkedin.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*Nk1E7yaVoclEl42s)"></div> </div> </div> </a> </div></article></body>

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

Photo by Caspar Camille Rubin on Unsplash

UPDATE 08/24/21 : Fixed Error related to consuming endpoint (This error was bought up by quite a few people) If you encounter any issues, clone my repo with the link provided below.

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

pip install azureml.core

If you are using Python3.9+ use ` — ignore-requires-python` when installing the packages.

Create Workspace

We can either create the workspace manually on Azure or use Python to create it. If you create it manually, access the workspace and download the config.json file. Below is how you can create it in Python

ws = Workspace.create(
               name='myworkspace',            
               subscription_id='<azure-subscription-id>',           
               resource_group='myresourcegroup',                 
               create_resource_group=True,                 
               location='eastus2'                
               )
  • name: The name of your workspace
  • subscription_id: Your Azure Subscription ID
  • resource_group: The name of your resource Group
  • Set the create_resource_group to True if you want to create a new resource group. If you want to use an existing resource group, set it to False
  • Set the location

Once it is created, save the details of the workspace in a config.json file to use the workspace later.

ws.write_config()

Create a folder named ‘.azureml’ and save the json file inside it. Make sure you name the folder and json file correctly. Python will look for the config.json file inside this folder while loading up your workspace

You won’t need to create a workspace every time, in the future, you can use the following command to load the workspace

ws = Workspace.from_config()

Register the Model

We will use the pickle file we created earlier to register our model.

model = Model.register(workspace = ws,
              model_path ="model/knn.pkl",
              model_name = "knn",
              tags = {"version": "1"},
              description = "Iris classification",
              )
  • workspace: The workspace object we created
  • model_path: the path to the pickle file
  • model_name: Name of the model on Azure MLS
  • tags: Although not necessary, you can add tags to your model
  • description: A description of your model

Create an Environment

We will need to create an environment similar to our local virtual environment

# to install required packages
env = Environment('env')
cd = CondaDependencies.create(pip_packages=['pandas==1.1.5', 'azureml-defaults','joblib==0.17.0'], conda_packages = ['scikit-learn==0.23.2'])
env.python.conda_dependencies = cd
# Register environment to re-use later
env.register(workspace = ws)
print("Registered Environment")
  • You can pass the name as a parameter to the Environment Instance
  • Add dependencies as needed
  • Register the environment so it can be used later on
myenv = Environment.get(workspace=ws, name="env")

This get’s the environment you just created.

Confirm that all the required libraries have been installed by creating a .yml based on the created environment. Ensure all the libraries are mentioned inside the .yml file

myenv.save_to_directory('./environ', overwrite=True)

This will create a new folder called environ with a .yml and a .json file inside it

Config Objects

aciconfig = AciWebservice.deploy_configuration(
            cpu_cores=1,
            memory_gb=1,
            tags={"data":"iris classifier"},
            description='iRIS cLASSIFICATION knn MODEL',
            )

Create a container instance and set the number of cpu_cores and memory_gb based on your requirements.

inference_config = InferenceConfig(entry_script="score.py", environment=myenv)

Create and InferenceConfig instance to link your environment and entry script. We will be creating an entry script below. The entry_script parameter should be set to the path to entry scrip.

Entry Script

The entry script will two functions, an init function and a run function.

def init():
    global model
    model_path = Model.get_model_path("knn")
    print("Model Path is  ", model_path)
    model = joblib.load(model_path)
  • Create a global variable called model
  • Load the model using the name of the model you registered earlier
  • Load the model from the path
def run(data):
   try:
     data = json.loads(data)
     result = model.predict(data['data'])
     return {'data' : result.tolist() , 'message' : "Successfully 
            classified Iris"}
   except Exception as e:
      error = str(e)
      return {'data' : error , 'message' : 'Failed to classify 
             iris'}

Save this file.

Deploy the Model

service = Model.deploy(workspace=ws,
                name='iris-model',
                models=[model],
                inference_config=inference_config,
                deployment_config=aciconfig, 
                overwrite = True)
service.wait_for_deployment(show_output=True)
url = service.scoring_uri
print(url)

Finally, we will deploy the model by combining our config objects, workspace and model together. Deployment usually takes a few minutes.

If your deployment is successful, the rest endpoint will be printed out. If it is unsuccessful, you can access the deployment logs

from azureml.core import Webservice
service = Webservice(ws,'iris-model-deployment')
print(service.get_logs())

Check the conclusion section for some things to keep in mind while deploying.

Consume the Endpoint

import requests
data = {
    'data': scaler.fit_transform([[0.9,0.1,0.1,0.9]]).tolist()
    }
headers = {'Content-Type':'application/json'}
r = requests.post(url, str.encode(json.dumps(data)), headers = headers)
print(r.status_code)
print(r.json())

We will use the requests library to make a request to the endpoint.

Conclusion

  • If you get an error related to Buffer Mismatch while deploying, ensure that your pkl file is created using a 64-bit Python
  • If you get any error related to ‘Module not found’, check the .yml file generated for your environment and ensure that all required libraries are present
  • If needed, specify the version of the library to be installed
  • Try running your score.py file locally to ensure there are no syntax errors. You can also invoke the init and the run function to make a prediction. To run it locally, create a workspace object from your config.json file and while accessing the model pass the workspace as a parameter
model_path = Model.get_model_path("knn" , workspace = ws)

Let me know if you face any errors and I will try to help you out based on my experience :)

I recently created a blog using WordPress, I would love it if you could check it out 😃

Connect with me on LinkedIn

Azure Services
Deployment
Azure Machine Learning
Azure
Ml Model Deployment
Recommended from ReadMedium