|
11 | 11 | "cell_type": "markdown",
|
12 | 12 | "metadata": {},
|
13 | 13 | "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)" |
15 | 27 | ]
|
16 | 28 | },
|
17 | 29 | {
|
|
25 | 37 | "cell_type": "markdown",
|
26 | 38 | "metadata": {},
|
27 | 39 | "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`." |
29 | 41 | ]
|
30 | 42 | },
|
31 | 43 | {
|
|
41 | 53 | "!{sys.executable} -m pip install --upgrade sagemaker"
|
42 | 54 | ]
|
43 | 55 | },
|
| 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 | + }, |
44 | 63 | {
|
45 | 64 | "cell_type": "code",
|
46 | 65 | "execution_count": null,
|
|
68 | 87 | "### Upload the model archive to S3"
|
69 | 88 | ]
|
70 | 89 | },
|
| 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 | + }, |
71 | 97 | {
|
72 | 98 | "cell_type": "code",
|
73 | 99 | "execution_count": null,
|
|
110 | 136 | "### Create a PyTorch SageMaker model"
|
111 | 137 | ]
|
112 | 138 | },
|
| 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 | + }, |
113 | 146 | {
|
114 | 147 | "cell_type": "code",
|
115 | 148 | "execution_count": null,
|
|
139 | 172 | "### Use Neo compiler to compile the model"
|
140 | 173 | ]
|
141 | 174 | },
|
| 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 | + }, |
142 | 182 | {
|
143 | 183 | "cell_type": "code",
|
144 | 184 | "execution_count": null,
|
|
163 | 203 | "## Deploy the model"
|
164 | 204 | ]
|
165 | 205 | },
|
| 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 | + }, |
166 | 213 | {
|
167 | 214 | "cell_type": "code",
|
168 | 215 | "execution_count": null,
|
|
183 | 230 | "cell_type": "markdown",
|
184 | 231 | "metadata": {},
|
185 | 232 | "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", |
187 | 234 | "\n",
|
188 | 235 | ""
|
189 | 236 | ]
|
190 | 237 | },
|
| 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 | + }, |
191 | 245 | {
|
192 | 246 | "cell_type": "code",
|
193 | 247 | "execution_count": null,
|
|
206 | 260 | "print(\"Most likely class: {}\".format(np.argmax(result)))"
|
207 | 261 | ]
|
208 | 262 | },
|
| 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 | + }, |
209 | 270 | {
|
210 | 271 | "cell_type": "code",
|
211 | 272 | "execution_count": null,
|
|
217 | 278 | "with open(\"imagenet1000_clsidx_to_labels.txt\", \"r\") as f:\n",
|
218 | 279 | " for line in f:\n",
|
219 | 280 | " key, val = line.strip().split(\":\")\n",
|
220 |
| - " object_categories[key] = val\n", |
| 281 | + " object_categories[key] = val.strip(\" \").strip(\",\")\n", |
221 | 282 | "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", |
226 | 287 | ")"
|
227 | 288 | ]
|
228 | 289 | },
|
|
231 | 292 | "metadata": {},
|
232 | 293 | "source": [
|
233 | 294 | "## 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." |
235 | 296 | ]
|
236 | 297 | },
|
237 | 298 | {
|
|
261 | 322 | "name": "python",
|
262 | 323 | "nbconvert_exporter": "python",
|
263 | 324 | "pygments_lexer": "ipython3",
|
264 |
| - "version": "3.6.10" |
| 325 | + "version": "3.6.13" |
265 | 326 | }
|
266 | 327 | },
|
267 | 328 | "nbformat": 4,
|
|
0 commit comments