Skip to content

Add batch transform test and remove extra MNIST images from test resources #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dist
test/resources/local_mode_lock
.idea/*
*~
.DS_Store
2 changes: 1 addition & 1 deletion test/integration/local/test_gluon_hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_gluon_hosting(docker_image, sagemaker_local_session, local_instance_typ
image=docker_image,
sagemaker_session=sagemaker_local_session)

with open(os.path.join(RESOURCE_PATH, 'mnist_images', '04.json'), 'r') as f:
with open(os.path.join(RESOURCE_PATH, 'mnist', 'images', '04.json'), 'r') as f:
input = json.load(f)

with local_mode_utils.lock():
Expand Down
66 changes: 66 additions & 0 deletions test/integration/sagemaker/test_batch_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2019 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://www.apache.org/licenses/LICENSE-2.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.
from __future__ import absolute_import

import json
import os
from urllib.parse import urlparse

from sagemaker import utils
from sagemaker.mxnet.model import MXNetModel

from test.integration import RESOURCE_PATH
import timeout

SCRIPT_PATH = os.path.join(RESOURCE_PATH, 'default_handlers', 'model', 'code', 'empty_module.py')
MNIST_PATH = os.path.join(RESOURCE_PATH, 'mnist')
MODEL_PATH = os.path.join(MNIST_PATH, 'model', 'model.tar.gz')

DATA_FILE = '07.csv'
DATA_PATH = os.path.join(MNIST_PATH, 'images', DATA_FILE)


def test_batch_transform(sagemaker_session, ecr_image, instance_type, framework_version):
s3_prefix = 'mxnet-serving/mnist'
model_data = sagemaker_session.upload_data(path=MODEL_PATH, key_prefix=s3_prefix)
model = MXNetModel(model_data,
'SageMakerRole',
SCRIPT_PATH,
image=ecr_image,
framework_version=framework_version,
sagemaker_session=sagemaker_session)

transformer = model.transformer(1, instance_type)
with timeout.timeout_and_delete_model_with_transformer(transformer, sagemaker_session, minutes=20):
input_data = sagemaker_session.upload_data(path=DATA_PATH, key_prefix=s3_prefix)

job_name = utils.unique_name_from_base('test-mxnet-serving-batch')
transformer.transform(input_data, content_type='text/csv', job_name=job_name)
transformer.wait()

prediction = _transform_result(sagemaker_session.boto_session, transformer.output_path)
assert prediction == 7


def _transform_result(boto_session, output_path):
s3 = boto_session.resource('s3', region_name=boto_session.region_name)

parsed_url = urlparse(output_path)
bucket_name = parsed_url.netloc
prefix = parsed_url.path[1:]

output_obj = s3.Object(bucket_name, '{}/{}.out'.format(prefix, DATA_FILE))
output = output_obj.get()['Body'].read().decode('utf-8')

probabilities = json.loads(output)[0]
return probabilities.index(max(probabilities))
11 changes: 11 additions & 0 deletions test/resources/mnist/images/07.csv

Large diffs are not rendered by default.

Binary file added test/resources/mnist/model/model.tar.gz
Binary file not shown.
1 change: 0 additions & 1 deletion test/resources/mnist_images/00.json

This file was deleted.

1 change: 0 additions & 1 deletion test/resources/mnist_images/01.json

This file was deleted.

1 change: 0 additions & 1 deletion test/resources/mnist_images/02.json

This file was deleted.

Loading