|
| 1 | +--- |
| 2 | +title: Deploy a PyTorch model as an Azure Functions application |
| 3 | +description: Use Python, PyTorch, and Azure Functions to classify an image |
| 4 | +author: gvashishtha |
| 5 | + |
| 6 | +ms.topic: tutorial |
| 7 | +ms.date: 02/28/2020 |
| 8 | +ms.author: gopalv |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +# Tutorial: Deploy a pre-trained image classification model to Azure Functions with PyTorch |
| 13 | + |
| 14 | +In this article, you learn how to use Python, PyTorch, and Azure Functions to load a pre-trained model for classifying an image based on its contents. Because you do all work locally and create no Azure resources in the cloud, there is no cost to complete this tutorial. |
| 15 | + |
| 16 | +> [!div class="checklist"] |
| 17 | +> * Initialize a local environment for developing Azure Functions in Python. |
| 18 | +> * Import a pre-trained PyTorch machine learning model into a function app. |
| 19 | +> * Build a serverless HTTP API for classifying an image as one of 1000 ImageNet [classes](https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a). |
| 20 | +> * Consume the API from a web app. |
| 21 | +
|
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio). |
| 25 | +- [Python 3.7.4](https://www.python.org/downloads/release/python-374/). (Python 3.7.4 and Python 3.6.x are verified with Azure Functions; Python 3.8 and later versions are not yet supported.) |
| 26 | +- The [Azure Functions Core Tools](functions-run-local.md#install-the-azure-functions-core-tools) |
| 27 | +- A code editor such as [Visual Studio Code](https://code.visualstudio.com/) |
| 28 | + |
| 29 | +### Prerequisite check |
| 30 | + |
| 31 | +1. In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version 2.7.1846 or later. |
| 32 | +1. Run `python --version` (Linux/MacOS) or `py --version` (Windows) to check your Python version reports 3.7.x. |
| 33 | + |
| 34 | +## Clone the tutorial repository |
| 35 | + |
| 36 | +1. In a terminal or command window, clone the following repository using Git: |
| 37 | + |
| 38 | + ``` |
| 39 | + git clone https://github.com/Azure-Samples/functions-python-pytorch-tutorial.git |
| 40 | + ``` |
| 41 | +
|
| 42 | +1. Navigate into the folder and examine its contents. |
| 43 | +
|
| 44 | + ``` |
| 45 | + cd functions-pytorch |
| 46 | + ``` |
| 47 | +
|
| 48 | + - *start* is your working folder for the tutorial. |
| 49 | + - *end* is the final result and full implementation for your reference. |
| 50 | + - *resources* contains the machine learning model and helper libraries. |
| 51 | + - *frontend* is a website that calls the function app. |
| 52 | + |
| 53 | +## Create and activate a Python virtual environment |
| 54 | +
|
| 55 | +Navigate to the *start* folder and run the following commands to create and activate a virtual environment named `.venv`. Be sure to use Python 3.7, which is supported by Azure Functions. |
| 56 | +
|
| 57 | +
|
| 58 | +# [bash](#tab/bash) |
| 59 | +
|
| 60 | +```bash |
| 61 | +cd start |
| 62 | +``` |
| 63 | + |
| 64 | +```bash |
| 65 | +python -m venv .venv |
| 66 | +``` |
| 67 | + |
| 68 | +```bash |
| 69 | +source .venv/bin/activate |
| 70 | +``` |
| 71 | + |
| 72 | +If Python didn't install the venv package on your Linux distribution, run the following command: |
| 73 | + |
| 74 | +```bash |
| 75 | +sudo apt-get install python3-venv |
| 76 | +``` |
| 77 | + |
| 78 | +# [PowerShell](#tab/powershell) |
| 79 | + |
| 80 | +```powershell |
| 81 | +cd start |
| 82 | +``` |
| 83 | + |
| 84 | +```powershell |
| 85 | +py -m venv .venv |
| 86 | +``` |
| 87 | + |
| 88 | +```powershell |
| 89 | +.venv\scripts\activate |
| 90 | +``` |
| 91 | + |
| 92 | +# [Cmd](#tab/cmd) |
| 93 | + |
| 94 | +```cmd |
| 95 | +cd start |
| 96 | +``` |
| 97 | + |
| 98 | +```cmd |
| 99 | +py -m venv .venv |
| 100 | +``` |
| 101 | + |
| 102 | +```cmd |
| 103 | +.venv\scripts\activate |
| 104 | +``` |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +You run all subsequent commands in this activated virtual environment. (To exit the virtual environment, run `deactivate`.) |
| 109 | + |
| 110 | + |
| 111 | +## Create a local functions project |
| 112 | + |
| 113 | +In Azure Functions, a function project is a container for one or more individual functions that each responds to a specific trigger. All functions in a project share the same local and hosting configurations. In this section, you create a function project that contains a single boilerplate function named `classify` that provides an HTTP endpoint. You add more specific code in a later section. |
| 114 | + |
| 115 | +1. In the *start* folder, use the Azure Functions Core Tools to initialize a Python function app: |
| 116 | + |
| 117 | + ``` |
| 118 | + func init --worker-runtime python |
| 119 | + ``` |
| 120 | +
|
| 121 | + After initialization, the *start* folder contains various files for the project, including configurations files named [local.settings.json](functions-run-local.md#local-settings-file) and [host.json](functions-host-json.md). Because *local.settings.json* can contain secrets downloaded from Azure, the file is excluded from source control by default in the *.gitignore* file. |
| 122 | +
|
| 123 | + > [!TIP] |
| 124 | + > Because a function project is tied to a specific runtime, all the functions in the project must be written with the same language. |
| 125 | +
|
| 126 | +1. Add a function to your project by using the following command, where the `--name` argument is the unique name of your function and the `--template` argument specifies the function's trigger. `func new` create 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*. |
| 127 | +
|
| 128 | + ``` |
| 129 | + func new --name classify --template "HTTP trigger" |
| 130 | + ``` |
| 131 | +
|
| 132 | + This command creates a folder matching the name of the function, *classify*. In that folder are two files: *\_\_init\_\_.py*, which contains the function code, and *function.json*, which describes the function's trigger and its input and output bindings. For details on the contents of these files, see [Examine the file contents](/azure/azure-functions/functions-create-first-azure-function-azure-cli?pivots=programming-language-python#optional-examine-the-file-contents) in the Python quickstart. |
| 133 | +
|
| 134 | +
|
| 135 | +## Run the function locally |
| 136 | +
|
| 137 | +1. Start the function by starting the local Azure Functions runtime host in the *start* folder: |
| 138 | +
|
| 139 | + ``` |
| 140 | + func start |
| 141 | + ``` |
| 142 | + |
| 143 | +1. Once you see the `classify` endpoint appear in the output, navigate to the URL, ```http://localhost:7071/api/classify?name=Azure```. The message "Hello Azure!" should appear in the output. |
| 144 | +
|
| 145 | +1. Use **Ctrl**-**C** to stop the host. |
| 146 | +
|
| 147 | +
|
| 148 | +## Import the PyTorch model and add helper code |
| 149 | +
|
| 150 | +To modify the `classify` function to classify an image based on its contents, you use a pre-trained [ResNet](https://arxiv.org/abs/1512.03385) model. The pre-trained model, which comes from [PyTorch](https://pytorch.org/hub/pytorch_vision_resnet/), classifies an image into 1 of 1000 [ImageNet classes](https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a). You then add some helper code and dependencies to your project. |
| 151 | +
|
| 152 | +1. In the *start* folder, run following command to copy the prediction code and labels into the *classify* folder. |
| 153 | +
|
| 154 | + # [bash](#tab/bash) |
| 155 | + |
| 156 | + ```bash |
| 157 | + cp ../resources/predict.py classify |
| 158 | + cp ../resources/labels.txt classify |
| 159 | + ``` |
| 160 | + |
| 161 | + # [PowerShell](#tab/powershell) |
| 162 | + |
| 163 | + ```powershell |
| 164 | + copy ..\resources\predict.py classify |
| 165 | + copy ..\resources\labels.txt classify |
| 166 | + ``` |
| 167 | + |
| 168 | + # [Cmd](#tab/cmd) |
| 169 | + |
| 170 | + ```cmd |
| 171 | + copy ..\resources\predict.py classify |
| 172 | + copy ..\resources\labels.txt classify |
| 173 | + ``` |
| 174 | + |
| 175 | + --- |
| 176 | + |
| 177 | +1. Verify that the *classify* folder contains files named *predict.py* and *labels.txt*. If not, check that you ran the command in the *start* folder. |
| 178 | +
|
| 179 | +1. Open *start/requirements.txt* in a text editor and add the following dependencies required by the helper code: |
| 180 | +
|
| 181 | + ```txt |
| 182 | + azure-functions |
| 183 | + requests |
| 184 | + numpy==1.15.4 |
| 185 | + https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp36-cp36m-win_amd64.whl; sys_platform == 'win32' and python_version == '3.6' |
| 186 | + 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' |
| 187 | + https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp37-cp37m-win_amd64.whl; sys_platform == 'win32' and python_version == '3.7' |
| 188 | + 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' |
| 189 | + https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp38-cp38-win_amd64.whl; sys_platform == 'win32' and python_version == '3.8' |
| 190 | + 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' |
| 191 | + torchvision==0.5.0 |
| 192 | + ``` |
| 193 | + |
| 194 | +1. Save *requirements.txt*. |
| 195 | +
|
| 196 | +1. Install the dependencies by running the following command in the *start* folder. Installation may take a few minutes, during which time you can proceed with modifying the function in the next section. |
| 197 | +
|
| 198 | + ``` |
| 199 | + pip install --no-cache-dir -r requirements.txt |
| 200 | + ``` |
| 201 | + |
| 202 | + On Windows, you may encounter the error, "Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory:" followed by a long pathname to a file like *sharded_mutable_dense_hashtable.cpython-37.pyc*. Typically, this error happens because the depth of the folder path becomes too long. In this case, set the registry key `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabled` to `1` to enable long paths. Alternately, check where your Python interpreter is installed. If that location has a long path, try reinstalling in a folder with a shorter path. |
| 203 | +
|
| 204 | +## Update the function to run predictions |
| 205 | +
|
| 206 | +1. Open *classify/\_\_init\_\_.py* in a text editor and add the following lines after the existing `import` statements to import the standard JSON library and the *predict* helpers: |
| 207 | +
|
| 208 | + :::code language="python" source="~/functions-pytorch/end/classify/__init__.py" range="1-6" highlight="5-6"::: |
| 209 | +
|
| 210 | +1. Replace the entire contents of the `main` function with the following code: |
| 211 | +
|
| 212 | + :::code language="python" source="~/functions-pytorch/end/classify/__init__.py" range="8-19"::: |
| 213 | +
|
| 214 | + This function receives an image URL in a query string parameter named `img`. It then calls `predict_image_from_url` from the helper library to download and classify the image using the PyTorch model. The function then returns an HTTP response with the results. |
| 215 | +
|
| 216 | + > [!IMPORTANT] |
| 217 | + > Because this HTTP endpoint is called by a web page hosted on another domain, the response includes an `Access-Control-Allow-Origin` header to satisfy the browser's Cross-Origin Resource Sharing (CORS) requirements. |
| 218 | + > |
| 219 | + > In a production application, change `*` to the web page's specific origin for added security. |
| 220 | +
|
| 221 | +1. Save your changes, then assuming that dependencies have finished installing, start the local function host again with `func start`. Be sure to run the host in the *start* folder with the virtual environment activated. Otherwise the host will start, but you will see errors when invoking the function. |
| 222 | +
|
| 223 | + ``` |
| 224 | + func start |
| 225 | + ``` |
| 226 | +
|
| 227 | +1. In a browser, open the following URL to invoke the function with the URL of a cat image and confirm that the returned JSON classifies the image as a cat. |
| 228 | +
|
| 229 | + ``` |
| 230 | + http://localhost:7071/api/classify?img=http://localhost:7071/api/classify?img=https://raw.githubusercontent.com/Azure-Samples/functions-python-pytorch-tutorial/master/resources/assets/Bernese-Mountain-Dog-Temperament-long.jpg |
| 231 | + ``` |
| 232 | + |
| 233 | +1. Keep the host running because you use it in the next step. |
| 234 | +
|
| 235 | +### Run the local web app front end to test the function |
| 236 | +
|
| 237 | +To test invoking the function endpoint from another web app, there's a simple app in the repository's *frontend* folder. |
| 238 | +
|
| 239 | +1. Open a new terminal or command prompt and activate the virtual environment (as described earlier under [Create and activate a Python virtual environment](#create-and-activate-a-python-virtual-environment)). |
| 240 | +
|
| 241 | +1. Navigate to the repository's *frontend* folder. |
| 242 | +
|
| 243 | +1. Start an HTTP server with Python: |
| 244 | +
|
| 245 | + # [bash](#tab/bash) |
| 246 | +
|
| 247 | + ```bash |
| 248 | + python -m http.server |
| 249 | + ``` |
| 250 | + |
| 251 | + # [PowerShell](#tab/powershell) |
| 252 | +
|
| 253 | + ```powershell |
| 254 | + py -m http.server |
| 255 | + ``` |
| 256 | +
|
| 257 | + # [Cmd](#tab/cmd) |
| 258 | +
|
| 259 | + ```cmd |
| 260 | + py -m http.server |
| 261 | + ``` |
| 262 | +
|
| 263 | +1. In a browser, navigate to `localhost:8000`, then enter one of the following photo URLs into the textbox, or use the URL of any publicly accessible image. |
| 264 | +
|
| 265 | + - `https://raw.githubusercontent.com/Azure-Samples/functions-python-pytorch-tutorial/master/resources/assets/Bernese-Mountain-Dog-Temperament-long.jpg` |
| 266 | + - `https://github.com/Azure-Samples/functions-python-pytorch-tutorial/blob/master/resources/assets/bald-eagle.jpg?raw=true` |
| 267 | + - `https://raw.githubusercontent.com/Azure-Samples/functions-python-pytorch-tutorial/master/resources/assets/penguin.jpg` |
| 268 | + |
| 269 | +1. Select **Submit** to invoke the function endpoint to classify the image. |
| 270 | +
|
| 271 | +  |
| 272 | +
|
| 273 | + If the browser reports an error when you submit the image URL, check the terminal in which you're running the function app. If you see an error like "No module found 'PIL'", you may have started the function app in the *start* folder without first activating the virtual environment you created earlier. If you still see errors, run `pip install -r requirements.txt` again with the virtual environment activated and look for errors. |
| 274 | +
|
| 275 | +## Clean up resources |
| 276 | +
|
| 277 | +Because the entirety of this tutorial runs locally on your machine, there are no Azure resources or services to clean up. |
| 278 | +
|
| 279 | +## Next steps |
| 280 | +
|
| 281 | +In this tutorial, you learned how to build and customize an HTTP API endpoint with Azure Functions to classify images using a PyTorch model. You also learned how to call the API from a web app. You can use the techniques in this tutorial to build out APIs of any complexity, all while running on the serverless compute model provided by Azure Functions. |
| 282 | +
|
| 283 | +> [!div class="nextstepaction"] |
| 284 | +> [Deploy the function to Azure Functions using the Azure CLI Guide](./functions-run-local.md#publish) |
| 285 | +
|
| 286 | +See also: |
| 287 | +
|
| 288 | +- [Deploy the function to Azure using Visual Studio Code](https://code.visualstudio.com/docs/python/tutorial-azure-functions). |
| 289 | +- [Azure Functions Python Developer Guide](./functions-reference-python.md) |
0 commit comments