Skip to content

Commit ef7a41a

Browse files
Xingyu Zhouyuanzhua
authored andcommitted
Adding new Neo folder and Region check (aws#810)
* Add Neo folder and move Neo notebook under it * Update README * Update mxnet_mnist_neo.ipynb with Evidence * add compile section in mnist notebook for mxnet and tensorflow * clean serializer function in neo tensorflow mnist * Add Neo Region Check for tensorflow_mnist and mxnet_mnist
1 parent bb0a5fc commit ef7a41a

File tree

20 files changed

+785
-38
lines changed

20 files changed

+785
-38
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ These examples that showcase unique functionality available in Amazon SageMaker.
109109
- [Inference Pipeline with SparkML and BlazingText](advanced_functionality/inference_pipeline_sparkml_blazingtext_dbpedia) shows how to deploy an Inference Pipeline with SparkML for data pre-processing and BlazingText for training on the DBPedia dataset. The pre-processing code is written once and used between training and inference.
110110
- [Experiment Management Capabilities with Search](advanced_functionality/search) shows how to organize Training Jobs into projects, and track relationships between Models, Endpoints, and Training Jobs.
111111

112+
### Amazon SageMaker Neo Compilation Jobs
113+
114+
These examples provide you an introduction to how to use Neo to optimizes deep learning model
115+
116+
- [Image Classification](sagemaker_neo_compilation_jobs/imageclassification_caltech) Adapts form [image classification](introduction_to_amazon_algorithms/imageclassification_caltech) including Neo API and comparsion between the baseline
117+
- [MNIST with MXNet](sagemaker_neo_compilation_jobs/mxnet_mnist) Adapts form [mxnet mnist](sagemaker-python-sdk/mxnet_mnist) including Neo API and comparsion between the baseline
118+
- [Deploying pre-trained PyTorch vision models](sagemaker_neo_compilation_jobs/pytorch_torchvision) shows how to use Amazon SageMaker Neo to compile and optimize pre-trained PyTorch models from TorchVision.
119+
- [Distributed TensorFlow](sagemaker_neo_compilation_jobs/tensorflow_distributed_mnist) Adapts form [tensorflow mnist](sagemaker-python-sdk/tensorflow_distributed_mnist) including Neo API and comparsion between the baseline
120+
- [Predicting Customer Churn](sagemaker_neo_compilation_jobs/xgboost_customer_churn) Adapts form [xgboost customer churn](introduction_to_applying_machine_learning/xgboost_customer_churn) including Neo API and comparsion between the baseline
112121

113122
### Amazon SageMaker Pre-Built Framework Containers and the Python SDK
114123

advanced_functionality/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ These examples that showcase unique functionality available in Amazon SageMaker.
1919
- [Inference Pipeline with SparkML and BlazingText](inference_pipeline_sparkml_blazingtext_dbpedia) shows how to deploy an Inference Pipeline with SparkML for data pre-processing and BlazingText for training on the DBPedia dataset. The pre-processing code is written once and used between training and inference.
2020
- [Creating Algorithm and Model Package - Listing on AWS Marketplace](creating_marketplace_products) provides a detailed walkthrough on how to package a scikit learn algorithm to create SageMaker Algorithm and SageMaker Model Package entities that can be used with the enhanced SageMaker Train/Transform/Hosting/Tuning APIs and listed on AWS Marketplace.
2121
- [Using Algorithm and Model Packages - From AWS Marketplace](using_marketplace_products) provides a detailed walkthrough on how to use Algorithm and Model Package entities with the enhanced SageMaker Train/Transform/Hosting/Tuning APIs by choosing a canonical product listed on AWS Marketplace.
22-
- [Deploying pre-trained PyTorch vision models with Amazon SageMaker Neo](pytorch_torchvision_neo) shows how to use Amazon SageMaker Neo to compile and optimize pre-trained PyTorch models from TorchVision.

introduction_to_amazon_algorithms/imageclassification_caltech/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ This notebook `Imageclassification-lst-format.ipynb` demos an end-2-end system f
1010
### SageMaker Image classification full training highlevel
1111
This notebook `ImageClassification-fulltraining-highlevel.ipynb` is similar to the `ImageClassification-fulltraining.ipynb` but using Sagemaker high-level APIs
1212

13-
### SageMaker Image classification full training highlevel with Neo
14-
This notebook `ImageClassification-fulltraining-highlevel-neo.ipynb` is similar to the `ImageClassification-fulltraining.ipynb` but using Sagemaker high-level APIs(including Neo)
15-
1613
### SageMaker Image classification transfer learning highlevel
1714
This notebook `Imageclassification-transfer-learning-highlevel.ipynb` is similar to the `ImageClassification-transfer-learning.ipynb` but using Sagemaker high-level APIs
1815

sagemaker-python-sdk/mxnet_mnist/mxnet_mnist.ipynb

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,35 @@
128128
"mnist_estimator.fit({'train': train_data_location, 'test': test_data_location})"
129129
]
130130
},
131+
{
132+
"cell_type": "markdown",
133+
"metadata": {},
134+
"source": [
135+
"### Opimtize your model with Neo API\n",
136+
"Neo API allows to optimize our model for a specific hardware type. When calling compile_model() function, we specify the target instance family (C5) as well as the S3 bucket to which the compiled model would be stored.\n",
137+
"\n",
138+
"#### Important. If the following command result in a permission error, scroll up and locate the value of execution role returned by get_execution_role(). The role must have access to the S3 bucket specified in output_path."
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [],
146+
"source": [
147+
"neo_optimize = False\n",
148+
"compiled_model = mnist_estimator\n",
149+
"if mnist_estimator.create_model().check_neo_region(boto3.Session().region_name) is False:\n",
150+
" print('Neo is not currently supported in', boto3.Session().region_name)\n",
151+
"else:\n",
152+
" output_path = '/'.join(mnist_estimator.output_path.split('/')[:-1])\n",
153+
" neo_optimize = True\n",
154+
" compiled_model = mnist_estimator.compile_model(target_instance_family='ml_m4', \n",
155+
" input_shape={'data':[1, 784]},\n",
156+
" role=role,\n",
157+
" output_path=output_path)"
158+
]
159+
},
131160
{
132161
"cell_type": "markdown",
133162
"metadata": {},
@@ -147,10 +176,29 @@
147176
"source": [
148177
"%%time\n",
149178
"\n",
150-
"predictor = mnist_estimator.deploy(initial_instance_count=1,\n",
179+
"predictor = compiled_model.deploy(initial_instance_count=1,\n",
151180
" instance_type='ml.m4.xlarge')"
152181
]
153182
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": null,
186+
"metadata": {},
187+
"outputs": [],
188+
"source": [
189+
"import io\n",
190+
"import numpy as np\n",
191+
"def numpy_bytes_serializer(data):\n",
192+
" f = io.BytesIO()\n",
193+
" np.save(f, data)\n",
194+
" f.seek(0)\n",
195+
" return f.read()\n",
196+
"\n",
197+
"if neo_optimize is True:\n",
198+
" predictor.content_type = 'application/vnd+python.numpy+binary'\n",
199+
" predictor.serializer = numpy_bytes_serializer"
200+
]
201+
},
154202
{
155203
"cell_type": "markdown",
156204
"metadata": {},
@@ -193,9 +241,11 @@
193241
"source": [
194242
"response = predictor.predict(data)\n",
195243
"print('Raw prediction result:')\n",
244+
"if neo_optimize is False:\n",
245+
" response = response[0]\n",
196246
"print(response)\n",
197247
"\n",
198-
"labeled_predictions = list(zip(range(10), response[0]))\n",
248+
"labeled_predictions = list(zip(range(10), response))\n",
199249
"print('Labeled predictions: ')\n",
200250
"print(labeled_predictions)\n",
201251
"\n",

sagemaker-python-sdk/tensorflow_distributed_mnist/mnist.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,15 @@ def _input_fn(training_dir, training_filename, batch_size=100):
123123
def neo_preprocess(payload, content_type):
124124
import logging
125125
import numpy as np
126-
import PIL.Image # Training container doesn't have this package
127126
import io
128127

129128
logging.info('Invoking user-defined pre-processing function')
130129

131-
if content_type != 'application/x-image':
132-
raise RuntimeError('Content type must be application/x-image')
130+
if content_type != 'application/x-image' and content_type != 'application/vnd+python.numpy+binary':
131+
raise RuntimeError('Content type must be application/x-image or application/vnd+python.numpy+binary')
133132

134133
f = io.BytesIO(payload)
135-
# Load image and convert to greyscale space
136-
image = PIL.Image.open(f).convert('L')
137-
# Resize
138-
image = np.asarray(image.resize((28, 28)))
139-
# Reshape
140-
image = image.reshape((1,-1)).astype('float32')
134+
image = np.load(f)*255
141135

142136
return image
143137

sagemaker-python-sdk/tensorflow_distributed_mnist/tensorflow_distributed_mnist.ipynb

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,35 @@
167167
"In the end of the training, the training job will generate a saved model for TF serving."
168168
]
169169
},
170+
{
171+
"cell_type": "markdown",
172+
"metadata": {},
173+
"source": [
174+
"### Compiling the model\n",
175+
"The input_shape is the definition for the model's input tensor and output_path is where the compiled model will be stored in S3.\n",
176+
"#### Important. If the following command result in a permission error, scroll up and locate the value of execution role returned by get_execution_role(). The role must have access to the S3 bucket specified in output_path."
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"metadata": {},
183+
"outputs": [],
184+
"source": [
185+
"import boto3\n",
186+
"neo_optimize = False\n",
187+
"optimized_estimator = mnist_estimator\n",
188+
"if mnist_estimator.create_model().check_neo_region(boto3.Session().region_name) is False:\n",
189+
" print('Neo is not currently supported in', boto3.Session().region_name)\n",
190+
"else:\n",
191+
" output_path = '/'.join(mnist_estimator.output_path.split('/')[:-1])\n",
192+
" neo_optimize = True\n",
193+
" optimized_estimator = mnist_estimator.compile_model(target_instance_family='ml_m4', \n",
194+
" input_shape={'data':[1, 784]}, # Batch size 1, 3 channels, 224x224 Images.\n",
195+
" output_path=output_path,\n",
196+
" framework='tensorflow', framework_version='1.11.0')"
197+
]
198+
},
170199
{
171200
"cell_type": "markdown",
172201
"metadata": {
@@ -186,10 +215,26 @@
186215
},
187216
"outputs": [],
188217
"source": [
189-
"mnist_predictor = mnist_estimator.deploy(initial_instance_count=1,\n",
218+
"mnist_predictor = optimized_estimator.deploy(initial_instance_count=1,\n",
190219
" instance_type='ml.m4.xlarge')"
191220
]
192221
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": null,
225+
"metadata": {},
226+
"outputs": [],
227+
"source": [
228+
"def numpy_bytes_serializer(data):\n",
229+
" f = io.BytesIO()\n",
230+
" np.save(f, data)\n",
231+
" f.seek(0)\n",
232+
" return f.read()\n",
233+
"if neo_optimize is True:\n",
234+
" mnist_predictor.content_type = 'application/vnd+python.numpy+binary'\n",
235+
" mnist_predictor.serializer = numpy_bytes_serializer"
236+
]
237+
},
193238
{
194239
"cell_type": "markdown",
195240
"metadata": {},
@@ -205,20 +250,25 @@
205250
},
206251
"outputs": [],
207252
"source": [
253+
"import io\n",
208254
"import numpy as np\n",
209255
"from tensorflow.examples.tutorials.mnist import input_data\n",
210256
"\n",
211257
"mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)\n",
212258
"\n",
213259
"for i in range(10):\n",
214-
" data = mnist.test.images[i].tolist()\n",
215-
" tensor_proto = tf.make_tensor_proto(values=np.asarray(data), shape=[1, len(data)], dtype=tf.float32)\n",
216-
" predict_response = mnist_predictor.predict(tensor_proto)\n",
260+
" data = mnist.test.images[i]\n",
261+
" if neo_optimize is False:\n",
262+
" data = data.tolist()\n",
263+
" data = tf.make_tensor_proto(values=np.asarray(data), shape=[1, len(data)], dtype=tf.float32)\n",
264+
" predict_response = mnist_predictor.predict(data)\n",
217265
" \n",
218266
" print(\"========================================\")\n",
219267
" label = np.argmax(mnist.test.labels[i])\n",
220268
" print(\"label is {}\".format(label))\n",
221-
" prediction = predict_response['outputs']['classes']['int64_val'][0]\n",
269+
" prediction = np.argmax(predict_response)\n",
270+
" if neo_optimize is False:\n",
271+
" prediction = predict_response['outputs']['classes']['int64_val'][0]\n",
222272
" print(\"prediction is {}\".format(prediction))"
223273
]
224274
},
@@ -242,7 +292,6 @@
242292
}
243293
],
244294
"metadata": {
245-
"notice": "Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.",
246295
"kernelspec": {
247296
"display_name": "Environment (conda_tensorflow_p27)",
248297
"language": "python",
@@ -259,7 +308,8 @@
259308
"nbconvert_exporter": "python",
260309
"pygments_lexer": "ipython3",
261310
"version": "2.7.13"
262-
}
311+
},
312+
"notice": "Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License."
263313
},
264314
"nbformat": 4,
265315
"nbformat_minor": 2
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Amazon SageMaker Examples
2+
3+
### Amazon SageMaker Neo Compilation Jobs
4+
5+
6+
These examples focus on the Amazon SageMaker Neo which allows you to compile models and host in pre-built containers.
7+
8+
- [Image Classification](imageclassification_caltech)
9+
- [MNIST with MXNet](mxnet_mnist)
10+
- [Deploying pre-trained PyTorch vision models](pytorch_torchvision)
11+
- [Distributed TensorFlow](tensorflow_distributed_mnist)
12+
- [Predicting Customer Churn](xgboost_customer_churn)

