| page_type | languages | products | description | urlFragment | |||
|---|---|---|---|---|---|---|---|
sample |
|
|
Predict ImageNet Classes with PyTorch and Azure Functions |
functions-python-pytorch-tutorial |
Note, the instructions below assume you are using a Linux environment.
mkdir startcd startpython -m venv .venvsource .venv/bin/activate
func init --worker-runtime pythonfunc new --name classify --template "HTTP trigger"
cp ../resources/predict.py classifycp ../resources/labels.txt classify- Add the following dependencies to start/requirements.txt, installing some numerical libraries and PyTorch itself:
azure-functions
requests
numpy==1.15.4
https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp36-cp36m-win_amd64.whl; sys_platform == 'win32' and python_version == '3.6'
https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp36-cp36m-linux_x86_64.whl; sys_platform == 'linux' and python_version == '3.6'
https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp37-cp37m-win_amd64.whl; sys_platform == 'win32' and python_version == '3.7'
https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp37-cp37m-linux_x86_64.whl; sys_platform == 'linux' and python_version == '3.7'
https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp38-cp38-win_amd64.whl; sys_platform == 'win32' and python_version == '3.8'
https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp38-cp38-linux_x86_64.whl; sys_platform == 'linux' and python_version == '3.8'
torchvision==0.5.0- Install dependencies with
pip install --no-cache-dir -r requirements.txt
- Add an
importstatement toclassify/__init__.py
import logging
import json
import azure.functions as func
from .predict import predict_image_from_url
- Replace the entire contents of the
mainfunction with the following code:
def main(req: func.HttpRequest) -> func.HttpResponse:
image_url = req.params.get('img')
logging.info('Image URL received: ' + image_url)
results = predict_image_from_url(image_url)
headers = {
"Content-type": "application/json",
"Access-Control-Allow-Origin": "*"
}
return func.HttpResponse(json.dumps(results), headers = headers)
- Run
func startfrom within the start folder with the virtual environment activated. - Run
http://localhost:7071/api/classify?img=https://raw.githubusercontent.com/gvashishtha/functions-pytorch/master/resources/assets/Bernese-Mountain-Dog-Temperament-long.jpg
Run the following in the Azure Cloud Shell to create a sample function app with a Python runtime:
#!/bin/bash
# Function app and storage account names must be unique.
storageName=mystorageaccount$RANDOM
functionAppName=myserverlessfunc$RANDOM
region=westeurope
pythonVersion=3.7
# Create a resource group.
az group create --name myResourceGroup --location $region
# Create an Azure storage account in the resource group.
az storage account create \
--name $storageName \
--location $region \
--resource-group myResourceGroup \
--sku Standard_LRS
# Create a serverless function app in the resource group.
az functionapp create \
--name $functionAppName \
--storage-account $storageName \
--consumption-plan-location $region \
--resource-group myResourceGroup \
--os-type Linux \
--runtime python \
--runtime-version $pythonVersion \
--functions-version 2
func azure functionapp publish <appname> --build local- Test by using the suggested URL and appending
&img=https://raw.githubusercontent.com/gvashishtha/functions-pytorch/master/resources/assets/Bernese-Mountain-Dog-Temperament-long.jpgat the end of the query string.
See LICENSE.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.