deploy-MLmodels-on-iotedge/object-detection-azureml/031_DevAndRegisterModel.ipynb

257 строки
5.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Develop and Register Model\n",
"In this noteook, we will go through the steps to load the MaskRCNN model and call the model to find the top predictions. We will then register the model in ACR using AzureML.\n",
"\n",
" Note: Always make sure you don't have any lingering notebooks running (Shutdown previous notebooks). Otherwise it may cause GPU memory issue.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%reload_ext autoreload\n",
"%autoreload 2\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torchvision\n",
"import numpy as np\n",
"from pathlib import *\n",
"from PIL import Image\n",
"from azureml.core.workspace import Workspace\n",
"from azureml.core.model import Model\n",
"from dotenv import set_key, find_dotenv\n",
"from testing_utilities import get_auth\n",
"import urllib"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"env_path = find_dotenv(raise_error_if_not_found=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Model\n",
"\n",
"We load a pretrained [**Mask R-CNN ResNet-50 FPN** object detection model](https://pytorch.org/blog/torchvision03/). This model is trained on subset of COCO train2017, which contains the same 20 categories as those from Pascal VOC."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# use pretrained model: https://pytorch.org/blog/torchvision03/\n",
"model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)\n",
"\n",
"#device = torch.device(\"cpu\")\n",
"device = torch.device(\"cuda\") if torch.cuda.is_available() else torch.device(\"cpu\")\n",
"model.to(device)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('maskrcnn_resnet50.pth', <http.client.HTTPMessage at 0x7f1553819048>)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"url = \"https://download.pytorch.org/models/maskrcnn_resnet50_fpn_coco-bf2d0c1e.pth\"\n",
"urllib.request.urlretrieve(url, \"maskrcnn_resnet50.pth\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1920, 1080)\n"
]
}
],
"source": [
"img_path = \"./test_image.jpg\"\n",
"print(Image.open(img_path).size)\n",
"img = Image.open(img_path)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"img = np.array(img.convert(mode='RGB'), dtype = np.float32) \n",
"img_tensor = torchvision.transforms.functional.to_tensor(img)/255"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.eval()\n",
"with torch.no_grad():\n",
" prediction = model([img_tensor.to(device)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(prediction)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Register Model\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get workspace\n",
"# Load existing workspace from the config file info.\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": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Registering model maskrcnn_resnet50_model\n"
]
}
],
"source": [
"\n",
"model = Model.register(\n",
" model_path=\"maskrcnn_resnet50.pth\", # this points to a local file\n",
" model_name=\"maskrcnn_resnet50_model\", # this is the name the model is registered as\n",
" tags={\"model\": \"dl\", \"framework\": \"maskrcnn\"},\n",
" description=\"torchvision maskrcnn_resnet50\",\n",
" workspace=ws,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"maskrcnn_resnet50_model torchvision maskrcnn_resnet50 1\n"
]
}
],
"source": [
"print(model.name, model.description, model.version)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, 'model_version', '1')"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"set_key(env_path, \"model_version\", str(model.version))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we will proceed with notebook [032_DevelopModelDriver.ipynb](032_DevelopModelDriver.ipynb)."
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}