{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright (c) Microsoft Corporation. All rights reserved.\n", "\n", "Licensed under the MIT License." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![Impressions](https://PixelServer20190423114238.azurewebsites.net/api/impressions/MachineLearningNotebooks/how-to-use-azureml/deployment/tensorflow/tensorflow-model-register-and-deploy.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Register TensorFlow SavedModel and deploy as webservice\n", "\n", "Following this notebook, you will:\n", "\n", " - Learn how to register a TF SavedModel in your Azure Machine Learning Workspace.\n", " - Deploy your model as a web service in an Azure Container Instance." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prerequisites\n", "\n", "If you are using an Azure Machine Learning Notebook VM, you are all set. Otherwise, make sure you go through the [configuration notebook](../../../configuration.ipynb) to install the Azure Machine Learning Python SDK and create a workspace." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import azureml.core\n", "\n", "# Check core SDK version number.\n", "print('SDK version:', azureml.core.VERSION)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialize workspace\n", "\n", "Create a [Workspace](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.workspace%28class%29?view=azure-ml-py) object from your persisted configuration." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "create workspace" ] }, "outputs": [], "source": [ "from azureml.core import Workspace\n", "\n", "ws = Workspace.from_config()\n", "print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep='\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Download the Model\n", "\n", "Download and extract the model from https://amlsamplenotebooksdata.blob.core.windows.net/data/flowers_model.tar.gz to \"models\" directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import tarfile\n", "import urllib.request\n", "\n", "# create directory for model\n", "model_dir = 'models'\n", "if not os.path.isdir(model_dir):\n", " os.mkdir(model_dir)\n", "\n", "url=\"https://amlsamplenotebooksdata.blob.core.windows.net/data/flowers_model.tar.gz\"\n", "response = urllib.request.urlretrieve(url, model_dir + \"/flowers_model.tar.gz\")\n", "tar = tarfile.open(model_dir + \"/flowers_model.tar.gz\", \"r:gz\")\n", "tar.extractall(model_dir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Register model\n", "\n", "Register a file or folder as a model by calling [Model.register()](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.model.model?view=azure-ml-py#register-workspace--model-path--model-name--tags-none--properties-none--description-none--datasets-none--model-framework-none--model-framework-version-none--child-paths-none-). For this example, we have provided a TensorFlow SavedModel (`flowers_model` in the notebook's directory).\n", "\n", "In addition to the content of the model file itself, your registered model will also store model metadata -- model description, tags, and framework information -- that will be useful when managing and deploying models in your workspace. Using tags, for instance, you can categorize your models and apply filters when listing models in your workspace. Also, marking this model with the scikit-learn framework will simplify deploying it as a web service, as we'll see later." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "register model from file" ] }, "outputs": [], "source": [ "from azureml.core import Model\n", "\n", "model = Model.register(workspace=ws,\n", " model_name='flowers', # Name of the registered model in your workspace.\n", " model_path= model_dir + '/flowers_model', # Local Tensorflow SavedModel folder to upload and register as a model.\n", " model_framework=Model.Framework.TENSORFLOW, # Framework used to create the model.\n", " model_framework_version='1.14.0', # Version of Tensorflow used to create the model.\n", " description='Flowers model')\n", "\n", "print('Name:', model.name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Deploy model\n", "\n", "Deploy your model as a web service using [Model.deploy()](https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.model.model?view=azure-ml-py#deploy-workspace--name--models--inference-config--deployment-config-none--deployment-target-none-). Web services take one or more models, load them in an environment, and run them on one of several supported deployment targets.\n", "\n", "For this example, we will deploy your TensorFlow SavedModel to an Azure Container Instance (ACI)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use a default environment (for supported models)\n", "\n", "The Azure Machine Learning service provides a default environment for supported model frameworks, including TensorFlow, based on the metadata you provided when registering your model. This is the easiest way to deploy your model.\n", "\n", "**Note**: This step can take several minutes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from azureml.core import Webservice\n", "from azureml.exceptions import WebserviceException\n", "\n", "service_name = 'tensorflow-flower-service'\n", "\n", "# Remove any existing service under the same name.\n", "try:\n", " Webservice(ws, service_name).delete()\n", "except WebserviceException:\n", " pass\n", "\n", "service = Model.deploy(ws, service_name, [model])\n", "service.wait_for_deployment(show_output=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After your model is deployed, perform a call to the web service." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "\n", "headers = {'Content-Type': 'application/json'}\n", "\n", "if service.auth_enabled:\n", " headers['Authorization'] = 'Bearer '+ service.get_keys()[0]\n", "elif service.token_auth_enabled:\n", " headers['Authorization'] = 'Bearer '+ service.get_token()[0]\n", "\n", "scoring_uri = service.scoring_uri # If you have a SavedModel with classify and regress, \n", " # you can change the scoring_uri from 'uri:predict' to 'uri:classify' or 'uri:regress'.\n", "print(scoring_uri)\n", "\n", "with open('tensorflow-flower-predict-input.json', 'rb') as data_file:\n", " response = requests.post(\n", " scoring_uri, data=data_file, headers=headers)\n", "print(response.status_code)\n", "print(response.elapsed)\n", "print(response.json())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you are finished testing your service, clean up the deployment." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "service.delete()" ] } ], "metadata": { "authors": [ { "name": "vaidyas" } ], "kernelspec": { "display_name": "Python 3.6", "language": "python", "name": "python36" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }