{ "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/deploy-to-local/register-model-deploy-local.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Register model and deploy locally\n", "\n", "This example shows how to deploy a web service in step-by-step fashion:\n", "\n", " 1. Register model\n", " 2. Deploy the image as a web service in a local Docker container.\n", " 3. Quickly test changes to your entry script by reloading the local service.\n", " 4. Optionally, you can also make changes to model, conda or extra_docker_file_steps and update local service" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prerequisites\n", "If you are using an Azure Machine Learning Notebook VM, you are all set. Otherwise, make sure you go through the [configuration](../../../configuration.ipynb) Notebook first if you haven't." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check core SDK version number\n", "import azureml.core\n", "\n", "print(\"SDK version:\", azureml.core.VERSION)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialize Workspace\n", "\n", "Initialize a workspace object from persisted configuration." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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": [ "## Register Model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can add tags and descriptions to your models. we are using `sklearn_regression_model.pkl` file in the current directory as a model with the name `sklearn_regression_model` in the workspace.\n", "\n", "Using tags, you can track useful information such as the name and version of the machine learning library used to train the model, framework, category, target customer etc. Note that tags must be alphanumeric." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "register model from file" ] }, "outputs": [], "source": [ "from azureml.core.model import Model\n", "\n", "model = Model.register(model_path=\"sklearn_regression_model.pkl\",\n", " model_name=\"sklearn_regression_model\",\n", " tags={'area': \"diabetes\", 'type': \"regression\"},\n", " description=\"Ridge regression model to predict diabetes\",\n", " workspace=ws)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Environment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from azureml.core.conda_dependencies import CondaDependencies\n", "from azureml.core.environment import Environment\n", "\n", "environment = Environment(\"LocalDeploy\")\n", "environment.python.conda_dependencies = CondaDependencies(\"myenv.yml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Inference Configuration" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from azureml.core.model import InferenceConfig\n", "\n", "inference_config = InferenceConfig(entry_script=\"score.py\",\n", " environment=environment)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Model Profiling\n", "\n", "Profile your model to understand how much CPU and memory the service, created as a result of its deployment, will need. Profiling returns information such as CPU usage, memory usage, and response latency. It also provides a CPU and memory recommendation based on the resource usage. You can profile your model (or more precisely the service built based on your model) on any CPU and/or memory combination where 0.1 <= CPU <= 3.5 and 0.1GB <= memory <= 15GB. If you do not provide a CPU and/or memory requirement, we will test it on the default configuration of 3.5 CPU and 15GB memory.\n", "\n", "In order to profile your model you will need:\n", "- a registered model\n", "- an entry script\n", "- an inference configuration\n", "- a single column tabular dataset, where each row contains a string representing sample request data sent to the service.\n", "\n", "Please, note that profiling is a long running operation and can take up to 25 minutes depending on the size of the dataset.\n", "\n", "At this point we only support profiling of services that expect their request data to be a string, for example: string serialized json, text, string serialized image, etc. The content of each row of the dataset (string) will be put into the body of the HTTP request and sent to the service encapsulating the model for scoring.\n", "\n", "Below is an example of how you can construct an input dataset to profile a service which expects its incoming requests to contain serialized json. In this case we created a dataset based one hundred instances of the same request data. In real world scenarios however, we suggest that you use larger datasets with various inputs, especially if your model resource usage/behavior is input dependent." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "from azureml.core import Datastore\n", "from azureml.core.dataset import Dataset\n", "from azureml.data import dataset_type_definitions\n", "\n", "\n", "# create a string that can be put in the body of the request\n", "serialized_input_json = json.dumps({\n", " 'data': [\n", " [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n", " [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n", " ]\n", "})\n", "dataset_content = []\n", "for i in range(100):\n", " dataset_content.append(serialized_input_json)\n", "dataset_content = '\\n'.join(dataset_content)\n", "file_name = 'sample_request_data_diabetes.txt'\n", "f = open(file_name, 'w')\n", "f.write(dataset_content)\n", "f.close()\n", "\n", "# upload the txt file created above to the Datastore and create a dataset from it\n", "data_store = Datastore.get_default(ws)\n", "data_store.upload_files(['./' + file_name], target_path='sample_request_data_diabetes')\n", "datastore_path = [(data_store, 'sample_request_data_diabetes' +'/' + file_name)]\n", "sample_request_data_diabetes = Dataset.Tabular.from_delimited_files(\n", " datastore_path,\n", " separator='\\n',\n", " infer_column_types=True,\n", " header=dataset_type_definitions.PromoteHeadersBehavior.NO_HEADERS)\n", "sample_request_data_diabetes = sample_request_data_diabetes.register(workspace=ws,\n", " name='sample_request_data_diabetes',\n", " create_new_version=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have an input dataset we are ready to go ahead with profiling. In this case we are testing the previously introduced sklearn regression model on 1 CPU and 0.5 GB memory. The memory usage and recommendation presented in the result is measured in Gigabytes. The CPU usage and recommendation is measured in CPU cores." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "from azureml.core import Environment\n", "from azureml.core.conda_dependencies import CondaDependencies\n", "from azureml.core.model import Model, InferenceConfig\n", "\n", "\n", "environment = Environment('my-sklearn-environment')\n", "environment.python.conda_dependencies = CondaDependencies.create(pip_packages=[\n", " 'azureml-defaults',\n", " 'inference-schema[numpy-support]',\n", " 'joblib',\n", " 'numpy',\n", " 'scikit-learn'\n", "])\n", "inference_config = InferenceConfig(entry_script='score.py', environment=environment)\n", "# if cpu and memory_in_gb parameters are not provided\n", "# the model will be profiled on default configuration of\n", "# 3.5CPU and 15GB memory\n", "profile = Model.profile(ws,\n", " 'profile-%s' % datetime.now().strftime('%m%d%Y-%H%M%S'),\n", " [model],\n", " inference_config,\n", " input_dataset=sample_request_data_diabetes,\n", " cpu=1.0,\n", " memory_in_gb=0.5)\n", "\n", "# profiling is a long running operation and may take up to 25 min\n", "profile.wait_for_completion(True)\n", "details = profile.get_details()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Deploy Model as a Local Docker Web Service\n", "\n", "*Make sure you have Docker installed and running.*\n", "\n", "Note that the service creation can take few minutes.\n", "\n", "NOTE:\n", "\n", "The Docker image runs as a Linux container. If you are running Docker for Windows, you need to ensure the Linux Engine is running:\n", "\n", " # PowerShell command to switch to Linux engine\n", " & 'C:\\Program Files\\Docker\\Docker\\DockerCli.exe' -SwitchLinuxEngine" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "sample-localwebservice-deploy" ] }, "outputs": [], "source": [ "from azureml.core.webservice import LocalWebservice\n", "\n", "# This is optional, if not provided Docker will choose a random unused port.\n", "deployment_config = LocalWebservice.deploy_configuration(port=6789)\n", "\n", "local_service = Model.deploy(ws, \"test\", [model], inference_config, deployment_config)\n", "\n", "local_service.wait_for_deployment()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Local service port: {}'.format(local_service.port))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Check Status and Get Container Logs\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(local_service.get_logs())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Web Service" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Call the web service with some input data to get a prediction." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "sample_input = json.dumps({\n", " 'data': [\n", " [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n", " [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n", " ]\n", "})\n", "\n", "sample_input = bytes(sample_input, encoding='utf-8')\n", "\n", "local_service.run(input_data=sample_input)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reload Service\n", "\n", "You can update your score.py file and then call `reload()` to quickly restart the service. This will only reload your execution script and dependency files, it will not rebuild the underlying Docker image. As a result, `reload()` is fast, but if you do need to rebuild the image -- to add a new Conda or pip package, for instance -- you will have to call `update()`, instead (see below)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%writefile score.py\n", "import os\n", "import pickle\n", "import json\n", "import numpy as np\n", "from sklearn.externals import joblib\n", "from sklearn.linear_model import Ridge\n", "\n", "from inference_schema.schema_decorators import input_schema, output_schema\n", "from inference_schema.parameter_types.numpy_parameter_type import NumpyParameterType\n", "\n", "def init():\n", " global model\n", " # AZUREML_MODEL_DIR is an environment variable created during deployment.\n", " # It is the path to the model folder (./azureml-models/$MODEL_NAME/$VERSION)\n", " # For multiple models, it points to the folder containing all deployed models (./azureml-models)\n", " model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'sklearn_regression_model.pkl')\n", " # deserialize the model file back into a sklearn model\n", " model = joblib.load(model_path)\n", "\n", "input_sample = np.array([[10,9,8,7,6,5,4,3,2,1]])\n", "output_sample = np.array([3726.995])\n", "\n", "@input_schema('data', NumpyParameterType(input_sample))\n", "@output_schema(NumpyParameterType(output_sample))\n", "def run(data):\n", " try:\n", " result = model.predict(data)\n", " # you can return any datatype as long as it is JSON-serializable\n", " return 'hello from updated score.py'\n", " except Exception as e:\n", " error = str(e)\n", " return error" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "local_service.reload()\n", "print(\"--------------------------------------------------------------\")\n", "\n", "# After calling reload(), run() will return the updated message.\n", "local_service.run(input_data=sample_input)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Update Service\n", "\n", "If you want to change your model(s), Conda dependencies, or deployment configuration, call `update()` to rebuild the Docker image.\n", "\n", "```python\n", "local_service.update(models=[SomeOtherModelObject],\n", " inference_config=inference_config,\n", " deployment_config=local_config)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Delete Service" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "local_service.delete()" ] } ], "metadata": { "authors": [ { "name": "keriehm" } ], "category": "tutorial", "compute": [ "Local" ], "datasets": [ "None" ], "deployment": [ "Local" ], "exclude_from_index": false, "framework": [ "None" ], "friendly_name": "Register a model and deploy locally", "index_order": 1, "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.6.8" }, "star_tag": [], "tags": [ "None" ], "task": "Deployment" }, "nbformat": 4, "nbformat_minor": 2 }