|
| 1 | +###################################### |
| 2 | +Upgrade from Legacy TensorFlow Support |
| 3 | +###################################### |
| 4 | + |
| 5 | +With v2 of the SageMaker Python SDK, support for legacy SageMaker TensorFlow images has been deprecated. |
| 6 | +This guide explains how to upgrade your SageMaker Python SDK usage. |
| 7 | + |
| 8 | +For more information about using TensorFlow with the SageMaker Python SDK, see `Use TensorFlow with the SageMaker Python SDK <using_tf.html>`_. |
| 9 | + |
| 10 | +.. contents:: |
| 11 | + |
| 12 | +******************************************** |
| 13 | +What Constitutes "Legacy TensorFlow Support" |
| 14 | +******************************************** |
| 15 | + |
| 16 | +This guide is relevant if one of the following applies: |
| 17 | + |
| 18 | +#. You are using TensorFlow versions 1.4-1.10 |
| 19 | +#. You are using TensorFlow versions 1.11-1.12 with Python 2, and |
| 20 | + |
| 21 | + - you do *not* have ``script_mode=True`` when creating your estimator |
| 22 | + - you are using ``sagemaker.tensorflow.model.TensorFlowModel`` and/or ``sagemaker.tensorflow.model.TensorFlowPredictor`` |
| 23 | + |
| 24 | +#. You are using a pre-built SageMaker image whose URI looks like ``520713654638.dkr.ecr.<region>.amazonaws.com/sagemaker-tensorflow:<tag>`` |
| 25 | + |
| 26 | +If one of the above applies, then keep reading. |
| 27 | + |
| 28 | +************** |
| 29 | +How to Upgrade |
| 30 | +************** |
| 31 | + |
| 32 | +We recommend that you use the latest supported version of TensorFlow because that's where we focus our development efforts. |
| 33 | +For information about supported versions of TensorFlow, see the `AWS documentation <https://aws.amazon.com/releasenotes/available-deep-learning-containers-images>`_. |
| 34 | + |
| 35 | +For general information about using TensorFlow with the SageMaker Python SDK, see `Use TensorFlow with the SageMaker Python SDK <using_tf.html>`_. |
| 36 | + |
| 37 | +Training Script |
| 38 | +=============== |
| 39 | + |
| 40 | +Newer versions of TensorFlow require your training script to be runnable as a command-line script, similar to what you might run outside of SageMaker. For more information, including how to adapt a locally-runnable script, see `Prepare a Training Script <using_tf.html#id1>`_. |
| 41 | + |
| 42 | +In addition, your training script needs to save your model. If you have your own ``serving_input_fn`` implementation, then that can be passed to an exporter: |
| 43 | + |
| 44 | +.. code:: python |
| 45 | +
|
| 46 | + import tensorflow as tf |
| 47 | +
|
| 48 | + exporter = tf.estimator.LatestExporter("Servo", serving_input_receiver_fn=serving_input_fn) |
| 49 | +
|
| 50 | +For an example of how to repackage your legacy TensorFlow training script for use with a newer version of TensorFlow, |
| 51 | +see `this example notebook <https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/tensorflow_moving_from_framework_mode_to_script_mode/tensorflow_moving_from_framework_mode_to_script_mode.ipynb>`_. |
| 52 | + |
| 53 | +Inference Script |
| 54 | +================ |
| 55 | + |
| 56 | +Newer versions of TensorFlow Serving require a different format for the inference script. Some key differences: |
| 57 | + |
| 58 | +- The script must be named ``inference.py``. |
| 59 | +- ``input_fn`` has been replaced by ``input_handler``. |
| 60 | +- ``output_fn`` has been replaced by ``output_handler``. |
| 61 | + |
| 62 | +Like with the legacy versions, the pre-built SageMaker TensorFlow Serving images do have default implementations for pre- and post-processing. |
| 63 | + |
| 64 | +For more information about implementing your own handlers, see `How to implement the pre- and/or post-processing handler(s) <using_tf.html#how-to-implement-the-pre-and-or-post-processing-handler-s>`_. |
| 65 | + |
| 66 | +***************************** |
| 67 | +Continue with Legacy Versions |
| 68 | +***************************** |
| 69 | + |
| 70 | +While not recommended, you can still use a legacy TensorFlow version with v2 of the SageMaker Python SDK. |
| 71 | +In order to do so, you need to change how a few parameters are defined. |
| 72 | + |
| 73 | +Training |
| 74 | +======== |
| 75 | + |
| 76 | +When creating an estimator, v2 requires the following changes: |
| 77 | + |
| 78 | +#. Explicitly specify the ECR image URI via ``image_name``. |
| 79 | + To determine the URI, you can use :func:`sagemaker.fw_utils.create_image_uri`. |
| 80 | +#. Specify ``model_dir=False``. |
| 81 | +#. Use hyperparameters for ``training_steps``, ``evaluation_steps``, ``checkpoint_path``, and ``requirements_file``. |
| 82 | + |
| 83 | +For example, if using TF 1.10.0 with an ml.m4.xlarge instance in us-west-2, |
| 84 | +the difference in code would be as follows: |
| 85 | + |
| 86 | +.. code:: python |
| 87 | +
|
| 88 | + from sagemaker.tensorflow import TensorFlow |
| 89 | +
|
| 90 | + # v1 |
| 91 | + estimator = TensorFlow( |
| 92 | + ... |
| 93 | + source_dir="code", |
| 94 | + framework_version="1.10.0", |
| 95 | + train_instance_type="ml.m4.xlarge", |
| 96 | + training_steps=100, |
| 97 | + evaluation_steps=10, |
| 98 | + checkpoint_path="s3://bucket/path", |
| 99 | + requirements_file="requirements.txt", |
| 100 | + ) |
| 101 | +
|
| 102 | + # v2 |
| 103 | + estimator = TensorFlow( |
| 104 | + ... |
| 105 | + source_dir="code", |
| 106 | + framework_version="1.10.0", |
| 107 | + train_instance_type="ml.m4.xlarge", |
| 108 | + image_name="520713654638.dkr.ecr.us-west-2.amazonaws.com/sagemaker-tensorflow:1.10.0-cpu-py2", |
| 109 | + hyperparameters={ |
| 110 | + "training_steps": 100, |
| 111 | + "evaluation_steps": 10, |
| 112 | + "checkpoint_path": "s3://bucket/path", |
| 113 | + "sagemaker_requirements": "requirements.txt", |
| 114 | + }, |
| 115 | + model_dir=False, |
| 116 | + ) |
| 117 | +
|
| 118 | +Requirements File with Training |
| 119 | +------------------------------- |
| 120 | + |
| 121 | +To provide a requirements file, define a hyperparameter named "sagemaker_requirements" that contains the relative path to the requirements file from ``source_dir``. |
| 122 | + |
| 123 | +Inference |
| 124 | +========= |
| 125 | + |
| 126 | +Using a legacy TensorFlow version for endpoints and batch transform can be achieved with v2 of the SageMaker Python SDK with some minor changes to your code. |
| 127 | + |
| 128 | +From an Estimator |
| 129 | +----------------- |
| 130 | + |
| 131 | +If you are starting with a training job, you can call :func:`sagemaker.estimator.EstimatorBase.deploy` or :func:`sagemaker.tensorflow.estimator.Estimator.transformer` from your estimator for inference. |
| 132 | + |
| 133 | +To specify the number of model server workers, you need to set it through an environment variable named ``MODEL_SERVER_WORKERS``: |
| 134 | + |
| 135 | +.. code:: python |
| 136 | +
|
| 137 | + # v1 |
| 138 | + estimator.deploy(..., model_server_workers=4) |
| 139 | +
|
| 140 | + # v2 |
| 141 | + estimator.deploy(..., env={"MODEL_SERVER_WORKERS": 4}) |
| 142 | +
|
| 143 | +From a Model |
| 144 | +------------ |
| 145 | + |
| 146 | +If you are starting with a model, v2 requires the following changes: |
| 147 | + |
| 148 | +#. Use the the :class:`sagemaker.model.FrameworkModel` class. |
| 149 | +#. Explicitly specify the ECR image URI via ``image``. |
| 150 | + To determine the URI, you can use :func:`sagemaker.fw_utils.create_image_uri`. |
| 151 | +#. Use an environment variable for ``model_server_workers``. |
| 152 | + |
| 153 | +For example, if using TF 1.10.0 with a CPU instance in us-west-2, |
| 154 | +the difference in code would be as follows: |
| 155 | + |
| 156 | +.. code:: python |
| 157 | +
|
| 158 | + # v1 |
| 159 | + from sagemaker.tensorflow import TensorFlowModel |
| 160 | +
|
| 161 | + model = TensorFlowModel( |
| 162 | + ... |
| 163 | + py_version="py2", |
| 164 | + framework_version="1.10.0", |
| 165 | + model_server_workers=4, |
| 166 | + ) |
| 167 | +
|
| 168 | + # v2 |
| 169 | + from sagemaker.model import FrameworkModel |
| 170 | +
|
| 171 | + model = FrameworkModel( |
| 172 | + ... |
| 173 | + image="520713654638.dkr.ecr.us-west-2.amazonaws.com/sagemaker-tensorflow:1.10.0-cpu-py2", |
| 174 | + env={"MODEL_SERVER_WORKERS": 4}, |
| 175 | + ) |
| 176 | +
|
| 177 | +Requirements File with Inference |
| 178 | +-------------------------------- |
| 179 | + |
| 180 | +To provide a requirements file, define an environment variable named ``SAGEMAKER_REQUIREMENTS`` that contains the relative path to the requirements file from ``source_dir``. |
| 181 | + |
| 182 | +From an estimator: |
| 183 | + |
| 184 | +.. code:: python |
| 185 | +
|
| 186 | + # for an endpoint |
| 187 | + estimator.deploy(..., env={"SAGEMAKER_REQUIREMENTS": "requirements.txt"}) |
| 188 | +
|
| 189 | + # for batch transform |
| 190 | + estimator.transformer(..., env={"SAGEMAKER_REQUIREMENTS": "requirements.txt"}) |
| 191 | +
|
| 192 | +From a model: |
| 193 | + |
| 194 | +.. code:: python |
| 195 | +
|
| 196 | + from sagemaker.model import FrameworkModel |
| 197 | +
|
| 198 | + model = FrameworkModel( |
| 199 | + ... |
| 200 | + source_dir="code", |
| 201 | + env={"SAGEMAKER_REQUIREMENTS": "requirements.txt"}, |
| 202 | + ) |
| 203 | +
|
| 204 | +
|
| 205 | +Predictors |
| 206 | +---------- |
| 207 | + |
| 208 | +If you want to use your model for endpoints, then you can use the :class:`sagemaker.predictor.RealTimePredictor` class instead of the legacy ``sagemaker.tensorflow.TensorFlowPredictor`` class: |
| 209 | + |
| 210 | +.. code:: python |
| 211 | +
|
| 212 | + from sagemaker.model import FrameworkModel |
| 213 | + from sagemaker.predictor import RealTimePredictor |
| 214 | +
|
| 215 | + model = FrameworkModel( |
| 216 | + ... |
| 217 | + predictor_cls=RealTimePredictor, |
| 218 | + ) |
| 219 | +
|
| 220 | + predictor = model.deploy(...) |
| 221 | +
|
| 222 | +If you are using protobuf prediction data, then you need to serialize and deserialize the data yourself. |
| 223 | + |
| 224 | +For example: |
| 225 | + |
| 226 | +.. code:: python |
| 227 | +
|
| 228 | + from google.protobuf import json_format |
| 229 | + from protobuf_to_dict import protobuf_to_dict |
| 230 | + from tensorflow.core.framework import tensor_pb2 |
| 231 | +
|
| 232 | + # Serialize the prediction data |
| 233 | + json_format.MessageToJson(data) |
| 234 | +
|
| 235 | + # Get the prediction result |
| 236 | + result = predictor.predict(data) |
| 237 | +
|
| 238 | + # Deserialize the prediction result |
| 239 | + protobuf_to_dict(json_format.Parse(result, tensor_pb2.TensorProto())) |
| 240 | +
|
| 241 | +Otherwise, you can use the serializers and deserialzers available in the SageMaker Python SDK or write your own. |
| 242 | + |
| 243 | +For example, if you want to use JSON serialization and deserialization: |
| 244 | + |
| 245 | +.. code:: python |
| 246 | +
|
| 247 | + from sagemaker.predictor import json_deserializer, json_serializer |
| 248 | +
|
| 249 | + predictor.content_type = "application/json" |
| 250 | + predictor.serializer = json_serializer |
| 251 | + predictor.accept = "application/json" |
| 252 | + predictor.deserializer = json_deserializer |
| 253 | +
|
| 254 | + predictor.predict(data) |
0 commit comments