Пример #1
0
def test_mnist_training_and_serving(docker_image, sagemaker_local_session,
                                    local_instance_type, framework_version,
                                    tmpdir):
    mx = MXNet(entry_point=SCRIPT_PATH,
               role='SageMakerRole',
               train_instance_count=1,
               train_instance_type=local_instance_type,
               sagemaker_session=sagemaker_local_session,
               image_name=docker_image,
               framework_version=framework_version,
               output_path='file://{}'.format(tmpdir))

    _train_and_assert_success(mx, str(tmpdir))

    with local_mode_utils.lock():
        try:
            model = mx.create_model(
                model_server_workers=NUM_MODEL_SERVER_WORKERS)
            predictor = _csv_predictor(model, local_instance_type)
            data = numpy.zeros(shape=(1, 1, 28, 28))
            prediction = predictor.predict(data)
        finally:
            mx.delete_endpoint()

    # Check that there is a probability for each possible class in the prediction
    prediction_values = prediction.decode('utf-8').split(',')
    assert len(prediction_values) == 10
def predictor(docker_image, sagemaker_local_session, local_instance_type):
    default_handler_path = os.path.join(RESOURCE_PATH, 'default_handlers')
    m = MXNetModel('file://{}'.format(os.path.join(default_handler_path, 'model')), 'SageMakerRole',
                   os.path.join(default_handler_path, 'code', 'empty_module.py'),
                   image=docker_image, sagemaker_session=sagemaker_local_session,
                   model_server_workers=NUM_MODEL_SERVER_WORKERS)
    with local_mode_utils.lock():
        try:
            predictor = m.deploy(1, local_instance_type)
            yield predictor
        finally:
            sagemaker_local_session.delete_endpoint(m.endpoint_name)
Пример #3
0
def predictor(docker_image, sagemaker_local_session, local_instance_type):
    model = MXNetModel('file://{}'.format(MODEL_PATH),
                       'SageMakerRole',
                       SCRIPT_PATH,
                       image=docker_image,
                       sagemaker_session=sagemaker_local_session)

    with local_mode_utils.lock():
        try:
            predictor = model.deploy(1, local_instance_type)
            yield predictor
        finally:
            predictor.delete_endpoint()
def test_hosting(docker_image, sagemaker_local_session, local_instance_type):
    model = MXNetModel('file://{}'.format(MODEL_PATH),
                       'SageMakerRole',
                       SCRIPT_PATH,
                       image=docker_image,
                       sagemaker_session=sagemaker_local_session)

    input = json.dumps({'some': 'json'})

    with local_mode_utils.lock():
        try:
            predictor = model.deploy(1, local_instance_type)
            output = predictor.predict(input)
            assert input == output
        finally:
            sagemaker_local_session.delete_endpoint(model.endpoint_name)
def test_hosting(docker_image, sagemaker_local_session, local_instance_type):
    hosting_resource_path = os.path.join(RESOURCE_PATH, 'dummy_hosting')
    m = MXNetModel('file://{}'.format(os.path.join(hosting_resource_path, 'code')), 'SageMakerRole',
                   os.path.join(hosting_resource_path, 'code', 'dummy_hosting_module.py'),
                   image=docker_image, sagemaker_session=sagemaker_local_session,
                   model_server_workers=NUM_MODEL_SERVER_WORKERS)

    input = json.dumps({'some': 'json'})

    with local_mode_utils.lock():
        try:
            predictor = m.deploy(1, local_instance_type)
            output = predictor.predict(input)
            assert input == output
        finally:
            sagemaker_local_session.delete_endpoint(m.endpoint_name)
def test_gluon_hosting(docker_image, sagemaker_local_session, local_instance_type):
    model = MXNetModel('file://{}'.format(MODEL_PATH),
                       'SageMakerRole',
                       SCRIPT_PATH,
                       image=docker_image,
                       sagemaker_session=sagemaker_local_session)

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

    with local_mode_utils.lock():
        try:
            predictor = model.deploy(1, local_instance_type)
            output = predictor.predict(input)
            assert [4.0] == output
        finally:
            predictor.delete_endpoint()
