|
| 1 | +# Copyright 2017-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +from __future__ import absolute_import |
| 14 | + |
| 15 | + |
| 16 | +import os |
| 17 | + |
| 18 | +import sagemaker |
| 19 | +import sagemaker.predictor |
| 20 | +import sagemaker.utils |
| 21 | +import tests.integ |
| 22 | +import tests.integ.timeout |
| 23 | +import numpy as np |
| 24 | +import matplotlib.image as mpimg |
| 25 | +from sagemaker.tensorflow.model import TensorFlowModel |
| 26 | +from tests.integ import ( |
| 27 | + DATA_DIR, |
| 28 | +) |
| 29 | +from tests.integ.timeout import timeout_and_delete_endpoint_by_name |
| 30 | + |
| 31 | +INPUT_MODEL = os.path.join(DATA_DIR, "tensorflow-serving-test-model.tar.gz") |
| 32 | +INFERENCE_IMAGE = os.path.join(DATA_DIR, "cuteCat.jpg") |
| 33 | + |
| 34 | + |
| 35 | +def test_compile_and_deploy_with_accelerator( |
| 36 | + sagemaker_session, |
| 37 | + tfs_eia_cpu_instance_type, |
| 38 | + tfs_eia_latest_version, |
| 39 | + tfs_eia_latest_py_version, |
| 40 | + tfs_eia_target_device, |
| 41 | + tfs_eia_compilation_job_name |
| 42 | +): |
| 43 | + endpoint_name = sagemaker.utils.unique_name_from_base("sagemaker-tensorflow-serving") |
| 44 | + model_data = sagemaker_session.upload_data( |
| 45 | + path=os.path.join(tests.integ.DATA_DIR, "tensorflow-serving-test-model.tar.gz"), |
| 46 | + key_prefix="tensorflow-serving/compiledmodels", |
| 47 | + ) |
| 48 | + bucket = sagemaker_session.default_bucket() |
| 49 | + with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): |
| 50 | + model = TensorFlowModel( |
| 51 | + model_data=model_data, |
| 52 | + role="SageMakerRole", |
| 53 | + framework_version=tfs_eia_latest_version, |
| 54 | + py_version=tfs_eia_latest_py_version, |
| 55 | + sagemaker_session=sagemaker_session, |
| 56 | + name=endpoint_name, |
| 57 | + ) |
| 58 | + data_shape = {"input": [1, 224, 224, 3]} |
| 59 | + compiled_model_path = "s3://{}/{}/output".format(bucket, tfs_eia_compilation_job_name) |
| 60 | + compiled_model = model.compile( |
| 61 | + target_instance_family=tfs_eia_target_device, |
| 62 | + input_shape=data_shape, |
| 63 | + output_path=compiled_model_path, |
| 64 | + role="SageMakerRole", |
| 65 | + job_name=tfs_eia_compilation_job_name, |
| 66 | + framework='tensorflow', |
| 67 | + framework_version=tfs_eia_latest_version |
| 68 | + ) |
| 69 | + predictor = compiled_model.deploy( |
| 70 | + 1, tfs_eia_cpu_instance_type, endpoint_name=endpoint_name, accelerator_type="ml.eia2.large" |
| 71 | + ) |
| 72 | + |
| 73 | + image_path = os.path.join(tests.integ.DATA_DIR, "cuteCat.jpg") |
| 74 | + img = mpimg.imread(image_path) |
| 75 | + img = np.resize(img, (224, 224, 3)) |
| 76 | + img = np.expand_dims(img, axis=0) |
| 77 | + input_data = {"inputs": img} |
| 78 | + result = predictor.predict(input_data) |
| 79 | + print("result", result) |
0 commit comments