|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# Using Amazon Elastic Inference with a pre-trained TensorFlow model on SageMaker\n", |
| 7 | + "# Using Amazon Elastic Inference with a pre-trained TensorFlow Serving model on SageMaker\n", |
8 | 8 | "\n",
|
9 |
| - "This notebook demonstrates how to enable and use Amazon Elastic Inference with our predefined SageMaker TensorFlow containers.\n", |
| 9 | + "This notebook demonstrates how to enable and use Amazon Elastic Inference with our predefined SageMaker TensorFlow Serving containers.\n", |
10 | 10 | "\n",
|
11 | 11 | "Amazon Elastic Inference (EI) is a resource you can attach to your Amazon EC2 instances to accelerate your deep learning (DL) inference workloads. EI allows you to add inference acceleration to an Amazon SageMaker hosted endpoint or Jupyter notebook for a fraction of the cost of using a full GPU instance. For more information please visit: https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html\n",
|
12 | 12 | "\n",
|
13 |
| - "This notebook's main objective is to show how to create an endpoint, backed by an Elastic Inference, to serve our pre-trained TensorFlow model for predictions. With a more efficient cost per performance, Amazon Elastic Inference can prove to be useful for those looking to use GPUs for higher inference performance at a lower cost.\n", |
| 13 | + "This notebook's main objective is to show how to create an endpoint, backed by an Elastic Inference, to serve our pre-trained TensorFlow Serving model for predictions. With a more efficient cost per performance, Amazon Elastic Inference can prove to be useful for those looking to use GPUs for higher inference performance at a lower cost.\n", |
14 | 14 | "\n",
|
15 | 15 | "1. [The model](#The-model)\n",
|
16 | 16 | "1. [Setup role for SageMaker](#Setup-role-for-SageMaker)\n",
|
17 |
| - "1. [Load the TensorFlow Model on Amazon SageMaker using Python SDK](#Load-the-TensorFlow-Model-on-Amazon-SageMaker-using-Python-SDK)\n", |
| 17 | + "1. [Load the TensorFlow Serving Model on Amazon SageMaker using Python SDK](#Load-the-TensorFlow-Serving-Model-on-Amazon-SageMaker-using-Python-SDK)\n", |
18 | 18 | "1. [Deploy the trained Model to an Endpoint with EI](#Deploy-the-trained-Model-to-an-Endpoint-with-EI)\n",
|
19 | 19 | " 1. [Using EI with a SageMaker notebook instance](#Using-EI-with-a-SageMaker-notebook-instance)\n",
|
20 | 20 | " 1. [Invoke the Endpoint to get inferences](#Invoke-the-Endpoint-to-get-inferences)\n",
|
|
38 | 38 | "\n",
|
39 | 39 | "The pre-trained model we will be using for this example is a NCHW ResNet-50 model from the [official Tensorflow model Github repository](https://github.com/tensorflow/models/tree/master/official/resnet#pre-trained-model). For more information in regards to deep residual networks, please check [here](https://github.com/tensorflow/models/tree/master/official/resnet). It isn't a requirement to train our model on SageMaker to use SageMaker for serving our model.\n",
|
40 | 40 | "\n",
|
41 |
| - "SageMaker expects our models to be compressed in a tar.gz format in S3. Thankfully, our model already comes in that format. The predefined SageMaker TensorFlow containers utilize TensorFlow serving for loading and handling inferences, so it is expecting a SavedModel. The [predefined SageMaker Tensorflow](https://github.com/aws/sagemaker-tensorflow-container/blob/master/src/tf_container/serve.py#L108) container expects the base folder name for the saved model to be `export/Servo` followed by a verison and your SavedModel artifacts.\n", |
| 41 | + "SageMaker expects our models to be compressed in a tar.gz format in S3. Thankfully, our model already comes in that format. The predefined TensorFlow Serving containers use REST API for handling inferences, for more informationm, please see [Deploying to TensorFlow Serving Endpoints](https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/tensorflow/deploying_tensorflow_serving.rst#making-predictions-against-a-sagemaker-endpoint).\n", |
42 | 42 | "\n",
|
43 | 43 | "To host our model for inferences in SageMaker, we need to first upload the SavedModel to S3. This can be done through the AWS console or AWS command line.\n",
|
44 | 44 | "\n",
|
|
85 | 85 | "cell_type": "markdown",
|
86 | 86 | "metadata": {},
|
87 | 87 | "source": [
|
88 |
| - "## Load the TensorFlow Model on Amazon SageMaker using Python SDK\n", |
| 88 | + "## Load the TensorFlow Serving Model on Amazon SageMaker using Python SDK\n", |
89 | 89 | "\n",
|
90 |
| - "We can use the SageMaker Python SDK to load our pre-trained TensorFlow model for hosting in SageMaker for predictions.\n", |
| 90 | + "We can use the SageMaker Python SDK to load our pre-trained TensorFlow Serving model for hosting in SageMaker for predictions.\n", |
91 | 91 | "\n",
|
92 |
| - "There are a few parameters that our TensorFlowModel is expecting.\n", |
| 92 | + "There are a few parameters that our TensorFlow Serving Model is expecting.\n", |
93 | 93 | "1. `model_data` - The S3 location of a model tar.gz file to load in SageMaker\n",
|
94 |
| - "2. `entry_point` - Path (absolute or relative) to the Python source file which should be executed as the entry point to model hosting. If a blank file is provided, default configurations will be used for model hosting.\n", |
95 |
| - "3. `role` - An IAM role name or ARN for SageMaker to access AWS resources on your behalf.\n", |
96 |
| - "4. `framework_version` - TensorFlow serving version you want to use for handling your inference request .\n" |
| 94 | + "2. `role` - An IAM role name or ARN for SageMaker to access AWS resources on your behalf.\n", |
| 95 | + "3. `framework_version` - TensorFlow Serving version you want to use for handling your inference request .\n" |
97 | 96 | ]
|
98 | 97 | },
|
99 | 98 | {
|
|
102 | 101 | "metadata": {},
|
103 | 102 | "outputs": [],
|
104 | 103 | "source": [
|
105 |
| - "from sagemaker.tensorflow import TensorFlowModel\n", |
| 104 | + "from sagemaker.tensorflow.serving import Model\n", |
106 | 105 | "\n",
|
107 |
| - "tensorflow_model = TensorFlowModel(model_data=saved_model,\n", |
108 |
| - " entry_point='entry.py',\n", |
109 |
| - " role=role,\n", |
110 |
| - " framework_version='1.12')" |
| 106 | + "tensorflow_model = Model(model_data=saved_model,\n", |
| 107 | + " role=role,\n", |
| 108 | + " framework_version='1.12')" |
111 | 109 | ]
|
112 | 110 | },
|
113 | 111 | {
|
|
118 | 116 | "\n",
|
119 | 117 | "The `deploy()` method creates an endpoint which serves prediction requests in real-time.\n",
|
120 | 118 | "\n",
|
121 |
| - "The only change required for utilizing EI with our SageMaker TensorFlow containers only requires providing an `accelerator_type` parameter, which determines which type of EI accelerator to attach to your endpoint. The supported types of accelerators can be found here: https://aws.amazon.com/sagemaker/pricing/instance-types/\n", |
122 |
| - "\n", |
123 |
| - "No code changes are necessary for your model, as our predefined TensorFlow containers utilizes TensorFlow serving, which has been modified to utilize EI for inference, as long as an EI accelerator is attached to the endpoint." |
| 119 | + "The only change required for utilizing EI with our SageMaker TensorFlow Serving containers only requires providing an `accelerator_type` parameter, which determines which type of EI accelerator to attach to your endpoint. The supported types of accelerators can be found here: https://aws.amazon.com/sagemaker/pricing/instance-types/\n" |
124 | 120 | ]
|
125 | 121 | },
|
126 | 122 | {
|
|
167 | 163 | "import numpy as np\n",
|
168 | 164 | "random_input = np.random.rand(1, 1, 3, 3)\n",
|
169 | 165 | "\n",
|
170 |
| - "prediction = predictor.predict({'input': random_input.tolist()})\n", |
| 166 | + "prediction = predictor.predict({'inputs': random_input.tolist()})\n", |
171 | 167 | "\n",
|
172 | 168 | "print(prediction)"
|
173 | 169 | ]
|
|
0 commit comments