deploy-MLmodels-on-iotedge/object-detection-azureml/033_BuildImageForMLModule.i...

233 строки
6.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Build Image\n",
"\n",
"In this notebook, we show:\n",
"\n",
"- Create/Register a Docker image in ACR using AzureML\n"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"import docker\n",
"#import numpy as np\n",
"import requests\n",
"from azure.mgmt.containerregistry import ContainerRegistryManagementClient\n",
"from azureml._model_management._util import (get_docker_client, pull_docker_image)\n",
"from azureml.core.conda_dependencies import CondaDependencies\n",
"from azureml.core.image import ContainerImage\n",
"from dotenv import find_dotenv, get_key, set_key\n",
"from testing_utilities import to_img, plot_predictions, get_auth, wait_until_ready\n"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"env_path = find_dotenv(raise_error_if_not_found=True)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"resource_group = get_key(env_path, 'resource_group')\n",
"model_name = 'maskrcnn_resnet50_model'\n",
"image_name = get_key(env_path, 'image_name')"
]
},
{
"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(auth=get_auth())\n",
"print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep = '\\n')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create Image"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"# create yml file to be used in the image\n",
"conda_pack = []\n",
"requirements = [\"torch==1.1.0\",\"torchvision==0.3\",\"Pillow==5.2.0\", \"azureml-defaults\", \"azureml-contrib-services\", \"toolz==0.9.0\"]\n",
"\n",
"imgenv = CondaDependencies.create(conda_packages=conda_pack,pip_packages=requirements)\n",
"with open(\"img_env.yml\", \"w\") as f:\n",
" f.write(imgenv.serialize_to_string())"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [],
"source": [
"\n",
"image_config = ContainerImage.image_configuration(execution_script = \"driver.py\",\n",
" runtime = \"python\",\n",
" conda_file = \"img_env.yml\",\n",
" description = \"Image for torchvision MaskRCNN ResNet 50 Model\",\n",
" tags = {\"name\":\"object detection\",\"project\":\"AzureML\"}, \n",
" #dependencies = [\"resnet152.py\"],\n",
" enable_gpu = True\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Creating image\n",
"Running..........................................................................................\n",
"Succeeded\n",
"Image creation operation finished for image imgformlmodel:7, operation \"Succeeded\"\n"
]
}
],
"source": [
"# create image. It may take upto 15-20 minutes. \n",
"image = ContainerImage.create(name = image_name,\n",
" # this is the model object\n",
" models = [ws.models[model_name]], \n",
" image_config = image_config,\n",
" workspace = ws)\n",
"\n",
"image.wait_for_creation(show_output = True)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"# You can find the logs of image creation\n",
"# image.image_build_log_uri\n",
"\n",
"# You can get the image object when not creating a new image\n",
"# image = ws.images['image1']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Getting your container details\n",
"ws = Workspace.from_config(auth=get_auth())\n",
"container_reg = ws.get_details()[\"containerRegistry\"]\n",
"reg_name=container_reg.split(\"/\")[-1]\n",
"container_url = \"\\\"\" + image.image_location + \"\\\",\"\n",
"subscription_id = ws.subscription_id\n",
"\n",
"client = ContainerRegistryManagementClient(ws._auth,subscription_id)\n",
"result= client.registries.list_credentials(resource_group, reg_name, custom_headers=None, raw=False)\n",
"username = result.username\n",
"password = result.passwords[0].value\n",
"print('ContainerURL:{}'.format(image.image_location))\n",
"print('Servername: {}'.format(reg_name))\n",
"print('Username: {}'.format(username))\n",
"print('Password: {}'.format(password))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"acr_name = reg_name # azure container registry name. e.g. arc_name = \"myacr\"\n",
"acr_login_server = '{}.azurecr.io'.format(acr_name)\n",
"set_key(env_path,\"acr_name\", acr_name)\n",
"set_key(env_path,\"acr_login_server\", acr_login_server)\n",
"set_key(env_path,\"acr_password\", password)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#img_model_location = '{}/{}:1'.format(acr_login_server, image_name)\n",
"ml_img_location = image.image_location\n",
"set_key(env_path,\"ml_img_location\", ml_img_location)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we will proceed with notebook [04_DeployOnIOTedge.ipynb](04_DeployOnIOTedge.ipynb.ipynb)."
]
}
],
"metadata": {
"celltoolbar": "Tags",
"jupytext": {
"formats": "ipynb"
},
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}