introduction_to_amazon_algorithms/imageclassification_caltech/Image-classification-fulltraining-highlevel-neo.ipynb renamed to sagemaker_neo_compilation_jobs/imageclassification_caltech/Image-classification-fulltraining-highlevel-neo.ipynb

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"\n",
2121
"Welcome to our model optimization example for image classification.\n",
2222
"\n",
23-
"In this demo, we will use the Amazon sagemaker image classification algorithm to train on the [caltech-256 dataset](http://www.vision.caltech.edu/Image_Datasets/Caltech256/). \n",
23+
"In this demo, we will use the Amazon sagemaker image classification algorithm to train on the [caltech-256 dataset](http://www.vision.caltech.edu/Image_Datasets/Caltech256/) and then we will demonstrate Amazon Sagemaker Neo's ability to optimize models.\n",
24+
"\n",
2425
"\n",
2526
"To get started, we need to set up the environment with a few prerequisite steps, for permissions, configurations, and so on."
2627
]
@@ -359,14 +360,25 @@
359360
"\n",
360361
"***\n",
361362
"\n",
362-
"Now we will test the trained model without any specific optimization for the hardware."
363+
"We will use Sagemaker Neo to optimize the model."
363364
]
364365
},
365366
{
366367
"cell_type": "markdown",
367368
"metadata": {},
368369
"source": [
369-
"## Optimize the model specifically for the architecture\n",
370+
"## Introduction to SageMaker Neo\n",
371+
"\n",
372+
"***\n",
373+
"\n",
374+
"[Amazon SageMaker Neo](https://aws.amazon.com/sagemaker/neo/) optimizes models to run up to fourth as fast, with less than a tenth of the memory footprint, with no loss in accuracy. You start with a machine learning model built using MXNet, TensorFlow, PyTorch, or XGBoost and trained using Amazon SageMaker. Then you choose your target hardware platform from Intel, NVIDIA, or ARM. With a single click, SageMaker Neo will then compile the trained model into an executable. In this example, we will use the model we just trained and see how well the optimized model could perform."
375+
]
376+
},
377+
{
378+
"cell_type": "markdown",
379+
"metadata": {},
380+
"source": [
381+
"## Optimize the model specifically for the architecture using Neo API\n",
370382
"Now we will compare the same model, but compiled specifically for the architecture we're deploying on."
371383
]
372384
},
@@ -391,10 +403,7 @@
391403
"metadata": {},
392404
"outputs": [],
393405
"source": [
394-
"# There is a known issue where SageMaker SDK locates the incorrect docker image URI for Image Classification\n",
395-
"# For now, we manually set Image URI\n",
396406
"optimized_ic.image = get_image_uri(sess.boto_region_name, 'image-classification-neo', repo_version=\"latest\")\n",
397-
"# There is a known issue where SageMaker SDK does not set the same. In the mean time we set the name\n",
398407
"optimized_ic.name = 'deployed-image-classification'"
399408
]
400409
},
@@ -462,6 +471,16 @@
462471
"print(\"Result: label - \" + object_categories[index] + \", probability - \" + str(result[index]))"
463472
]
464473
},
474+
{
475+
"cell_type": "markdown",
476+
"metadata": {},
477+
"source": [
478+
"## Conclusion\n",
479+
"---\n",
480+
"As you can notice the inference time using our neo-optimized model is better than the original one. SageMaker Neo automatically optimizes machine learning models to perform at up to fourth the speed with no loss in accuracy. In the diagram below shows you how our neo-optimized model performs 3x better with ResNet 152 in C5.9xlarge instance. The originl model stands for the uncompiled model deployed on Flask container on May 10th, 2019 and neo-optimized model stands for the compiled model deployed on Neo-AI-DLR container. The data for each trial is the average of 1000 trys for each endpoint.\n",
481+
"![alt text](image-classification-latency.png \"Title\")\n"
482+
]
483+
},
465484
{
466485
"cell_type": "markdown",
467486
"metadata": {},
15.4 KB
Loading

0 commit comments

Comments
 (0)