Skip to content

Commit 1a36199

Browse files
authored
Add markdown explanation for TorchVision Neo NB (#3295)
* Add markdown explanation for TorchVision Neo NB * Use present tense
1 parent 8f73d51 commit 1a36199

File tree

1 file changed

+71
-10
lines changed

1 file changed

+71
-10
lines changed

sagemaker_neo_compilation_jobs/pytorch_torchvision/pytorch_torchvision_neo.ipynb

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"source": [
14-
"Amazon SageMaker Neo is an API to compile machine learning models to optimize them for our choice of hardware targets. Currently, Neo supports pre-trained PyTorch models from [TorchVision](https://pytorch.org/docs/stable/torchvision/models.html). General support for other PyTorch models is forthcoming."
14+
"Amazon SageMaker Neo is an API to compile machine learning models to optimize them for our choice of hardware targets. Currently, Neo supports pre-trained PyTorch models from [TorchVision](https://pytorch.org/docs/stable/torchvision/models.html). General support for other PyTorch models is forthcoming.\n",
15+
"\n",
16+
"### Runtime\n",
17+
"\n",
18+
"This notebook takes approximately 8 minutes to run.\n",
19+
"\n",
20+
"### Contents\n",
21+
"\n",
22+
"1. [Import ResNet18 from TorchVision](#Import-ResNet18-from-TorchVision)\n",
23+
"1. [Invoke Neo Compilation API](#Invoke-Neo-Compilation-API)\n",
24+
"1. [Deploy the model](#Deploy-the-model)\n",
25+
"1. [Send requests](#Send-requests)\n",
26+
"1. [Delete the Endpoint](#Delete-the-Endpoint)"
1527
]
1628
},
1729
{
@@ -25,7 +37,7 @@
2537
"cell_type": "markdown",
2638
"metadata": {},
2739
"source": [
28-
"We'll import [ResNet18](https://arxiv.org/abs/1512.03385) model from TorchVision and create a model artifact `model.tar.gz`."
40+
"We import the [ResNet18](https://arxiv.org/abs/1512.03385) model from TorchVision and create a model artifact `model.tar.gz`."
2941
]
3042
},
3143
{
@@ -41,6 +53,13 @@
4153
"!{sys.executable} -m pip install --upgrade sagemaker"
4254
]
4355
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {},
59+
"source": [
60+
"Specify the input data shape. For more information, see [Prepare Model for Compilation](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-compilation-preparing-model.html)."
61+
]
62+
},
4463
{
4564
"cell_type": "code",
4665
"execution_count": null,
@@ -68,6 +87,13 @@
6887
"### Upload the model archive to S3"
6988
]
7089
},
90+
{
91+
"cell_type": "markdown",
92+
"metadata": {},
93+
"source": [
94+
"Specify parameters for the compilation job and upload the `model.tar.gz` archive file."
95+
]
96+
},
7197
{
7298
"cell_type": "code",
7399
"execution_count": null,
@@ -110,6 +136,13 @@
110136
"### Create a PyTorch SageMaker model"
111137
]
112138
},
139+
{
140+
"cell_type": "markdown",
141+
"metadata": {},
142+
"source": [
143+
"Use the `PyTorchModel` and define parameters including the path to the model, the `entry_point` script that is used to perform inference, and other version and environment variables."
144+
]
145+
},
113146
{
114147
"cell_type": "code",
115148
"execution_count": null,
@@ -139,6 +172,13 @@
139172
"### Use Neo compiler to compile the model"
140173
]
141174
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"Run the compilation job, which is saved in S3 at the specified `compiled_model_path` location."
180+
]
181+
},
142182
{
143183
"cell_type": "code",
144184
"execution_count": null,
@@ -163,6 +203,13 @@
163203
"## Deploy the model"
164204
]
165205
},
206+
{
207+
"cell_type": "markdown",
208+
"metadata": {},
209+
"source": [
210+
"Deploy the compiled model to an endpoint so it can be used for inference."
211+
]
212+
},
166213
{
167214
"cell_type": "code",
168215
"execution_count": null,
@@ -183,11 +230,18 @@
183230
"cell_type": "markdown",
184231
"metadata": {},
185232
"source": [
186-
"Let's try to send a cat picture.\n",
233+
"Let's send a picture to the endpoint to predict the image subject.\n",
187234
"\n",
188235
"![title](cat.jpg)"
189236
]
190237
},
238+
{
239+
"cell_type": "markdown",
240+
"metadata": {},
241+
"source": [
242+
"Open the image and pass the payload as a bytearray to the predictor, receiving a response."
243+
]
244+
},
191245
{
192246
"cell_type": "code",
193247
"execution_count": null,
@@ -206,6 +260,13 @@
206260
"print(\"Most likely class: {}\".format(np.argmax(result)))"
207261
]
208262
},
263+
{
264+
"cell_type": "markdown",
265+
"metadata": {},
266+
"source": [
267+
"Use the ImageNet class ID response to look up which subject the image contains, and with what probability."
268+
]
269+
},
209270
{
210271
"cell_type": "code",
211272
"execution_count": null,
@@ -217,12 +278,12 @@
217278
"with open(\"imagenet1000_clsidx_to_labels.txt\", \"r\") as f:\n",
218279
" for line in f:\n",
219280
" key, val = line.strip().split(\":\")\n",
220-
" object_categories[key] = val\n",
281+
" object_categories[key] = val.strip(\" \").strip(\",\")\n",
221282
"print(\n",
222-
" \"Result: label - \"\n",
223-
" + object_categories[str(np.argmax(result))]\n",
224-
" + \" probability - \"\n",
225-
" + str(np.amax(result))\n",
283+
" \"The label is\",\n",
284+
" object_categories[str(np.argmax(result))],\n",
285+
" \"with probability\",\n",
286+
" str(np.amax(result))[:5],\n",
226287
")"
227288
]
228289
},
@@ -231,7 +292,7 @@
231292
"metadata": {},
232293
"source": [
233294
"## Delete the Endpoint\n",
234-
"Having an endpoint running will incur some costs. Therefore as a clean-up job, we should delete the endpoint."
295+
"Delete the endpoint to avoid incurring costs now that it is no longer needed."
235296
]
236297
},
237298
{
@@ -261,7 +322,7 @@
261322
"name": "python",
262323
"nbconvert_exporter": "python",
263324
"pygments_lexer": "ipython3",
264-
"version": "3.6.10"
325+
"version": "3.6.13"
265326
}
266327
},
267328
"nbformat": 4,

0 commit comments

Comments
 (0)