|
| 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 | +import tarfile |
| 16 | + |
| 17 | +import botocore.exceptions |
| 18 | +import os |
| 19 | + |
| 20 | +import pytest |
| 21 | +import sagemaker |
| 22 | +import sagemaker.predictor |
| 23 | +import sagemaker.utils |
| 24 | +import tests.integ |
| 25 | +import tests.integ.timeout |
| 26 | +import numpy as np |
| 27 | +import matplotlib.image as mpimg |
| 28 | +from sagemaker.deserializers import JSONDeserializer |
| 29 | +from sagemaker.tensorflow.model import TensorFlowModel, TensorFlowPredictor |
| 30 | +from sagemaker.serializers import CSVSerializer, IdentitySerializer |
| 31 | +from tests.integ import ( |
| 32 | + DATA_DIR, |
| 33 | +) |
| 34 | +from tests.integ.timeout import timeout_and_delete_endpoint_by_name |
| 35 | + |
| 36 | +INPUT_MODEL = os.path.join(DATA_DIR, "tensorflow-serving-test-model.tar.gz") |
| 37 | +INFERENCE_IMAGE = os.path.join(DATA_DIR, "cuteCat.jpg") |
| 38 | + |
| 39 | + |
| 40 | +def test_compile_and_deploy_with_accelerator( |
| 41 | + sagemaker_session, |
| 42 | + tfs_eia_cpu_instance_type, |
| 43 | + tfs_eia_latest_version, |
| 44 | + tfs_eia_latest_py_version, |
| 45 | + tfs_eia_target_device, |
| 46 | + tfs_eia_compilation_job_name |
| 47 | +): |
| 48 | + endpoint_name = sagemaker.utils.unique_name_from_base("sagemaker-tensorflow-serving") |
| 49 | + model_data = sagemaker_session.upload_data( |
| 50 | + path=os.path.join(tests.integ.DATA_DIR, "tensorflow-serving-test-model.tar.gz"), |
| 51 | + key_prefix="tensorflow-serving/compiledmodels", |
| 52 | + ) |
| 53 | + bucket = sagemaker_session.default_bucket() |
| 54 | + with tests.integ.timeout.timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session): |
| 55 | + model = TensorFlowModel( |
| 56 | + model_data=model_data, |
| 57 | + role="SageMakerRole", |
| 58 | + framework_version=tfs_eia_latest_version, |
| 59 | + py_version=tfs_eia_latest_py_version |
| 60 | + sagemaker_session=sagemaker_session, |
| 61 | + name=endpoint_name, |
| 62 | + ) |
| 63 | + data_shape = {"input": [1, 224, 224, 3]} |
| 64 | + compiled_model_path = "s3://{}/{}/output".format(bucket, tfs_eia_compilation_job_name) |
| 65 | + compiled_model = model.compile( |
| 66 | + target_instance_family=tfs_eia_target_device, |
| 67 | + input_shape=data_shape, |
| 68 | + output_path=compiled_model_path, |
| 69 | + role="SageMakerRole", |
| 70 | + job_name=tfs_eia_compilation_job_name, |
| 71 | + framework='tensorflow', |
| 72 | + framework_version=tfs_eia_latest_version |
| 73 | + ) |
| 74 | + predictor = compiled_model.deploy( |
| 75 | + 1, tfs_eia_cpu_instance_type, endpoint_name=endpoint_name, accelerator_type="ml.eia2.large" |
| 76 | + ) |
| 77 | + |
| 78 | + image_path = os.path.join(tests.integ.DATA_DIR, "cuteCat.jpg") |
| 79 | + img = mpimg.imread(image_path) |
| 80 | + img = np.resize(img, (224, 224, 3)) |
| 81 | + img = np.expand_dims(img, axis=0) |
| 82 | + input_data = {"inputs": img} |
| 83 | + result = predictor.predict(input_data) |
0 commit comments