Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 0552fc6

Browse files
committed
Added iPython Notebooks.
1 parent 6999839 commit 0552fc6

8 files changed

+2668
-0
lines changed
Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"Copyright (c) Microsoft Corporation. All rights reserved."
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Overview"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"These notebooks are designed to be used to train, validate, and containerize a MaskRCNN neural network that can detect voids on our test dataset. The model in these notebooks can be used on a CPU or GPU."
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"## Introduction\n",
29+
"\n",
30+
"This notebook configures your library of notebooks to connect to an Azure Machine Learning (ML) workspace. In this case, a library contains all of the notebooks in the current folder and any nested folders. You can configure this notebook library to use an existing workspace or create a new workspace.\n",
31+
"\n",
32+
"Typically you will need to run this notebook only once per notebook library as all other notebooks will use connection information that is written here. If you want to redirect your notebook library to work with a different workspace, then you should re-run this notebook.\n",
33+
"\n",
34+
"In this notebook you will\n",
35+
"* Learn about getting an Azure subscription\n",
36+
"* Specify your workspace parameters\n",
37+
"* Access or create your workspace\n",
38+
"* Add a default compute cluster for your workspace\n",
39+
"\n",
40+
"**These notebooks are designed to be run in an Azure ML Notebook VM.**\n",
41+
"\n",
42+
"### What is an Azure Machine Learning workspace\n",
43+
"\n",
44+
"An Azure ML Workspace is an Azure resource that organizes and coordinates the actions of many other Azure resources to assist in executing and sharing machine learning workflows. In particular, an Azure ML Workspace coordinates storage, databases, and compute resources providing added functionality for machine learning experimentation, deployment, inferencing, and the monitoring of deployed models."
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"## Setup\n",
52+
"\n",
53+
"This section describes activities required before you can access any Azure ML services functionality."
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {},
59+
"source": [
60+
"### 1. Azure Subscription\n",
61+
"\n",
62+
"In order to create an Azure ML Workspace, first you need access to an Azure subscription. An Azure subscription allows you to manage storage, compute, and other assets in the Azure cloud. You can [create a new subscription](https://azure.microsoft.com/en-us/free/) or access existing subscription information from the [Azure portal](https://portal.azure.com). Later in this notebook you will need information such as your subscription ID in order to create and access AML workspaces.\n",
63+
"\n",
64+
"### 2. Azure ML SDK and other library installation\n",
65+
"\n",
66+
"If you are running in your own environment, follow [SDK installation instructions](https://docs.microsoft.com/azure/machine-learning/service/how-to-configure-environment). If you are running in Azure Notebooks or another Microsoft managed environment, the SDK is already installed.\n",
67+
"\n",
68+
"Also install following libraries to your environment. Many of the example notebooks depend on them\n",
69+
"\n",
70+
"```\n",
71+
"(myenv) $ conda install -y matplotlib tqdm scikit-learn\n",
72+
"```\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {},
79+
"outputs": [],
80+
"source": [
81+
"import sys\n",
82+
"import os\n",
83+
"!pip install tensorflow"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"Once installation is complete, the following cell checks the Azure ML SDK version:"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 2,
96+
"metadata": {
97+
"tags": [
98+
"install"
99+
]
100+
},
101+
"outputs": [
102+
{
103+
"name": "stdout",
104+
"output_type": "stream",
105+
"text": [
106+
"This notebook was created using version 1.0.23 of the Azure ML SDK\n",
107+
"You are currently using version 1.0.53 of the Azure ML SDK\n"
108+
]
109+
}
110+
],
111+
"source": [
112+
"import azureml.core\n",
113+
"from azureml.core import Workspace\n",
114+
"from dotenv import set_key, get_key, find_dotenv\n",
115+
"from pathlib import Path\n",
116+
"\n",
117+
"print(\"This notebook was created using version 1.0.23 of the Azure ML SDK\")\n",
118+
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
119+
]
120+
},
121+
{
122+
"cell_type": "markdown",
123+
"metadata": {},
124+
"source": [
125+
"If you are using an older version of the SDK then this notebook was created using, you should upgrade your SDK.\n",
126+
"\n",
127+
"### 3. Azure Container Instance registration\n",
128+
"Azure Machine Learning uses of [Azure Container Instance (ACI)](https://azure.microsoft.com/services/container-instances) to deploy dev/test web services. An Azure subscription needs to be registered to use ACI. If you or the subscription owner have not yet registered ACI on your subscription, you will need to use the [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest) and execute the following commands. Note that if you ran through the AML [quickstart](https://docs.microsoft.com/en-us/azure/machine-learning/service/quickstart-get-started) you have already registered ACI. \n",
129+
"\n",
130+
"```shell\n",
131+
"# check to see if ACI is already registered\n",
132+
"(myenv) $ az provider show -n Microsoft.ContainerInstance -o table\n",
133+
"\n",
134+
"# if ACI is not registered, run this command.\n",
135+
"# note you need to be the subscription owner in order to execute this command successfully.\n",
136+
"(myenv) $ az provider register -n Microsoft.ContainerInstance\n",
137+
"```\n",
138+
"\n",
139+
"---"
140+
]
141+
},
142+
{
143+
"cell_type": "markdown",
144+
"metadata": {},
145+
"source": [
146+
"## Configure your Azure ML workspace\n",
147+
"\n",
148+
"### Workspace parameters\n",
149+
"\n",
150+
"To use an AML Workspace, you will need to import the Azure ML SDK and supply the following information:\n",
151+
"* Your subscription id\n",
152+
"* A resource group name\n",
153+
"* (optional) The region that will host your workspace\n",
154+
"* A name for your workspace\n",
155+
"\n",
156+
"You can get your subscription ID from the [Azure portal](https://portal.azure.com).\n",
157+
"\n",
158+
"You will also need access to a [_resource group_](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-overview#resource-groups), which organizes Azure resources and provides a default region for the resources in a group. You can see what resource groups to which you have access, or create a new one in the [Azure portal](https://portal.azure.com). If you don't have a resource group, the create workspace command will create one for you using the name you provide.\n",
159+
"\n",
160+
"The region to host your workspace will be used if you are creating a new workspace. You do not need to specify this if you are using an existing workspace. You can find the list of supported regions [here](https://azure.microsoft.com/en-us/global-infrastructure/services/?products=machine-learning-service). You should pick a region that is close to your location or that contains your data.\n",
161+
"\n",
162+
"The name for your workspace is unique within the subscription and should be descriptive enough to discern among other AML Workspaces. The subscription may be used only by you, or it may be used by your department or your entire enterprise, so choose a name that makes sense for your situation.\n",
163+
"\n",
164+
"The following cell allows you to specify your workspace parameters. This cell uses the python method `os.getenv` to read values from environment variables which is useful for automation. If no environment variable exists, the parameters will be set to the specified default values. \n",
165+
"\n",
166+
"If you ran the Azure Machine Learning [quickstart](https://docs.microsoft.com/en-us/azure/machine-learning/service/quickstart-get-started) in Azure Notebooks, you already have a configured workspace! You can go to your Azure Machine Learning Getting Started library, view *config.json* file, and copy-paste the values for subscription ID, resource group and workspace name below.\n",
167+
"\n",
168+
"Replace the default values in the cell below with your workspace parameters"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": 3,
174+
"metadata": {},
175+
"outputs": [],
176+
"source": [
177+
"subscription_id = \"\"\n",
178+
"resource_group = \"\"\n",
179+
"workspace_name = \"\"\n",
180+
"workspace_region = \"westus2\""
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 4,
186+
"metadata": {},
187+
"outputs": [],
188+
"source": [
189+
"env_path = find_dotenv()\n",
190+
"if env_path == \"\":\n",
191+
" Path(\".env\").touch()\n",
192+
" env_path = find_dotenv()"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": 5,
198+
"metadata": {},
199+
"outputs": [
200+
{
201+
"data": {
202+
"text/plain": [
203+
"(True, 'workspace_region', 'westus2')"
204+
]
205+
},
206+
"execution_count": 5,
207+
"metadata": {},
208+
"output_type": "execute_result"
209+
}
210+
],
211+
"source": [
212+
"set_key(env_path, \"subscription_id\", subscription_id)\n",
213+
"set_key(env_path, \"resource_group\", resource_group)\n",
214+
"set_key(env_path, \"workspace_name\", workspace_name)\n",
215+
"set_key(env_path, \"workspace_region\", workspace_region)"
216+
]
217+
},
218+
{
219+
"cell_type": "markdown",
220+
"metadata": {},
221+
"source": [
222+
"### Access your workspace\n",
223+
"\n",
224+
"The following cell uses the Azure ML SDK to attempt to load the workspace specified by your parameters. If this cell succeeds, your notebook library will be configured to access the workspace from all notebooks using the `Workspace.from_config()` method. The cell can fail if the specified workspace doesn't exist or you don't have permissions to access it. "
225+
]
226+
},
227+
{
228+
"cell_type": "code",
229+
"execution_count": 11,
230+
"metadata": {},
231+
"outputs": [
232+
{
233+
"name": "stdout",
234+
"output_type": "stream",
235+
"text": [
236+
"Workspace configuration succeeded. Skip the workspace creation steps below\n"
237+
]
238+
}
239+
],
240+
"source": [
241+
"ws = None\n",
242+
"try:\n",
243+
" ws = Workspace(subscription_id = subscription_id, resource_group = resource_group, workspace_name = workspace_name)\n",
244+
" # write the details of the workspace to a configuration file to the notebook library\n",
245+
" ws.write_config()\n",
246+
" print(\"Workspace configuration succeeded. Skip the workspace creation steps below\")\n",
247+
"except:\n",
248+
" print(\"Workspace not accessible. Change your parameters or create a new workspace below\")"
249+
]
250+
},
251+
{
252+
"cell_type": "markdown",
253+
"metadata": {},
254+
"source": [
255+
"### Create a new workspace\n",
256+
"\n",
257+
"If you don't have an existing workspace and are the owner of the subscription or resource group, you can create a new workspace. If you don't have a resource group, the create workspace command will create one for you using the name you provide.\n",
258+
"\n",
259+
"**Note**: As with other Azure services, there are limits on certain resources (for example AmlCompute quota) associated with the Azure ML service. Please read [this article](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-manage-quotas) on the default limits and how to request more quota.\n",
260+
"\n",
261+
"This cell will create an Azure ML workspace for you in a subscription provided you have the correct permissions.\n",
262+
"\n",
263+
"This will fail if:\n",
264+
"* You do not have permission to create a workspace in the resource group\n",
265+
"* You do not have permission to create a resource group if it's non-existing.\n",
266+
"* You are not a subscription owner or contributor and no Azure ML workspaces have ever been created in this subscription\n",
267+
"\n",
268+
"If workspace creation fails, please work with your IT admin to provide you with the appropriate permissions or to provision the required resources."
269+
]
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": 12,
274+
"metadata": {
275+
"tags": [
276+
"create workspace"
277+
]
278+
},
279+
"outputs": [],
280+
"source": [
281+
"# Create the workspace using the specified parameters\n",
282+
"ws = Workspace.create(name = workspace_name,\n",
283+
" subscription_id = subscription_id,\n",
284+
" resource_group = resource_group, \n",
285+
" location = workspace_region,\n",
286+
" create_resource_group = True,\n",
287+
" exist_ok = True)\n",
288+
"ws.get_details()\n",
289+
"\n",
290+
"# write the details of the workspace to a configuration file to the notebook library\n",
291+
"ws.write_config()"
292+
]
293+
},
294+
{
295+
"cell_type": "markdown",
296+
"metadata": {},
297+
"source": [
298+
"You can now proceed to next notebook to develop the model driver for the web service."
299+
]
300+
}
301+
],
302+
"metadata": {
303+
"authors": [
304+
{
305+
"name": "roastala"
306+
}
307+
],
308+
"kernelspec": {
309+
"display_name": "Python 3.6 - AzureML",
310+
"language": "python",
311+
"name": "python3-azureml"
312+
},
313+
"language_info": {
314+
"codemirror_mode": {
315+
"name": "ipython",
316+
"version": 3
317+
},
318+
"file_extension": ".py",
319+
"mimetype": "text/x-python",
320+
"name": "python",
321+
"nbconvert_exporter": "python",
322+
"pygments_lexer": "ipython3",
323+
"version": "3.6.2"
324+
}
325+
},
326+
"nbformat": 4,
327+
"nbformat_minor": 2
328+
}

0 commit comments

Comments
 (0)