Lab 2 - Create the Function
Deploy your ONNX model in an Azure Function

1. Create the Azure Function

Run the func init command back in the terminal window you have open, as follows, to create a functions project in a folder named SimpsonsFunctions with the Python runtime and navigate into the project folder.
1
func init SimpsonsFunctions --python
2
cd SimpsonsFunctions
Copied!
This folder contains various files for the project, including configurations files named local.settings.json and host.json. Because local.settings.json can contain secrets downloaded from Azure, the file is excluded from source control by default in the .gitignore file.
Add a function to your project by using the following command, where the --name argument is the unique name of your function (Classify) and the --template argument specifies the function's trigger (HTTP).
1
func new --name Classify --template "HTTP trigger" --authlevel "anonymous"
Copied!
func new creates a subfolder matching the function name that contains a code file appropriate to the project's chosen language and a configuration file named function.json.
Open the file that contains the code for your function.
1
code Classify/__init__.py
Copied!
Replace the code in the function (Classify/__init__.py) with:
1
import logging
2
import onnxruntime as nxrun
3
import numpy as np
4
import PIL
5
from PIL import Image
6
import requests
7
from io import BytesIO
8
import azure.functions as func
9
10
def main(req: func.HttpRequest) -> func.HttpResponse:
11
12
## Get the image and resize it to 244x244 pixels
13
url = req.params.get('url')
14
response = requests.get(url)
15
image = PIL.Image.open(BytesIO(response.content)).resize([224,224])
16
17
## Load the model and score the image
18
model_path = "model/model.onnx"
19
sess = nxrun.InferenceSession(model_path)
20
input_array = np.array(image, dtype=np.float32)[np.newaxis, :, :, :]
21
input_array = input_array.transpose((0, 3, 1, 2))[:, (2, 1, 0), :, :]
22
input_name = sess.get_inputs()[0].name
23
outputs = sess.run(None, {input_name: input_array.astype(np.float32)})
24
25
## Find the label with the highest score
26
label = outputs[0][0][0]
27
score = (outputs[1][0][outputs[0][0][0]]*100)
28
29
## Return and log the response
30
response = f"I'm {score:.2f}% sure I see: {label}"
31
logging.info(response)
32
return func.HttpResponse(response)
Copied!
Copy the model to the function.
1
cp ../model/ ./model -r
Copied!
Open the requirements.txt. This files contains a list of packages that need to be installed in the Azure Function.
1
code requirements.txt
Copied!
Add the following packages to the requirements.txt
1
onnxruntime
2
pillow!=8.3.0
3
requests
Copied!

2. Deploy the Azure Functions

Create the Azure Resources

Create a resource group for the Azure Function
1
az group create --name SimpsonsFunc_RG --location WestEurope
Copied!
Create a Storage Account. Replace <UNIQUE_NAME> with a unique name like simpsonfunc112
In the previous example, replace <STORAGE_NAME> with a name that is appropriate to you and unique in Azure Storage. Names must contain three to 24 characters numbers and lowercase letters only. Standard_LRS specifies a general-purpose account, which is supported by Functions.
1
az storage account create --name <UNIQUE_NAME> --resource-group SimpsonsFunc_RG --sku Standard_LRS
Copied!
Create the function app in Azure:
1
az functionapp create --name <APP_NAME> --storage-account <STORAGE_NAME> --resource-group SimpsonsFunc_RG --consumption-plan-location westeurope --runtime python --runtime-version 3.8 --functions-version 3 --os-type linux
Copied!
In the previous example, replace <APP_NAME> with a globally unique name appropriate to you. The <APP_NAME> is also the default DNS domain for the function app.
This command creates a function app running in your specified language runtime under the Azure Functions Consumption Plan, which is free for the amount of usage you incur here. The command also provisions an associated Azure Application Insights instance in the same resource group, with which you can monitor your function app and view logs. For more information, see Monitor Azure Functions. The instance incurs no costs until you activate it.
After you've successfully created your function app in Azure, you're now ready to deploy your local functions project by using the func azure functionapp publish command.

Publish the Azure Function

In the following example, replace <APP_NAME> with the name of your app.
1
func azure functionapp publish <APP_NAME>
Copied!

Test the published function

Copy the Invoke URL, paste it in your browser and add the query string below to the URL.
1
?url=https://github.com/hnky/dataset-lego-figures/raw/master/_test/Bart.jpg
Copied!
As a result, you should see:
Done! Well done, you have now successfully deployed a classification model, that you have created using the Azure Custom Vision Service, running in a Python Azure Function.