rewrite notebooks
This commit is contained in:
Родитель
7a0bbea360
Коммит
3d25027081
|
@ -4,7 +4,9 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Configuration\n",
|
||||
"## Do the pre-req setup\n",
|
||||
"https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-configure-environment#create-workspace-configuration-file https://github.com/Azure/ViennaDocs/blob/master/PrivatePreview/notebooks/00.configuration.ipynb\n",
|
||||
"\n",
|
||||
"This notebook configures your library of notebooks to connect to an Azure Machine Learning Workspace. In this case, a library contains all of the notebooks in the current folder and any nested folders. You can configure this notebook to use an existing workspace or create a new workspace."
|
||||
]
|
||||
},
|
||||
|
@ -14,11 +16,8 @@
|
|||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#Initialize an Azure ML Workspace\n",
|
||||
"subscription_id = \"<YOUR AZURE SUBSCRIPTION NAME or ID>\" \n",
|
||||
"resource_group = \"<YOUR RESOURCE GROUP NAME>\" # e.g. myamlworkspace\n",
|
||||
"workspace_name = \"<YOUR WORKSPACE NAME>\" # e.g. myamlworkspace\n",
|
||||
"workspace_region = \"<WORKSPACE REGION>\" # e.g. eastus2 or eastus2euap"
|
||||
"# how to upgrade AML?\n",
|
||||
"# !pip install --upgrade azureml-sdk[notebooks,automl]"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -31,20 +30,13 @@
|
|||
"!az login -o table"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If you have more than one Azure account you will need to select it with the command below. If you only have one account you can skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!az account set --subscription \"$subscription_id\""
|
||||
"!az account set --subscription \"<YOUR_SUBSCRIPTION>\""
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -62,22 +54,36 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"SDK Version: 0.1.68\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!pip install --upgrade azureml-sdk[notebooks,automl]"
|
||||
"import azureml.core\n",
|
||||
"from azureml.core import Workspace\n",
|
||||
"\n",
|
||||
"print(\"SDK Version:\", azureml.core.VERSION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 36,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import azureml.core\n",
|
||||
"\n",
|
||||
"print(\"SDK Version:\", azureml.core.VERSION)"
|
||||
"#Initialize an Azure ML Workspace\n",
|
||||
"subscription_id_query = !az account show --query id -o tsv\n",
|
||||
"subscription_id = account_query.s\n",
|
||||
"resource_group = \"yanzamlworkspace\" # e.g. myamlworkspace\n",
|
||||
"workspace_name = \"yanzamlworkspace\" # e.g. myamlworkspace\n",
|
||||
"workspace_region = \"eastus2\" # e.g. eastus2 or eastus2euap"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -92,24 +98,18 @@
|
|||
"ws = Workspace.create(name = workspace_name,\n",
|
||||
" subscription_id = subscription_id,\n",
|
||||
" resource_group = resource_group, \n",
|
||||
" location = workspace_region)\n",
|
||||
"ws.get_details()"
|
||||
" location = workspace_region,\n",
|
||||
" create_resource_group=True,\n",
|
||||
" exist_ok=True)\n",
|
||||
"# persist the subscription id, resource group name, and workspace name in aml_config/config.json.\n",
|
||||
"ws.write_config()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core import Workspace\n",
|
||||
"\n",
|
||||
"ws = Workspace(workspace_name = workspace_name,\n",
|
||||
" subscription_id = subscription_id,\n",
|
||||
" resource_group = resource_group)\n",
|
||||
"\n",
|
||||
"# persist the subscription id, resource group name, and workspace name in aml_config/config.json.\n",
|
||||
"ws.write_config()"
|
||||
"Below we will reload it just to make sure that everything is working."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -122,22 +122,6 @@
|
|||
"my_workspace = Workspace.from_config()\n",
|
||||
"my_workspace.get_details()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"sample_projects_folder = './sample_projects'\n",
|
||||
"\n",
|
||||
"if not os.path.isdir(sample_projects_folder):\n",
|
||||
" os.mkdir(sample_projects_folder)\n",
|
||||
" \n",
|
||||
"print('Sample projects will be created in {}.'.format(sample_projects_folder))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -0,0 +1,203 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Develop Model Driver\n",
|
||||
"\n",
|
||||
"In this notebook, we will develop the API that will call our model. This module initializes the model, transforms the input so that it is in the appropriate format and defines the scoring method that will produce the predictions. The API will expect the input to be in JSON format. Once a request is received, the API will convert the json encoded request body into the image format. There are two main functions in the API. The first function loads the model and returns a scoring function. The second function process the images and uses the first function to score them."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core import Workspace\n",
|
||||
"from azureml.core.compute import AksCompute, ComputeTarget\n",
|
||||
"from azureml.core.webservice import Webservice, AksWebservice\n",
|
||||
"from azureml.core.image import Image\n",
|
||||
"from azureml.core.model import Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0.1.74\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import azureml.core\n",
|
||||
"print(azureml.core.VERSION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Write and save driver script"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Writing driver.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile driver.py\n",
|
||||
"def init():\n",
|
||||
" import tensorflow as tf\n",
|
||||
" from resnet152 import ResNet152\n",
|
||||
" from keras.preprocessing import image\n",
|
||||
" from keras.applications.imagenet_utils import preprocess_input, decode_predictions\n",
|
||||
"\n",
|
||||
" import numpy as np\n",
|
||||
" import timeit as t\n",
|
||||
" import base64\n",
|
||||
" import json\n",
|
||||
" from PIL import Image, ImageOps\n",
|
||||
" from io import BytesIO\n",
|
||||
" import logging\n",
|
||||
"\n",
|
||||
" global model\n",
|
||||
" model = ResNet152(weights='imagenet')\n",
|
||||
" print('Model loaded')\n",
|
||||
" \n",
|
||||
"def run(inputString):\n",
|
||||
" \n",
|
||||
" import tensorflow as tf\n",
|
||||
" from resnet152 import ResNet152\n",
|
||||
" from keras.preprocessing import image\n",
|
||||
" from keras.applications.imagenet_utils import preprocess_input, decode_predictions\n",
|
||||
"\n",
|
||||
" import numpy as np\n",
|
||||
" import timeit as t\n",
|
||||
" import base64\n",
|
||||
" import json\n",
|
||||
" from PIL import Image, ImageOps\n",
|
||||
" from io import BytesIO\n",
|
||||
" import logging \n",
|
||||
" \n",
|
||||
" model = ResNet152(weights='imagenet')\n",
|
||||
" print('Model loaded')\n",
|
||||
" \n",
|
||||
" responses = []\n",
|
||||
" base64Dict = json.loads(inputString)\n",
|
||||
"\n",
|
||||
" for k, v in base64Dict.items():\n",
|
||||
" img_file_name, base64Img = k, v\n",
|
||||
" decoded_img = base64.b64decode(base64Img)\n",
|
||||
" img_buffer = BytesIO(decoded_img)\n",
|
||||
" imageData = Image.open(img_buffer).convert(\"RGB\")\n",
|
||||
" \n",
|
||||
" # Evaluate the model using the input data\n",
|
||||
" img = ImageOps.fit(imageData, (224,224), Image.ANTIALIAS)\n",
|
||||
" img = np.array(img) # shape: (224, 224, 3)\n",
|
||||
" \n",
|
||||
" img = np.expand_dims(img, axis=0)\n",
|
||||
" img = preprocess_input(img)\n",
|
||||
" \n",
|
||||
" preds = model.predict(img)\n",
|
||||
" print('Predicted:', decode_predictions(preds, top=3))\n",
|
||||
" resp = {img_file_name: str(decode_predictions(preds, top=3))}\n",
|
||||
"\n",
|
||||
" responses.append(resp)\n",
|
||||
" return json.dumps(responses) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run driver.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Test the driver¶ \n",
|
||||
"We test the driver by passing data."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Using TensorFlow backend.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Model loaded\n",
|
||||
"Predicted: [[('n02127052', 'lynx', 0.9816487), ('n02128385', 'leopard', 0.007744099), ('n02123159', 'tiger_cat', 0.0036861112)]]\n",
|
||||
"[{\"220px-Lynx_lynx_poing.jpg\": \"[[('n02127052', 'lynx', 0.9816487), ('n02128385', 'leopard', 0.007744099), ('n02123159', 'tiger_cat', 0.0036861112)]]\"}]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from io import BytesIO\n",
|
||||
"from PIL import Image, ImageOps\n",
|
||||
"import base64\n",
|
||||
"import json\n",
|
||||
"\n",
|
||||
"img_path = '220px-Lynx_lynx_poing.jpg'\n",
|
||||
"encoded = None\n",
|
||||
"with open(img_path, 'rb') as file:\n",
|
||||
" encoded = base64.b64encode(file.read())\n",
|
||||
"img_dict = {img_path: encoded.decode('utf-8')}\n",
|
||||
"body = json.dumps(img_dict)\n",
|
||||
"\n",
|
||||
"resp = run(body)\n",
|
||||
"print(resp)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"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.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
|
@ -0,0 +1,323 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core import Workspace\n",
|
||||
"from azureml.core.compute import AksCompute, ComputeTarget\n",
|
||||
"from azureml.core.webservice import Webservice, AksWebservice\n",
|
||||
"from azureml.core.image import Image\n",
|
||||
"from azureml.core.model import Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import azureml.core\n",
|
||||
"print(azureml.core.VERSION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Get workspace\n",
|
||||
"Load existing workspace from the config file info."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core.workspace import Workspace\n",
|
||||
"\n",
|
||||
"ws = Workspace.from_config()\n",
|
||||
"print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep = '\\n')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%writefile deploy_environment.yml\n",
|
||||
"name: project_environment\n",
|
||||
"dependencies:\n",
|
||||
" # The python interpreter version.\n",
|
||||
" # Currently Azure ML only supports 3.5.2 and later.\n",
|
||||
"- python=3.6\n",
|
||||
"- pip:\n",
|
||||
" # Required packages for AzureML execution, history, and data preparation.\n",
|
||||
" - azureml-defaults\n",
|
||||
" - papermill==0.14.1\n",
|
||||
" - python-dotenv==0.9.0\n",
|
||||
" - Pillow==5.2.0\n",
|
||||
" - wget==3.2\n",
|
||||
" - aiohttp==3.3.2\n",
|
||||
" - toolz==0.9.0\n",
|
||||
" - tqdm==4.23.4\n",
|
||||
" - keras==2.2.0\n",
|
||||
"- scikit-learn\n",
|
||||
"- zlib==1.2.11\n",
|
||||
"- libgcc=5.2.0\n",
|
||||
"- tensorflow==1.8.0\n",
|
||||
"- tornado==4.5.3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core.image import ContainerImage\n",
|
||||
"\n",
|
||||
"image_config = ContainerImage.image_configuration(execution_script = \"driver.py\",\n",
|
||||
" runtime = \"python\",\n",
|
||||
" conda_file = \"deploy_environment.yml\",\n",
|
||||
" docker_file = \"mydockerfile\",\n",
|
||||
" description = \"Image for AKS Deployment Tutorial\",\n",
|
||||
" tags = {\"name\":\"AKS\",\"project\":\"AML\"}, \n",
|
||||
" dependencies = [\"resnet152.py\"],\n",
|
||||
" enable_gpu = True\n",
|
||||
" )\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"get_workspace error using subscription_id=edf507a2-6235-46c5-b560-fd463ba2e771, resource_group_name=yanzamlworkspace, workspace_name=yanzamlworkspace\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Found the config file in: /workspace/AKSDeploymentTutorial_AML/Keras_Tensorflow/aml_config/config.json\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "ProjectSystemException",
|
||||
"evalue": "Workspace not found.",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mErrorResponseWrapperException\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/_project/_commands.py\u001b[0m in \u001b[0;36mget_workspace\u001b[0;34m(auth, subscription_id, resource_group_name, workspace_name)\u001b[0m\n\u001b[1;32m 330\u001b[0m \u001b[0mresource_group_name\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 331\u001b[0;31m workspace_name)\n\u001b[0m\u001b[1;32m 332\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mErrorResponseWrapperException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mresponse_exception\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/_base_sdk_common/workspace/operations/workspaces_operations.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, resource_group_name, workspace_name, custom_headers, raw, **operation_config)\u001b[0m\n\u001b[1;32m 77\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 78\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mmodels\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mErrorResponseWrapperException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_deserialize\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 79\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mErrorResponseWrapperException\u001b[0m: Operation returned an invalid status code 'Not Found'",
|
||||
"\nDuring handling of the above exception, another exception occurred:\n",
|
||||
"\u001b[0;31mProjectSystemException\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-4-11154f14bcec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_workspace\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mWorkspace\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_config\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/core/workspace.py\u001b[0m in \u001b[0;36mfrom_config\u001b[0;34m(path, auth)\u001b[0m\n\u001b[1;32m 151\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Found the config file in: {}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfound_path\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 152\u001b[0m return Workspace(subscription_id_from_config, resource_group_from_config, workspace_name_from_config,\n\u001b[0;32m--> 153\u001b[0;31m auth=auth)\n\u001b[0m\u001b[1;32m 154\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 155\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mstaticmethod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/core/workspace.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, subscription_id, resource_group, workspace_name, auth, _location, _disable_service_check)\u001b[0m\n\u001b[1;32m 84\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 85\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_disable_service_check\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 86\u001b[0;31m \u001b[0mauto_rest_workspace\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_commands\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_workspace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mauth\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msubscription_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresource_group\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mworkspace_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 87\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_workspace_autorest_object\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mauto_rest_workspace\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 88\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/_project/_commands.py\u001b[0m in \u001b[0;36mget_workspace\u001b[0;34m(auth, subscription_id, resource_group_name, workspace_name)\u001b[0m\n\u001b[1;32m 335\u001b[0m \u001b[0msubscription_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresource_group_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mworkspace_name\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 336\u001b[0m ))\n\u001b[0;32m--> 337\u001b[0;31m \u001b[0mresource_error_handling\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresponse_exception\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mWORKSPACE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 338\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 339\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/_base_sdk_common/common.py\u001b[0m in \u001b[0;36mresource_error_handling\u001b[0;34m(response_exception, resource_type)\u001b[0m\n\u001b[1;32m 255\u001b[0m \u001b[0;34m\"\"\"General error handling for projects\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 256\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresponse_exception\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m404\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 257\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mProjectSystemException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"{resource_type} not found.\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresource_type\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mresource_type\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 258\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 259\u001b[0m \u001b[0mresponse_message\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_http_exception_response_string\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresponse_exception\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mProjectSystemException\u001b[0m: Workspace not found."
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"my_workspace = Workspace.from_config()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"my_workspace.models"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image = ContainerImage.create(name = \"image1\",\n",
|
||||
" # this is the model object\n",
|
||||
" models = [], \n",
|
||||
" image_config = image_config,\n",
|
||||
" workspace = ws)\n",
|
||||
"\n",
|
||||
"image.wait_for_creation(show_output = True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image_name = image.image_location\n",
|
||||
"image_name"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"registry_name = my_workspace.get_details()['containerRegistry'].split('/')[-1]\n",
|
||||
"query_results = !az acr show -n {registry_name} -g {my_workspace.resource_group} --query loginServer -o tsv\n",
|
||||
"acr_server = query_results.s\n",
|
||||
"query_results = !az acr credential show -n {registry_name} -g {my_workspace.resource_group}\n",
|
||||
"acr_credentials = json.loads(query_results.s)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dc = get_docker_client(acr_credentials['username'], \n",
|
||||
" acr_credentials['passwords'][0]['value'], \n",
|
||||
" acr_server)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pull_docker_image(dc, image_name, acr_credentials['username'], acr_credentials['passwords'][0]['value'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!kubectl --kubeconfig config get services"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"container_labels = {'containerName': 'mama'}\n",
|
||||
"container = dc.containers.run(image_name, \n",
|
||||
" detach=True, \n",
|
||||
" ports={'5001/tcp': 80},\n",
|
||||
" labels=container_labels,\n",
|
||||
" runtime='nvidia' )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(container.logs().decode('UTF8'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client = docker.APIClient()\n",
|
||||
"details = client.inspect_container(container.id)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"service_ip = details['NetworkSettings']['Ports']['5001/tcp'][0]['HostIp']\n",
|
||||
"service_port = details['NetworkSettings']['Ports']['5001/tcp'][0]['HostPort']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!curl {service_ip}:{service_port}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"headers = {'Content-Type': 'application/json'}\n",
|
||||
"headers.update(my_workspace._auth.get_authentication_header())\n",
|
||||
"params = {'api-version': MMS_WORKSPACE_API_VERSION}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"service_url = 'http://{}:{}/score'.format(service_ip, service_port)\n",
|
||||
"headers = {\"content-type\": \"application/json\"}\n",
|
||||
"%time r = requests.post(service_url, data=jsonimg, headers=headers)\n",
|
||||
"print(r)\n",
|
||||
"r.json()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"container.stop()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"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.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
|
@ -0,0 +1,342 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Test web application locally¶ \n",
|
||||
"This notebook pulls some images and tests them against the local web app running inside the Docker container we made previously."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"from testing_utilities import to_img, img_url_to_json, plot_predictions\n",
|
||||
"import requests\n",
|
||||
"from dotenv import get_key, find_dotenv\n",
|
||||
"from azureml._model_management._constants import MMS_WORKSPACE_API_VERSION\n",
|
||||
"from azureml._model_management._util import (get_docker_client, pull_docker_image, get_docker_port, \n",
|
||||
" container_scoring_call, cleanup_container)\n",
|
||||
"from azureml.core import Workspace\n",
|
||||
"from testing_utilities import img_url_to_json\n",
|
||||
"import json\n",
|
||||
"import docker\n",
|
||||
"%matplotlib inline"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import azureml.core\n",
|
||||
"print(azureml.core.VERSION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"my_workspace = Workspace.from_config()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image = my_workspace.images['image1']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image_name = image.image_location"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"registry_name = my_workspace.get_details()['containerRegistry'].split('/')[-1]\n",
|
||||
"query_results = !az acr show -n {registry_name} -g {my_workspace.resource_group} --query loginServer -o tsv\n",
|
||||
"acr_server = query_results.s\n",
|
||||
"query_results = !az acr credential show -n {registry_name} -g {my_workspace.resource_group}\n",
|
||||
"acr_credentials = json.loads(query_results.s)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dc = get_docker_client(acr_credentials['username'], \n",
|
||||
" acr_credentials['passwords'][0]['value'], \n",
|
||||
" acr_server)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pull_docker_image(dc, image_name, acr_credentials['username'], acr_credentials['passwords'][0]['value'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Run the Docker conatainer in the background and open port 80. Notice we are using nvidia-docker and not docker command."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"container_labels = {'containerName': 'pytorchgpu'}\n",
|
||||
"container = dc.containers.run(image_name, \n",
|
||||
" detach=True, \n",
|
||||
" ports={'5001/tcp': 80},\n",
|
||||
" labels=container_labels,\n",
|
||||
" runtime='nvidia' )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# TODO Add wait function\n",
|
||||
"container_logs = container.logs().decode('UTF8')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%time container_logs.find(\"Users's init has completed successfully\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(container.logs().decode('UTF8'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client = docker.APIClient()\n",
|
||||
"details = client.inspect_container(container.id)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"service_ip = details['NetworkSettings']['Ports']['5001/tcp'][0]['HostIp']\n",
|
||||
"service_port = details['NetworkSettings']['Ports']['5001/tcp'][0]['HostPort']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Wait a few seconds for the application to spin up and then check that everything works."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print('Checking service on {} port {}'.format(service_ip, service_port))\n",
|
||||
"!curl 'http://{service_ip}:{service_port}/'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"IMAGEURL = \"https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Lynx_lynx_poing.jpg/220px-Lynx_lynx_poing.jpg\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.imshow(to_img(IMAGEURL))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"jsonimg = img_url_to_json(IMAGEURL)\n",
|
||||
"jsonimg[:100]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"headers = {\"content-type\": \"application/json\"}\n",
|
||||
"%time r = requests.post('http://0.0.0.0:80/score', data=jsonimg, headers=headers)\n",
|
||||
"print(r)\n",
|
||||
"r.json()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let's try a few more images."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"images = (\n",
|
||||
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Lynx_lynx_poing.jpg/220px-Lynx_lynx_poing.jpg\",\n",
|
||||
" \"https://upload.wikimedia.org/wikipedia/commons/3/3a/Roadster_2.5_windmills_trimmed.jpg\",\n",
|
||||
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Harmony_of_the_Seas_%28ship%2C_2016%29_001.jpg/1920px-Harmony_of_the_Seas_%28ship%2C_2016%29_001.jpg\",\n",
|
||||
" \"http://yourshot.nationalgeographic.com/u/ss/fQYSUbVfts-T7pS2VP2wnKyN8wxywmXtY0-FwsgxpiZv_E9ZfPsNV5B0ER8-bOdruvNfMD5EbP4SznWz4PYn/\",\n",
|
||||
" \"https://cdn.arstechnica.net/wp-content/uploads/2012/04/bohol_tarsier_wiki-4f88309-intro.jpg\",\n",
|
||||
" \"http://i.telegraph.co.uk/multimedia/archive/03233/BIRDS-ROBIN_3233998b.jpg\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"url = \"http://0.0.0.0:80/score\"\n",
|
||||
"results = [\n",
|
||||
" requests.post(url, data=img_url_to_json(img), headers=headers) for img in images\n",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plot_predictions(images, results)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image_data = list(map(img_url_to_json, images)) # Retrieve the images and data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"timer_results = list()\n",
|
||||
"for img in image_data:\n",
|
||||
" res=%timeit -r 1 -o -q requests.post(url, data=img, headers=headers)\n",
|
||||
" timer_results.append(res.best)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"timer_results"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(\"Average time taken: {0:4.2f} ms\".format(10 ** 3 * np.mean(timer_results)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"container.stop()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"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.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -4,7 +4,9 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Configuration\n",
|
||||
"## Do the pre-req setup\n",
|
||||
"https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-configure-environment#create-workspace-configuration-file https://github.com/Azure/ViennaDocs/blob/master/PrivatePreview/notebooks/00.configuration.ipynb\n",
|
||||
"\n",
|
||||
"This notebook configures your library of notebooks to connect to an Azure Machine Learning Workspace. In this case, a library contains all of the notebooks in the current folder and any nested folders. You can configure this notebook to use an existing workspace or create a new workspace."
|
||||
]
|
||||
},
|
||||
|
@ -14,11 +16,8 @@
|
|||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#Initialize an Azure ML Workspace\n",
|
||||
"subscription_id = \"<YOUR AZURE SUBSCRIPTION NAME or ID>\" \n",
|
||||
"resource_group = \"<YOUR RESOURCE GROUP NAME>\" # e.g. myamlworkspace\n",
|
||||
"workspace_name = \"<YOUR WORKSPACE NAME>\" # e.g. myamlworkspace\n",
|
||||
"workspace_region = \"<WORKSPACE REGION>\" # e.g. eastus2 or eastus2euap"
|
||||
"# how to upgrade AML?\n",
|
||||
"# !pip install --upgrade azureml-sdk[notebooks,automl]"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -31,20 +30,13 @@
|
|||
"!az login -o table"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If you have more than one Azure account you will need to select it with the command below. If you only have one account you can skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!az account set --subscription \"$subscription_id\""
|
||||
"!az account set --subscription \"<YOUR_SUBSCRIPTION>\""
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -62,22 +54,36 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"SDK Version: 0.1.68\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!pip install --upgrade azureml-sdk[notebooks,automl]"
|
||||
"import azureml.core\n",
|
||||
"from azureml.core import Workspace\n",
|
||||
"\n",
|
||||
"print(\"SDK Version:\", azureml.core.VERSION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 36,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import azureml.core\n",
|
||||
"\n",
|
||||
"print(\"SDK Version:\", azureml.core.VERSION)"
|
||||
"#Initialize an Azure ML Workspace\n",
|
||||
"subscription_id_query = !az account show --query id -o tsv\n",
|
||||
"subscription_id = account_query.s\n",
|
||||
"resource_group = \"yanzamlworkspace\" # e.g. myamlworkspace\n",
|
||||
"workspace_name = \"yanzamlworkspace\" # e.g. myamlworkspace\n",
|
||||
"workspace_region = \"eastus2\" # e.g. eastus2 or eastus2euap"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -92,24 +98,18 @@
|
|||
"ws = Workspace.create(name = workspace_name,\n",
|
||||
" subscription_id = subscription_id,\n",
|
||||
" resource_group = resource_group, \n",
|
||||
" location = workspace_region)\n",
|
||||
"ws.get_details()"
|
||||
" location = workspace_region,\n",
|
||||
" create_resource_group=True,\n",
|
||||
" exist_ok=True)\n",
|
||||
"# persist the subscription id, resource group name, and workspace name in aml_config/config.json.\n",
|
||||
"ws.write_config()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core import Workspace\n",
|
||||
"\n",
|
||||
"ws = Workspace(workspace_name = workspace_name,\n",
|
||||
" subscription_id = subscription_id,\n",
|
||||
" resource_group = resource_group)\n",
|
||||
"\n",
|
||||
"# persist the subscription id, resource group name, and workspace name in aml_config/config.json.\n",
|
||||
"ws.write_config()"
|
||||
"Below we will reload it just to make sure that everything is working."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -122,22 +122,6 @@
|
|||
"my_workspace = Workspace.from_config()\n",
|
||||
"my_workspace.get_details()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"sample_projects_folder = './sample_projects'\n",
|
||||
"\n",
|
||||
"if not os.path.isdir(sample_projects_folder):\n",
|
||||
" os.mkdir(sample_projects_folder)\n",
|
||||
" \n",
|
||||
"print('Sample projects will be created in {}.'.format(sample_projects_folder))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -0,0 +1,203 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Develop Model Driver\n",
|
||||
"\n",
|
||||
"In this notebook, we will develop the API that will call our model. This module initializes the model, transforms the input so that it is in the appropriate format and defines the scoring method that will produce the predictions. The API will expect the input to be in JSON format. Once a request is received, the API will convert the json encoded request body into the image format. There are two main functions in the API. The first function loads the model and returns a scoring function. The second function process the images and uses the first function to score them."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core import Workspace\n",
|
||||
"from azureml.core.compute import AksCompute, ComputeTarget\n",
|
||||
"from azureml.core.webservice import Webservice, AksWebservice\n",
|
||||
"from azureml.core.image import Image\n",
|
||||
"from azureml.core.model import Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0.1.74\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import azureml.core\n",
|
||||
"print(azureml.core.VERSION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Write and save driver script"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Writing driver.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile driver.py\n",
|
||||
"def init():\n",
|
||||
" import tensorflow as tf\n",
|
||||
" from resnet152 import ResNet152\n",
|
||||
" from keras.preprocessing import image\n",
|
||||
" from keras.applications.imagenet_utils import preprocess_input, decode_predictions\n",
|
||||
"\n",
|
||||
" import numpy as np\n",
|
||||
" import timeit as t\n",
|
||||
" import base64\n",
|
||||
" import json\n",
|
||||
" from PIL import Image, ImageOps\n",
|
||||
" from io import BytesIO\n",
|
||||
" import logging\n",
|
||||
"\n",
|
||||
" global model\n",
|
||||
" model = ResNet152(weights='imagenet')\n",
|
||||
" print('Model loaded')\n",
|
||||
" \n",
|
||||
"def run(inputString):\n",
|
||||
" \n",
|
||||
" import tensorflow as tf\n",
|
||||
" from resnet152 import ResNet152\n",
|
||||
" from keras.preprocessing import image\n",
|
||||
" from keras.applications.imagenet_utils import preprocess_input, decode_predictions\n",
|
||||
"\n",
|
||||
" import numpy as np\n",
|
||||
" import timeit as t\n",
|
||||
" import base64\n",
|
||||
" import json\n",
|
||||
" from PIL import Image, ImageOps\n",
|
||||
" from io import BytesIO\n",
|
||||
" import logging \n",
|
||||
" \n",
|
||||
" model = ResNet152(weights='imagenet')\n",
|
||||
" print('Model loaded')\n",
|
||||
" \n",
|
||||
" responses = []\n",
|
||||
" base64Dict = json.loads(inputString)\n",
|
||||
"\n",
|
||||
" for k, v in base64Dict.items():\n",
|
||||
" img_file_name, base64Img = k, v\n",
|
||||
" decoded_img = base64.b64decode(base64Img)\n",
|
||||
" img_buffer = BytesIO(decoded_img)\n",
|
||||
" imageData = Image.open(img_buffer).convert(\"RGB\")\n",
|
||||
" \n",
|
||||
" # Evaluate the model using the input data\n",
|
||||
" img = ImageOps.fit(imageData, (224,224), Image.ANTIALIAS)\n",
|
||||
" img = np.array(img) # shape: (224, 224, 3)\n",
|
||||
" \n",
|
||||
" img = np.expand_dims(img, axis=0)\n",
|
||||
" img = preprocess_input(img)\n",
|
||||
" \n",
|
||||
" preds = model.predict(img)\n",
|
||||
" print('Predicted:', decode_predictions(preds, top=3))\n",
|
||||
" resp = {img_file_name: str(decode_predictions(preds, top=3))}\n",
|
||||
"\n",
|
||||
" responses.append(resp)\n",
|
||||
" return json.dumps(responses) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run driver.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Test the driver¶ \n",
|
||||
"We test the driver by passing data."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Using TensorFlow backend.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Model loaded\n",
|
||||
"Predicted: [[('n02127052', 'lynx', 0.9816487), ('n02128385', 'leopard', 0.007744099), ('n02123159', 'tiger_cat', 0.0036861112)]]\n",
|
||||
"[{\"220px-Lynx_lynx_poing.jpg\": \"[[('n02127052', 'lynx', 0.9816487), ('n02128385', 'leopard', 0.007744099), ('n02123159', 'tiger_cat', 0.0036861112)]]\"}]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from io import BytesIO\n",
|
||||
"from PIL import Image, ImageOps\n",
|
||||
"import base64\n",
|
||||
"import json\n",
|
||||
"\n",
|
||||
"img_path = '220px-Lynx_lynx_poing.jpg'\n",
|
||||
"encoded = None\n",
|
||||
"with open(img_path, 'rb') as file:\n",
|
||||
" encoded = base64.b64encode(file.read())\n",
|
||||
"img_dict = {img_path: encoded.decode('utf-8')}\n",
|
||||
"body = json.dumps(img_dict)\n",
|
||||
"\n",
|
||||
"resp = run(body)\n",
|
||||
"print(resp)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"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.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
|
@ -0,0 +1,323 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core import Workspace\n",
|
||||
"from azureml.core.compute import AksCompute, ComputeTarget\n",
|
||||
"from azureml.core.webservice import Webservice, AksWebservice\n",
|
||||
"from azureml.core.image import Image\n",
|
||||
"from azureml.core.model import Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import azureml.core\n",
|
||||
"print(azureml.core.VERSION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Get workspace\n",
|
||||
"Load existing workspace from the config file info."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core.workspace import Workspace\n",
|
||||
"\n",
|
||||
"ws = Workspace.from_config()\n",
|
||||
"print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep = '\\n')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%writefile deploy_environment.yml\n",
|
||||
"name: project_environment\n",
|
||||
"dependencies:\n",
|
||||
" # The python interpreter version.\n",
|
||||
" # Currently Azure ML only supports 3.5.2 and later.\n",
|
||||
"- python=3.6\n",
|
||||
"- pip:\n",
|
||||
" # Required packages for AzureML execution, history, and data preparation.\n",
|
||||
" - azureml-defaults\n",
|
||||
" - papermill==0.14.1\n",
|
||||
" - python-dotenv==0.9.0\n",
|
||||
" - Pillow==5.2.0\n",
|
||||
" - wget==3.2\n",
|
||||
" - aiohttp==3.3.2\n",
|
||||
" - toolz==0.9.0\n",
|
||||
" - tqdm==4.23.4\n",
|
||||
" - keras==2.2.0\n",
|
||||
"- scikit-learn\n",
|
||||
"- zlib==1.2.11\n",
|
||||
"- libgcc=5.2.0\n",
|
||||
"- tensorflow==1.8.0\n",
|
||||
"- tornado==4.5.3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core.image import ContainerImage\n",
|
||||
"\n",
|
||||
"image_config = ContainerImage.image_configuration(execution_script = \"driver.py\",\n",
|
||||
" runtime = \"python\",\n",
|
||||
" conda_file = \"deploy_environment.yml\",\n",
|
||||
" docker_file = \"mydockerfile\",\n",
|
||||
" description = \"Image for AKS Deployment Tutorial\",\n",
|
||||
" tags = {\"name\":\"AKS\",\"project\":\"AML\"}, \n",
|
||||
" dependencies = [\"resnet152.py\"],\n",
|
||||
" enable_gpu = True\n",
|
||||
" )\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"get_workspace error using subscription_id=edf507a2-6235-46c5-b560-fd463ba2e771, resource_group_name=yanzamlworkspace, workspace_name=yanzamlworkspace\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Found the config file in: /workspace/AKSDeploymentTutorial_AML/Keras_Tensorflow/aml_config/config.json\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "ProjectSystemException",
|
||||
"evalue": "Workspace not found.",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mErrorResponseWrapperException\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/_project/_commands.py\u001b[0m in \u001b[0;36mget_workspace\u001b[0;34m(auth, subscription_id, resource_group_name, workspace_name)\u001b[0m\n\u001b[1;32m 330\u001b[0m \u001b[0mresource_group_name\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 331\u001b[0;31m workspace_name)\n\u001b[0m\u001b[1;32m 332\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mErrorResponseWrapperException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mresponse_exception\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/_base_sdk_common/workspace/operations/workspaces_operations.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, resource_group_name, workspace_name, custom_headers, raw, **operation_config)\u001b[0m\n\u001b[1;32m 77\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 78\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mmodels\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mErrorResponseWrapperException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_deserialize\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 79\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mErrorResponseWrapperException\u001b[0m: Operation returned an invalid status code 'Not Found'",
|
||||
"\nDuring handling of the above exception, another exception occurred:\n",
|
||||
"\u001b[0;31mProjectSystemException\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-4-11154f14bcec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_workspace\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mWorkspace\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_config\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/core/workspace.py\u001b[0m in \u001b[0;36mfrom_config\u001b[0;34m(path, auth)\u001b[0m\n\u001b[1;32m 151\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Found the config file in: {}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfound_path\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 152\u001b[0m return Workspace(subscription_id_from_config, resource_group_from_config, workspace_name_from_config,\n\u001b[0;32m--> 153\u001b[0;31m auth=auth)\n\u001b[0m\u001b[1;32m 154\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 155\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mstaticmethod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/core/workspace.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, subscription_id, resource_group, workspace_name, auth, _location, _disable_service_check)\u001b[0m\n\u001b[1;32m 84\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 85\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_disable_service_check\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 86\u001b[0;31m \u001b[0mauto_rest_workspace\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_commands\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_workspace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mauth\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msubscription_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresource_group\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mworkspace_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 87\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_workspace_autorest_object\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mauto_rest_workspace\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 88\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/_project/_commands.py\u001b[0m in \u001b[0;36mget_workspace\u001b[0;34m(auth, subscription_id, resource_group_name, workspace_name)\u001b[0m\n\u001b[1;32m 335\u001b[0m \u001b[0msubscription_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresource_group_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mworkspace_name\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 336\u001b[0m ))\n\u001b[0;32m--> 337\u001b[0;31m \u001b[0mresource_error_handling\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresponse_exception\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mWORKSPACE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 338\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 339\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/opt/conda/envs/py3.6/lib/python3.6/site-packages/azureml/_base_sdk_common/common.py\u001b[0m in \u001b[0;36mresource_error_handling\u001b[0;34m(response_exception, resource_type)\u001b[0m\n\u001b[1;32m 255\u001b[0m \u001b[0;34m\"\"\"General error handling for projects\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 256\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresponse_exception\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m404\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 257\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mProjectSystemException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"{resource_type} not found.\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresource_type\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mresource_type\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 258\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 259\u001b[0m \u001b[0mresponse_message\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_http_exception_response_string\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresponse_exception\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mProjectSystemException\u001b[0m: Workspace not found."
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"my_workspace = Workspace.from_config()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"my_workspace.models"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image = ContainerImage.create(name = \"image1\",\n",
|
||||
" # this is the model object\n",
|
||||
" models = [], \n",
|
||||
" image_config = image_config,\n",
|
||||
" workspace = ws)\n",
|
||||
"\n",
|
||||
"image.wait_for_creation(show_output = True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image_name = image.image_location\n",
|
||||
"image_name"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"registry_name = my_workspace.get_details()['containerRegistry'].split('/')[-1]\n",
|
||||
"query_results = !az acr show -n {registry_name} -g {my_workspace.resource_group} --query loginServer -o tsv\n",
|
||||
"acr_server = query_results.s\n",
|
||||
"query_results = !az acr credential show -n {registry_name} -g {my_workspace.resource_group}\n",
|
||||
"acr_credentials = json.loads(query_results.s)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dc = get_docker_client(acr_credentials['username'], \n",
|
||||
" acr_credentials['passwords'][0]['value'], \n",
|
||||
" acr_server)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pull_docker_image(dc, image_name, acr_credentials['username'], acr_credentials['passwords'][0]['value'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!kubectl --kubeconfig config get services"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"container_labels = {'containerName': 'mama'}\n",
|
||||
"container = dc.containers.run(image_name, \n",
|
||||
" detach=True, \n",
|
||||
" ports={'5001/tcp': 80},\n",
|
||||
" labels=container_labels,\n",
|
||||
" runtime='nvidia' )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(container.logs().decode('UTF8'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client = docker.APIClient()\n",
|
||||
"details = client.inspect_container(container.id)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"service_ip = details['NetworkSettings']['Ports']['5001/tcp'][0]['HostIp']\n",
|
||||
"service_port = details['NetworkSettings']['Ports']['5001/tcp'][0]['HostPort']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!curl {service_ip}:{service_port}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"headers = {'Content-Type': 'application/json'}\n",
|
||||
"headers.update(my_workspace._auth.get_authentication_header())\n",
|
||||
"params = {'api-version': MMS_WORKSPACE_API_VERSION}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"service_url = 'http://{}:{}/score'.format(service_ip, service_port)\n",
|
||||
"headers = {\"content-type\": \"application/json\"}\n",
|
||||
"%time r = requests.post(service_url, data=jsonimg, headers=headers)\n",
|
||||
"print(r)\n",
|
||||
"r.json()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"container.stop()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"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.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
|
@ -0,0 +1,342 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Test web application locally¶ \n",
|
||||
"This notebook pulls some images and tests them against the local web app running inside the Docker container we made previously."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"from testing_utilities import to_img, img_url_to_json, plot_predictions\n",
|
||||
"import requests\n",
|
||||
"from dotenv import get_key, find_dotenv\n",
|
||||
"from azureml._model_management._constants import MMS_WORKSPACE_API_VERSION\n",
|
||||
"from azureml._model_management._util import (get_docker_client, pull_docker_image, get_docker_port, \n",
|
||||
" container_scoring_call, cleanup_container)\n",
|
||||
"from azureml.core import Workspace\n",
|
||||
"from testing_utilities import img_url_to_json\n",
|
||||
"import json\n",
|
||||
"import docker\n",
|
||||
"%matplotlib inline"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import azureml.core\n",
|
||||
"print(azureml.core.VERSION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"my_workspace = Workspace.from_config()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image = my_workspace.images['image1']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image_name = image.image_location"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"registry_name = my_workspace.get_details()['containerRegistry'].split('/')[-1]\n",
|
||||
"query_results = !az acr show -n {registry_name} -g {my_workspace.resource_group} --query loginServer -o tsv\n",
|
||||
"acr_server = query_results.s\n",
|
||||
"query_results = !az acr credential show -n {registry_name} -g {my_workspace.resource_group}\n",
|
||||
"acr_credentials = json.loads(query_results.s)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dc = get_docker_client(acr_credentials['username'], \n",
|
||||
" acr_credentials['passwords'][0]['value'], \n",
|
||||
" acr_server)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pull_docker_image(dc, image_name, acr_credentials['username'], acr_credentials['passwords'][0]['value'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Run the Docker conatainer in the background and open port 80. Notice we are using nvidia-docker and not docker command."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"container_labels = {'containerName': 'pytorchgpu'}\n",
|
||||
"container = dc.containers.run(image_name, \n",
|
||||
" detach=True, \n",
|
||||
" ports={'5001/tcp': 80},\n",
|
||||
" labels=container_labels,\n",
|
||||
" runtime='nvidia' )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# TODO Add wait function\n",
|
||||
"container_logs = container.logs().decode('UTF8')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%time container_logs.find(\"Users's init has completed successfully\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(container.logs().decode('UTF8'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client = docker.APIClient()\n",
|
||||
"details = client.inspect_container(container.id)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"service_ip = details['NetworkSettings']['Ports']['5001/tcp'][0]['HostIp']\n",
|
||||
"service_port = details['NetworkSettings']['Ports']['5001/tcp'][0]['HostPort']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Wait a few seconds for the application to spin up and then check that everything works."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print('Checking service on {} port {}'.format(service_ip, service_port))\n",
|
||||
"!curl 'http://{service_ip}:{service_port}/'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"IMAGEURL = \"https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Lynx_lynx_poing.jpg/220px-Lynx_lynx_poing.jpg\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.imshow(to_img(IMAGEURL))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"jsonimg = img_url_to_json(IMAGEURL)\n",
|
||||
"jsonimg[:100]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"headers = {\"content-type\": \"application/json\"}\n",
|
||||
"%time r = requests.post('http://0.0.0.0:80/score', data=jsonimg, headers=headers)\n",
|
||||
"print(r)\n",
|
||||
"r.json()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let's try a few more images."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"images = (\n",
|
||||
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Lynx_lynx_poing.jpg/220px-Lynx_lynx_poing.jpg\",\n",
|
||||
" \"https://upload.wikimedia.org/wikipedia/commons/3/3a/Roadster_2.5_windmills_trimmed.jpg\",\n",
|
||||
" \"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Harmony_of_the_Seas_%28ship%2C_2016%29_001.jpg/1920px-Harmony_of_the_Seas_%28ship%2C_2016%29_001.jpg\",\n",
|
||||
" \"http://yourshot.nationalgeographic.com/u/ss/fQYSUbVfts-T7pS2VP2wnKyN8wxywmXtY0-FwsgxpiZv_E9ZfPsNV5B0ER8-bOdruvNfMD5EbP4SznWz4PYn/\",\n",
|
||||
" \"https://cdn.arstechnica.net/wp-content/uploads/2012/04/bohol_tarsier_wiki-4f88309-intro.jpg\",\n",
|
||||
" \"http://i.telegraph.co.uk/multimedia/archive/03233/BIRDS-ROBIN_3233998b.jpg\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"url = \"http://0.0.0.0:80/score\"\n",
|
||||
"results = [\n",
|
||||
" requests.post(url, data=img_url_to_json(img), headers=headers) for img in images\n",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plot_predictions(images, results)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image_data = list(map(img_url_to_json, images)) # Retrieve the images and data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"timer_results = list()\n",
|
||||
"for img in image_data:\n",
|
||||
" res=%timeit -r 1 -o -q requests.post(url, data=img, headers=headers)\n",
|
||||
" timer_results.append(res.best)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"timer_results"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(\"Average time taken: {0:4.2f} ms\".format(10 ** 3 * np.mean(timer_results)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"container.stop()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"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.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
Двоичные данные
Keras_Tensorflow/220px-Lynx_lynx_poing.jpg
Двоичные данные
Keras_Tensorflow/220px-Lynx_lynx_poing.jpg
Двоичный файл не отображается.
До Ширина: | Высота: | Размер: 26 KiB |
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -1,28 +0,0 @@
|
|||
# Conda environment specification. The dependencies defined in this file will
|
||||
# be automatically provisioned for runs with userManagedDependencies=False.
|
||||
|
||||
# Details about the Conda environment file format:
|
||||
# https://conda.io/docs/user-guide/tasks/manage-environments.html#create-env-file-manually
|
||||
|
||||
name: project_environment
|
||||
dependencies:
|
||||
# The python interpreter version.
|
||||
# Currently Azure ML only supports 3.5.2 and later.
|
||||
- python=3.6
|
||||
|
||||
- pip:
|
||||
# Required packages for AzureML execution, history, and data preparation.
|
||||
- azureml-defaults
|
||||
- papermill==0.14.1
|
||||
- python-dotenv==0.9.0
|
||||
- Pillow==5.2.0
|
||||
- wget==3.2
|
||||
- aiohttp==3.3.2
|
||||
- toolz==0.9.0
|
||||
- tqdm==4.23.4
|
||||
- keras==2.2.0
|
||||
- scikit-learn
|
||||
- zlib==1.2.11
|
||||
- libgcc=5.2.0
|
||||
- tensorflow==1.8.0
|
||||
- tornado==4.5.3
|
|
@ -0,0 +1,120 @@
|
|||
import base64
|
||||
import json
|
||||
import urllib
|
||||
from io import BytesIO
|
||||
|
||||
import matplotlib.gridspec as gridspec
|
||||
import matplotlib.pyplot as plt
|
||||
import toolz
|
||||
from PIL import Image, ImageOps
|
||||
import random
|
||||
|
||||
def read_image_from(url):
|
||||
return toolz.pipe(url,
|
||||
urllib.request.urlopen,
|
||||
lambda x: x.read(),
|
||||
BytesIO)
|
||||
|
||||
|
||||
def to_rgb(img_bytes):
|
||||
return Image.open(img_bytes).convert('RGB')
|
||||
|
||||
|
||||
@toolz.curry
|
||||
def resize(img_file, new_size=(100, 100)):
|
||||
return ImageOps.fit(img_file, new_size, Image.ANTIALIAS)
|
||||
|
||||
|
||||
def to_base64(img):
|
||||
imgio = BytesIO()
|
||||
img.save(imgio, 'PNG')
|
||||
imgio.seek(0)
|
||||
dataimg = base64.b64encode(imgio.read())
|
||||
return dataimg.decode('utf-8')
|
||||
|
||||
|
||||
def to_img(img_url):
|
||||
return toolz.pipe(img_url,
|
||||
read_image_from,
|
||||
to_rgb,
|
||||
resize(new_size=(224,224)))
|
||||
|
||||
|
||||
def img_url_to_json(url, label='image'):
|
||||
img_data = toolz.pipe(url,
|
||||
to_img,
|
||||
to_base64)
|
||||
return json.dumps({'input':{label:'\"{0}\"'.format(img_data)}})
|
||||
|
||||
|
||||
def _plot_image(ax, img):
|
||||
ax.imshow(to_img(img))
|
||||
ax.tick_params(axis='both',
|
||||
which='both',
|
||||
bottom=False,
|
||||
top=False,
|
||||
left=False,
|
||||
right=False,
|
||||
labelleft=False,
|
||||
labelbottom=False)
|
||||
return ax
|
||||
|
||||
|
||||
def _plot_prediction_bar(ax, r):
|
||||
perf = list(c[1] for c in r.json()[0]['image'])
|
||||
ax.barh(range(3, 0, -1), perf, align='center', color='#55DD55')
|
||||
ax.tick_params(axis='both',
|
||||
which='both',
|
||||
bottom=False,
|
||||
top=False,
|
||||
left=False,
|
||||
right=False,
|
||||
labelbottom=False)
|
||||
tick_labels = reversed(list(' '.join(c[0].split()[1:]).split(',')[0] for c in r.json()[0]['image']))
|
||||
ax.yaxis.set_ticks([1,2,3])
|
||||
ax.yaxis.set_ticklabels(tick_labels, position=(0.5,0), minor=False, horizontalalignment='center')
|
||||
|
||||
|
||||
def plot_predictions(images, classification_results):
|
||||
if len(images)!=6:
|
||||
raise Exception('This method is only designed for 6 images')
|
||||
gs = gridspec.GridSpec(2, 3)
|
||||
fig = plt.figure(figsize=(12, 9))
|
||||
gs.update(hspace=0.1, wspace=0.001)
|
||||
|
||||
for gg,r, img in zip(gs, classification_results, images):
|
||||
gg2 = gridspec.GridSpecFromSubplotSpec(4, 10, subplot_spec=gg)
|
||||
ax = fig.add_subplot(gg2[0:3, :])
|
||||
_plot_image(ax, img)
|
||||
ax = fig.add_subplot(gg2[3, 1:9])
|
||||
_plot_prediction_bar(ax, r)
|
||||
|
||||
def write_json_to_file(json_dict, filename, mode='w'):
|
||||
with open(filename, mode) as outfile:
|
||||
json.dump(json_dict, outfile, indent=4,sort_keys=True)
|
||||
outfile.write('\n\n')
|
||||
|
||||
def gen_variations_of_one_image(IMAGEURL, num, label='image'):
|
||||
out_images = []
|
||||
img = to_img(IMAGEURL).convert('RGB')
|
||||
# Flip the colours for one-pixel
|
||||
# "Different Image"
|
||||
for i in range(num):
|
||||
diff_img = img.copy()
|
||||
rndm_pixel_x_y = (random.randint(0, diff_img.size[0]-1),
|
||||
random.randint(0, diff_img.size[1]-1))
|
||||
current_color = diff_img.getpixel(rndm_pixel_x_y)
|
||||
diff_img.putpixel(rndm_pixel_x_y, current_color[::-1])
|
||||
b64img = to_base64(diff_img)
|
||||
out_images.append(json.dumps({'input':{label:'\"{0}\"'.format(b64img)}}))
|
||||
return out_images
|
||||
|
||||
def wait_until_container_ready(somepredicate, timeout, period=0.25, *args, **kwargs):
|
||||
mustend = time.time() + timeout
|
||||
while time.time() < mustend:
|
||||
if somepredicate(*args, **kwargs): return True
|
||||
time.sleep(period)
|
||||
return False
|
||||
|
||||
def wait_until():
|
||||
pass
|
Загрузка…
Ссылка в новой задаче