def test_onnx_import(docker_image, sagemaker_local_session, local_instance_type):
    model_path = 'file://{}'.format(os.path.join(ONNX_PATH, 'onnx_model'))
    m = MXNetModel(model_path, 'SageMakerRole', SCRIPT_PATH, image=docker_image,
                   sagemaker_session=sagemaker_local_session,
                   model_server_workers=NUM_MODEL_SERVER_WORKERS)

    input = numpy.zeros(shape=(1, 1, 28, 28))

    with local_mode_utils.lock():
        try:
            predictor = m.deploy(1, local_instance_type)
            output = predictor.predict(input)
        finally:
            sagemaker_local_session.delete_endpoint(m.endpoint_name)

    # Check that there is a probability for each possible class in the prediction
    assert len(output[0]) == 10
Пример #8
0
def test_onnx_import(docker_image, sagemaker_local_session,
                     local_instance_type):
    model = MXNetModel('file://{}'.format(MODEL_PATH),
                       'SageMakerRole',
                       SCRIPT_PATH,
                       image=docker_image,
                       sagemaker_session=sagemaker_local_session)

    input = numpy.zeros(shape=(1, 1, 28, 28))

    with local_mode_utils.lock():
        try:
            predictor = model.deploy(1, local_instance_type)
            output = predictor.predict(input)
        finally:
            predictor.delete_endpoint()

    # Check that there is a probability for each possible class in the prediction
    assert len(output[0]) == 10
Пример #9
0
def test_hosting(docker_image, sagemaker_local_session, local_instance_type):
    model = MXNetModel('file://{}'.format(MODEL_PATH),
                       'SageMakerRole',
                       SCRIPT_PATH,
                       image=docker_image,
                       sagemaker_session=sagemaker_local_session)

    with local_mode_utils.lock():
        try:
            predictor = model.deploy(1, local_instance_type)
            predictor.serializer = None
            predictor.deserializer = StringDeserializer()
            predictor.accept = None
            predictor.content_type = None

            input = 'some data'
            output = predictor.predict(input)
            assert input == output
        finally:
            predictor.delete_endpoint()
Пример #10
0
def test_gluon_hosting(docker_image, sagemaker_local_session,
                       local_instance_type):
    gluon_path = os.path.join(RESOURCE_PATH, 'gluon_hosting')
    m = MXNetModel('file://{}'.format(os.path.join(gluon_path, 'model')),
                   'SageMakerRole',
                   os.path.join(gluon_path, 'code', 'gluon.py'),
                   image=docker_image,
                   sagemaker_session=sagemaker_local_session,
                   model_server_workers=NUM_MODEL_SERVER_WORKERS)

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

    with local_mode_utils.lock():
        try:
            predictor = m.deploy(1, local_instance_type)
            output = predictor.predict(input)
            assert [4.0] == output
        finally:
            sagemaker_local_session.delete_endpoint(m.endpoint_name)
def test_default_model_fn(docker_image, sagemaker_local_session,
                          local_instance_type):
    default_handler_path = os.path.join(RESOURCE_PATH, 'default_handlers')
    m = MXNetModel('file://{}'.format(
        os.path.join(default_handler_path, 'model')),
                   'SageMakerRole',
                   os.path.join(default_handler_path, 'code',
                                'empty_module.py'),
                   image=docker_image,
                   sagemaker_session=sagemaker_local_session,
                   model_server_workers=NUM_MODEL_SERVER_WORKERS)

    input = [[1, 2]]

    with local_mode_utils.lock():
        try:
            predictor = m.deploy(1, local_instance_type)
            output = predictor.predict(input)
            assert [[4.9999918937683105]] == output
        finally:
            sagemaker_local_session.delete_endpoint(m.endpoint_name)