{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "# Exercise 8. Inferencing" }, { "metadata": {}, "cell_type": "markdown", "source": "## Inferencing\n### Create scoring script\n\nFirst, we will create a scoring script that will be invoked by the web service call. Note that the scoring script must have two required functions:\n* `init()`: In this function, you typically load the model into a `global` object. This function is executed only once when the Docker container is started. \n* `run(input_data)`: In this function, the model is used to predict a value based on the input data. The input and output typically use JSON as serialization and deserialization format, but you are not limited to that.\n\nRefer to the scoring script `pytorch_score.py` for this tutorial. Our web service will use this file to predict the dog breed based on a 120 class model trained earlier, located in the folder `model`. We will test the scoring file locally first before you go and deploy the web service.\n\n1. Import the scoring script, so that init() and run() are accessible" }, { "metadata": { "trusted": false }, "cell_type": "code", "source": "%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\nimport pytorch_score", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "2. Call the init function -- this will load the model and the class_names from the model directory" }, { "metadata": { "trusted": false }, "cell_type": "code", "source": "pytorch_score.init()", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "3. Add some helper functions:" }, { "metadata": { "trusted": false }, "cell_type": "code", "source": "import os, json, base64\nfrom io import BytesIO\nimport matplotlib.pyplot as plt\nfrom skimage import io\nfrom PIL import Image\nimport urllib.request\nimport io, requests\n\n##Get random dog\ndef get_random_dog():\n r = requests.get(url =\"https://dog.ceo/api/breeds/image/random\")\n URL= r.json()['message']\n return URL\n\ndef imgToBase64(img):\n \"\"\"Convert pillow image to base64-encoded image\"\"\"\n imgio = BytesIO()\n img.save(imgio, 'JPEG')\n img_str = base64.b64encode(imgio.getvalue())\n return img_str.decode('utf-8')", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "4. Find an image of a dog and call the run() function" }, { "metadata": { "trusted": false }, "cell_type": "code", "source": "##Get Random Dog Image\nURL = get_random_dog()\n\nwith urllib.request.urlopen(URL) as url:\n test_img = io.BytesIO(url.read())\n\n\nplt.imshow(Image.open(test_img))\n\nbase64Img = imgToBase64(Image.open(test_img))\n\nresult = pytorch_score.run(input_data=json.dumps({'data': base64Img}))\nprint(URL)\nprint(json.loads(result))", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "### Register the model\nOnce the run completes, we can register the model that was created.\n\n**Please use a unique name for the model**" }, { "metadata": { "scrolled": true, "trusted": false }, "cell_type": "code", "source": "from azureml.core.model import Model\nmodel = Model.register(ws, model_name='model', model_path = 'model', description='120 Dogbreeds')\nprint(model.name, model.id, model.version, sep = '\\t')", "execution_count": null, "outputs": [] } ], "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "3.5.4", "pygments_lexer": "ipython3", "codemirror_mode": { "version": 3, "name": "ipython" } } }, "nbformat": 4, "nbformat_minor": 2 }