mirror of
https://github.com/Azure/MachineLearningNotebooks.git
synced 2025-12-20 01:27:06 -05:00
Compare commits
1 Commits
azureml-sd
...
release_up
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dda35dfcf1 |
@@ -103,7 +103,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import azureml.core\n",
|
"import azureml.core\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ def fetch_openml_with_retries(data_id, max_retries=4, retry_delay=60):
|
|||||||
print("Download attempt {0} of {1}".format(i + 1, max_retries))
|
print("Download attempt {0} of {1}".format(i + 1, max_retries))
|
||||||
data = fetch_openml(data_id=data_id, as_frame=True)
|
data = fetch_openml(data_id=data_id, as_frame=True)
|
||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e: # noqa: B902
|
||||||
print("Download attempt failed with exception:")
|
print("Download attempt failed with exception:")
|
||||||
print(e)
|
print(e)
|
||||||
if i + 1 != max_retries:
|
if i + 1 != max_retries:
|
||||||
@@ -47,7 +47,7 @@ _categorical_columns = [
|
|||||||
|
|
||||||
|
|
||||||
def fetch_census_dataset():
|
def fetch_census_dataset():
|
||||||
"""Fetch the Adult Census Dataset
|
"""Fetch the Adult Census Dataset.
|
||||||
|
|
||||||
This uses a particular URL for the Adult Census dataset. The code
|
This uses a particular URL for the Adult Census dataset. The code
|
||||||
is a simplified version of fetch_openml() in sklearn.
|
is a simplified version of fetch_openml() in sklearn.
|
||||||
@@ -63,17 +63,35 @@ def fetch_census_dataset():
|
|||||||
|
|
||||||
filename = "1595261.gz"
|
filename = "1595261.gz"
|
||||||
data_url = "https://rainotebookscdn.blob.core.windows.net/datasets/"
|
data_url = "https://rainotebookscdn.blob.core.windows.net/datasets/"
|
||||||
urlretrieve(data_url + filename, filename)
|
|
||||||
|
|
||||||
http_stream = gzip.GzipFile(filename=filename, mode='rb')
|
remaining_attempts = 5
|
||||||
|
sleep_duration = 10
|
||||||
|
while remaining_attempts > 0:
|
||||||
|
try:
|
||||||
|
urlretrieve(data_url + filename, filename)
|
||||||
|
|
||||||
with closing(http_stream):
|
http_stream = gzip.GzipFile(filename=filename, mode='rb')
|
||||||
def _stream_generator(response):
|
|
||||||
for line in response:
|
|
||||||
yield line.decode('utf-8')
|
|
||||||
|
|
||||||
stream = _stream_generator(http_stream)
|
with closing(http_stream):
|
||||||
data = arff.load(stream)
|
def _stream_generator(response):
|
||||||
|
for line in response:
|
||||||
|
yield line.decode('utf-8')
|
||||||
|
|
||||||
|
stream = _stream_generator(http_stream)
|
||||||
|
data = arff.load(stream)
|
||||||
|
except Exception as exc: # noqa: B902
|
||||||
|
remaining_attempts -= 1
|
||||||
|
print("Error downloading dataset from {} ({} attempt(s) remaining)"
|
||||||
|
.format(data_url, remaining_attempts))
|
||||||
|
print(exc)
|
||||||
|
time.sleep(sleep_duration)
|
||||||
|
sleep_duration *= 2
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
# dataset successfully downloaded
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise Exception("Could not retrieve dataset from {}.".format(data_url))
|
||||||
|
|
||||||
attributes = OrderedDict(data['attributes'])
|
attributes = OrderedDict(data['attributes'])
|
||||||
arff_columns = list(attributes)
|
arff_columns = list(attributes)
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ dependencies:
|
|||||||
|
|
||||||
- pip:
|
- pip:
|
||||||
# Required packages for AzureML execution, history, and data preparation.
|
# Required packages for AzureML execution, history, and data preparation.
|
||||||
- azureml-widgets~=1.28.0
|
- azureml-widgets~=1.29.0
|
||||||
- pytorch-transformers==1.0.0
|
- pytorch-transformers==1.0.0
|
||||||
- spacy==2.1.8
|
- spacy==2.1.8
|
||||||
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
||||||
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.28.0/validated_win32_requirements.txt [--no-deps]
|
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.29.0/validated_win32_requirements.txt [--no-deps]
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ dependencies:
|
|||||||
|
|
||||||
- pip:
|
- pip:
|
||||||
# Required packages for AzureML execution, history, and data preparation.
|
# Required packages for AzureML execution, history, and data preparation.
|
||||||
- azureml-widgets~=1.28.0
|
- azureml-widgets~=1.29.0
|
||||||
- pytorch-transformers==1.0.0
|
- pytorch-transformers==1.0.0
|
||||||
- spacy==2.1.8
|
- spacy==2.1.8
|
||||||
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
||||||
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.28.0/validated_linux_requirements.txt [--no-deps]
|
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.29.0/validated_linux_requirements.txt [--no-deps]
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ dependencies:
|
|||||||
|
|
||||||
- pip:
|
- pip:
|
||||||
# Required packages for AzureML execution, history, and data preparation.
|
# Required packages for AzureML execution, history, and data preparation.
|
||||||
- azureml-widgets~=1.28.0
|
- azureml-widgets~=1.29.0
|
||||||
- pytorch-transformers==1.0.0
|
- pytorch-transformers==1.0.0
|
||||||
- spacy==2.1.8
|
- spacy==2.1.8
|
||||||
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
||||||
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.28.0/validated_darwin_requirements.txt [--no-deps]
|
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.29.0/validated_darwin_requirements.txt [--no-deps]
|
||||||
|
|||||||
@@ -105,7 +105,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -93,7 +93,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -96,7 +96,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -81,7 +81,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -91,7 +91,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -113,7 +113,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -87,7 +87,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -97,7 +97,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -94,7 +94,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -82,7 +82,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -96,7 +96,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -96,7 +96,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -92,7 +92,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -250,7 +250,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"### Deploy model as web service\n",
|
"### Deploy model as web service\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The ```mlflow.azureml.deploy``` function registers the logged Keras+Tensorflow model and deploys the model in a framework-aware manner. It automatically creates the Tensorflow-specific inferencing wrapper code and specifies package dependencies for you. See [this doc](https://mlflow.org/docs/latest/models.html#id34) for more information on deploying models on Azure ML using MLflow.\n",
|
"The ```client.create_deployment``` function registers the logged Keras+Tensorflow model and deploys the model in a framework-aware manner. It automatically creates the Tensorflow-specific inferencing wrapper code and specifies package dependencies for you. See [this doc](https://mlflow.org/docs/latest/models.html#id34) for more information on deploying models on Azure ML using MLflow.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"In this example, we deploy the Docker image to Azure Container Instance: a serverless compute capable of running a single container. You can tag and add descriptions to help keep track of your web service. \n",
|
"In this example, we deploy the Docker image to Azure Container Instance: a serverless compute capable of running a single container. You can tag and add descriptions to help keep track of your web service. \n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -259,131 +259,63 @@
|
|||||||
"Note that the service deployment can take several minutes."
|
"Note that the service deployment can take several minutes."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"source": [
|
||||||
|
"First define your deployment target and customize parameters in the deployment config. Refer to [this documentation](https://docs.microsoft.com/azure/machine-learning/reference-azure-machine-learning-cli#azure-container-instance-deployment-configuration-schema) for more information. "
|
||||||
|
],
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.webservice import AciWebservice, Webservice\n",
|
"import json\n",
|
||||||
|
" \n",
|
||||||
|
"# Data to be written\n",
|
||||||
|
"deploy_config ={\n",
|
||||||
|
" \"computeType\": \"aci\"\n",
|
||||||
|
"}\n",
|
||||||
|
"# Serializing json \n",
|
||||||
|
"json_object = json.dumps(deploy_config)\n",
|
||||||
|
" \n",
|
||||||
|
"# Writing to sample.json\n",
|
||||||
|
"with open(\"deployment_config.json\", \"w\") as outfile:\n",
|
||||||
|
" outfile.write(json_object)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from mlflow.deployments import get_deploy_client\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"# set the tracking uri as the deployment client\n",
|
||||||
|
"client = get_deploy_client(mlflow.get_tracking_uri())\n",
|
||||||
|
"\n",
|
||||||
|
"# set the model path \n",
|
||||||
"model_path = \"model\"\n",
|
"model_path = \"model\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"aci_config = AciWebservice.deploy_configuration(cpu_cores=2, \n",
|
"# set the deployment config\n",
|
||||||
" memory_gb=5, \n",
|
"deployment_config_path = \"deployment_config.json\"\n",
|
||||||
" tags={\"data\": \"MNIST\", \"method\" : \"keras\"}, \n",
|
"test_config = {'deploy-config-file': deployment_config_path}\n",
|
||||||
" description=\"Predict using webservice\")\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"webservice, azure_model = mlflow.azureml.deploy(model_uri='runs:/{}/{}'.format(run.id, model_path),\n",
|
"# define the model path and the name is the service name\n",
|
||||||
" workspace=ws,\n",
|
"# the model gets registered automatically and a name is autogenerated using the \"name\" parameter below \n",
|
||||||
" deployment_config=aci_config,\n",
|
"client.create_deployment(model_uri='runs:/{}/{}'.format(run.id, model_path),\n",
|
||||||
" service_name=\"keras-mnist-1\",\n",
|
" config=test_config,\n",
|
||||||
" model_name=\"keras_mnist\")"
|
" name=\"keras-aci-deployment\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"Once the deployment has completed you can check the scoring URI of the web service."
|
"Once the deployment has completed you can check the scoring URI of the web service in AzureML studio UI in the endpoints tab. Refer [mlflow predict](https://mlflow.org/docs/latest/python_api/mlflow.deployments.html#mlflow.deployments.BaseDeploymentClient.predict) on how to test your deployment. "
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"print(\"Scoring URI is: {}\".format(webservice.scoring_uri))"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"In case of a service creation issue, you can use ```webservice.get_logs()``` to get logs to debug."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"### Make predictions using a web service\n",
|
|
||||||
"\n",
|
|
||||||
"To make the web service, create a test data set as normalized NumPy array. \n",
|
|
||||||
"\n",
|
|
||||||
"Then, let's define a utility function that takes a random image and converts it into a format and shape suitable for input to the Keras inferencing end-point. The conversion is done by: \n",
|
|
||||||
"\n",
|
|
||||||
" 1. Select a random (image, label) tuple\n",
|
|
||||||
" 2. Take the image and converting to to NumPy array \n",
|
|
||||||
" 3. Reshape array into 1 x 1 x N array\n",
|
|
||||||
" * 1 image in batch, 1 color channel, N = 784 pixels for MNIST images\n",
|
|
||||||
" * Note also ```x = x.view(-1, 1, 28, 28)``` in net definition in ```train.py``` program to shape incoming scoring requests.\n",
|
|
||||||
" 4. Convert the NumPy array to list to make it into a built-in type.\n",
|
|
||||||
" 5. Create a dictionary {\"data\", <list>} that can be converted to JSON string for web service requests."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"import keras\n",
|
|
||||||
"import random\n",
|
|
||||||
"import numpy as np\n",
|
|
||||||
"\n",
|
|
||||||
"# the data, split between train and test sets\n",
|
|
||||||
"(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n",
|
|
||||||
"\n",
|
|
||||||
"# Scale images to the [0, 1] range\n",
|
|
||||||
"x_test = x_test.astype(\"float32\") / 255\n",
|
|
||||||
"x_test = x_test.reshape(len(x_test), -1)\n",
|
|
||||||
"\n",
|
|
||||||
"# convert class vectors to binary class matrices\n",
|
|
||||||
"y_test = keras.utils.to_categorical(y_test, 10)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"%matplotlib inline\n",
|
|
||||||
"\n",
|
|
||||||
"import json\n",
|
|
||||||
"import matplotlib.pyplot as plt\n",
|
|
||||||
"\n",
|
|
||||||
"# send a random row from the test set to score\n",
|
|
||||||
"random_index = np.random.randint(0, len(x_test)-1)\n",
|
|
||||||
"input_data = \"{\\\"data\\\": [\" + str(list(x_test[random_index])) + \"]}\"\n",
|
|
||||||
"\n",
|
|
||||||
"response = webservice.run(input_data)\n",
|
|
||||||
"\n",
|
|
||||||
"response = sorted(response[0].items(), key = lambda x: x[1], reverse = True)\n",
|
|
||||||
"\n",
|
|
||||||
"print(\"Predicted label:\", response[0][0])\n",
|
|
||||||
"plt.imshow(x_test[random_index].reshape(28,28), cmap = \"gray\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"You can also call the web service using a raw POST method against the web service"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"import requests\n",
|
|
||||||
"\n",
|
|
||||||
"response = requests.post(url=webservice.scoring_uri, data=input_data,headers={\"Content-type\": \"application/json\"})\n",
|
|
||||||
"print(response.text)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -400,7 +332,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"webservice.delete()"
|
"client.delete(\"keras-aci-deployment\")"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -249,7 +249,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Deploy model as web service\n",
|
"## Deploy model as web service\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The ```mlflow.azureml.deploy``` function registers the logged PyTorch model and deploys the model in a framework-aware manner. It automatically creates the PyTorch-specific inferencing wrapper code and specifies package dependencies for you. See [this doc](https://mlflow.org/docs/latest/models.html#id34) for more information on deploying models on Azure ML using MLflow.\n",
|
"The ```client.create_deployment``` function registers the logged PyTorch model and deploys the model in a framework-aware manner. It automatically creates the PyTorch-specific inferencing wrapper code and specifies package dependencies for you. See [this doc](https://mlflow.org/docs/latest/models.html#id34) for more information on deploying models on Azure ML using MLflow.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"In this example, we deploy the Docker image to Azure Container Instance: a serverless compute capable of running a single container. You can tag and add descriptions to help keep track of your web service. \n",
|
"In this example, we deploy the Docker image to Azure Container Instance: a serverless compute capable of running a single container. You can tag and add descriptions to help keep track of your web service. \n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -258,33 +258,63 @@
|
|||||||
"Note that the service deployment can take several minutes."
|
"Note that the service deployment can take several minutes."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"source": [
|
||||||
|
"First define your deployment target and customize parameters in the deployment config. Refer to [this documentation](https://docs.microsoft.com/azure/machine-learning/reference-azure-machine-learning-cli#azure-container-instance-deployment-configuration-schema) for more information. "
|
||||||
|
],
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.webservice import AciWebservice, Webservice\n",
|
"import json\n",
|
||||||
|
" \n",
|
||||||
|
"# Data to be written\n",
|
||||||
|
"deploy_config ={\n",
|
||||||
|
" \"computeType\": \"aci\"\n",
|
||||||
|
"}\n",
|
||||||
|
"# Serializing json \n",
|
||||||
|
"json_object = json.dumps(deploy_config)\n",
|
||||||
|
" \n",
|
||||||
|
"# Writing to sample.json\n",
|
||||||
|
"with open(\"deployment_config.json\", \"w\") as outfile:\n",
|
||||||
|
" outfile.write(json_object)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from mlflow.deployments import get_deploy_client\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"# set the tracking uri as the deployment client\n",
|
||||||
|
"client = get_deploy_client(mlflow.get_tracking_uri())\n",
|
||||||
|
"\n",
|
||||||
|
"# set the model path \n",
|
||||||
"model_path = \"model\"\n",
|
"model_path = \"model\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"aci_config = AciWebservice.deploy_configuration(cpu_cores=2, \n",
|
"# set the deployment config\n",
|
||||||
" memory_gb=5, \n",
|
"deployment_config_path = \"deployment_config.json\"\n",
|
||||||
" tags={\"data\": \"MNIST\", \"method\" : \"pytorch\"}, \n",
|
"test_config = {'deploy-config-file': deployment_config_path}\n",
|
||||||
" description=\"Predict using webservice\")\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"webservice, azure_model = mlflow.azureml.deploy(model_uri='runs:/{}/{}'.format(run.id, model_path),\n",
|
"# define the model path and the name is the service name\n",
|
||||||
" workspace=ws,\n",
|
"# the model gets registered automatically and a name is autogenerated using the \"name\" parameter below \n",
|
||||||
" deployment_config=aci_config,\n",
|
"client.create_deployment(model_uri='runs:/{}/{}'.format(run.id, model_path),\n",
|
||||||
" service_name=\"pytorch-mnist-1\",\n",
|
" config=test_config,\n",
|
||||||
" model_name=\"pytorch_mnist\")"
|
" name=\"keras-aci-deployment\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"Once the deployment has completed you can check the scoring URI of the web service."
|
"Once the deployment has completed you can check the scoring URI of the web service in AzureML studio UI in the endpoints tab. Refer [mlflow predict](https://mlflow.org/docs/latest/python_api/mlflow.deployments.html#mlflow.deployments.BaseDeploymentClient.predict) on how to test your deployment. "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -293,133 +323,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"Scoring URI is: {}\".format(webservice.scoring_uri))"
|
"client.delete(\"keras-aci-deployment\")"
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"In case of a service creation issue, you can use ```webservice.get_logs()``` to get logs to debug."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"## Make predictions using a web service\n",
|
|
||||||
"\n",
|
|
||||||
"To make the web service, create a test data set as normalized PyTorch tensors. \n",
|
|
||||||
"\n",
|
|
||||||
"Then, let's define a utility function that takes a random image and converts it into a format and shape suitable for input to the PyTorch inferencing end-point. The conversion is done by: \n",
|
|
||||||
"\n",
|
|
||||||
" 1. Select a random (image, label) tuple\n",
|
|
||||||
" 2. Take the image and converting the tensor to NumPy array \n",
|
|
||||||
" 3. Reshape array into 1 x 1 x N array\n",
|
|
||||||
" * 1 image in batch, 1 color channel, N = 784 pixels for MNIST images\n",
|
|
||||||
" * Note also ```x = x.view(-1, 1, 28, 28)``` in net definition in ```train.py``` program to shape incoming scoring requests.\n",
|
|
||||||
" 4. Convert the NumPy array to list to make it into a built-in type.\n",
|
|
||||||
" 5. Create a dictionary {\"data\", <list>} that can be converted to JSON string for web service requests."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"from torchvision import datasets, transforms\n",
|
|
||||||
"import random\n",
|
|
||||||
"import numpy as np\n",
|
|
||||||
"\n",
|
|
||||||
"# Use Azure Open Datasets for MNIST dataset\n",
|
|
||||||
"datasets.MNIST.resources = [\n",
|
|
||||||
" (\"https://azureopendatastorage.azurefd.net/mnist/train-images-idx3-ubyte.gz\",\n",
|
|
||||||
" \"f68b3c2dcbeaaa9fbdd348bbdeb94873\"),\n",
|
|
||||||
" (\"https://azureopendatastorage.azurefd.net/mnist/train-labels-idx1-ubyte.gz\",\n",
|
|
||||||
" \"d53e105ee54ea40749a09fcbcd1e9432\"),\n",
|
|
||||||
" (\"https://azureopendatastorage.azurefd.net/mnist/t10k-images-idx3-ubyte.gz\",\n",
|
|
||||||
" \"9fb629c4189551a2d022fa330f9573f3\"),\n",
|
|
||||||
" (\"https://azureopendatastorage.azurefd.net/mnist/t10k-labels-idx1-ubyte.gz\",\n",
|
|
||||||
" \"ec29112dd5afa0611ce80d1b7f02629c\")\n",
|
|
||||||
"]\n",
|
|
||||||
"\n",
|
|
||||||
"test_data = datasets.MNIST('../data', train=False, transform=transforms.Compose([\n",
|
|
||||||
" transforms.ToTensor(),\n",
|
|
||||||
" transforms.Normalize((0.1307,), (0.3081,))]))\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def get_random_image():\n",
|
|
||||||
" image_idx = random.randint(0,len(test_data))\n",
|
|
||||||
" image_as_tensor = test_data[image_idx][0]\n",
|
|
||||||
" return {\"data\": elem for elem in image_as_tensor.numpy().reshape(1,1,-1).tolist()}"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"Then, invoke the web service using a random test image. Convert the dictionary containing the image to JSON string before passing it to web service.\n",
|
|
||||||
"\n",
|
|
||||||
"The response contains the raw scores for each label, with greater value indicating higher probability. Sort the labels and select the one with greatest score to get the prediction. Let's also plot the image sent to web service for comparison purposes."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"%matplotlib inline\n",
|
|
||||||
"\n",
|
|
||||||
"import json\n",
|
|
||||||
"import matplotlib.pyplot as plt\n",
|
|
||||||
"\n",
|
|
||||||
"test_image = get_random_image()\n",
|
|
||||||
"\n",
|
|
||||||
"response = webservice.run(json.dumps(test_image))\n",
|
|
||||||
"\n",
|
|
||||||
"response = sorted(response[0].items(), key = lambda x: x[1], reverse = True)\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"print(\"Predicted label:\", response[0][0])\n",
|
|
||||||
"plt.imshow(np.array(test_image[\"data\"]).reshape(28,28), cmap = \"gray\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"You can also call the web service using a raw POST method against the web service"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"import requests\n",
|
|
||||||
"\n",
|
|
||||||
"response = requests.post(url=webservice.scoring_uri, data=json.dumps(test_image),headers={\"Content-type\": \"application/json\"})\n",
|
|
||||||
"print(response.text)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"## Clean up\n",
|
|
||||||
"You can delete the ACI deployment with a delete API call."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"webservice.delete()"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ from contextlib import closing
|
|||||||
import gzip
|
import gzip
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from sklearn.utils import Bunch
|
from sklearn.utils import Bunch
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
def _is_gzip_encoded(_fsrc):
|
def _is_gzip_encoded(_fsrc):
|
||||||
@@ -29,7 +30,7 @@ _categorical_columns = [
|
|||||||
|
|
||||||
|
|
||||||
def fetch_census_dataset():
|
def fetch_census_dataset():
|
||||||
"""Fetch the Adult Census Dataset
|
"""Fetch the Adult Census Dataset.
|
||||||
|
|
||||||
This uses a particular URL for the Adult Census dataset. The code
|
This uses a particular URL for the Adult Census dataset. The code
|
||||||
is a simplified version of fetch_openml() in sklearn.
|
is a simplified version of fetch_openml() in sklearn.
|
||||||
@@ -45,21 +46,38 @@ def fetch_census_dataset():
|
|||||||
|
|
||||||
filename = "1595261.gz"
|
filename = "1595261.gz"
|
||||||
data_url = "https://rainotebookscdn.blob.core.windows.net/datasets/"
|
data_url = "https://rainotebookscdn.blob.core.windows.net/datasets/"
|
||||||
urlretrieve(data_url + filename, filename)
|
|
||||||
|
|
||||||
http_stream = gzip.GzipFile(filename=filename, mode='rb')
|
remaining_attempts = 5
|
||||||
|
sleep_duration = 10
|
||||||
|
while remaining_attempts > 0:
|
||||||
|
try:
|
||||||
|
urlretrieve(data_url + filename, filename)
|
||||||
|
|
||||||
with closing(http_stream):
|
http_stream = gzip.GzipFile(filename=filename, mode='rb')
|
||||||
def _stream_generator(response):
|
|
||||||
for line in response:
|
|
||||||
yield line.decode('utf-8')
|
|
||||||
|
|
||||||
stream = _stream_generator(http_stream)
|
with closing(http_stream):
|
||||||
data = arff.load(stream)
|
def _stream_generator(response):
|
||||||
|
for line in response:
|
||||||
|
yield line.decode('utf-8')
|
||||||
|
|
||||||
|
stream = _stream_generator(http_stream)
|
||||||
|
data = arff.load(stream)
|
||||||
|
except Exception as exc: # noqa: B902
|
||||||
|
remaining_attempts -= 1
|
||||||
|
print("Error downloading dataset from {} ({} attempt(s) remaining)"
|
||||||
|
.format(data_url, remaining_attempts))
|
||||||
|
print(exc)
|
||||||
|
sleep(sleep_duration)
|
||||||
|
sleep_duration *= 2
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
# dataset successfully downloaded
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise Exception("Could not retrieve dataset from {}.".format(data_url))
|
||||||
|
|
||||||
attributes = OrderedDict(data['attributes'])
|
attributes = OrderedDict(data['attributes'])
|
||||||
arff_columns = list(attributes)
|
arff_columns = list(attributes)
|
||||||
|
|
||||||
raw_df = pd.DataFrame(data=data['data'], columns=arff_columns)
|
raw_df = pd.DataFrame(data=data['data'], columns=arff_columns)
|
||||||
|
|
||||||
target_column_name = 'class'
|
target_column_name = 'class'
|
||||||
|
|||||||
@@ -100,7 +100,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"# Check core SDK version number\n",
|
"# Check core SDK version number\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"This notebook was created using SDK version 1.28.0, you are currently running version\", azureml.core.VERSION)"
|
"print(\"This notebook was created using SDK version 1.29.0, you are currently running version\", azureml.core.VERSION)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -102,7 +102,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import azureml.core\n",
|
"import azureml.core\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"This notebook was created using version 1.28.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user