How to call Sagemaker training model endpoint API in C#
Asked Answered
J

2

8

I have implemented machine learning algorithms through sagemaker.

I have installed SDK for .net, and tried by executing below code.

Uri sagemakerEndPointURI = new Uri("https://runtime.sagemaker.us-east-2.amazonaws.com/endpoints/MyEndpointName/invocations");
Amazon.SageMakerRuntime.Model.InvokeEndpointRequest request = new Amazon.SageMakerRuntime.Model.InvokeEndpointRequest();
request.EndpointName = "MyEndpointName";
AmazonSageMakerRuntimeClient aawsClient = new AmazonSageMakerRuntimeClient(myAwsAccessKey,myAwsSecreteKey);            
Amazon.SageMakerRuntime.Model.InvokeEndpointResponse resposnse= aawsClient.InvokeEndpoint(request);

By executing this, I am getting validation error as "1 validation error detected: Value at 'body' failed to satisfy constraint: Member must not be null"

Can anyone guide me on how and what more input data I need to pass to call the given API?

EDIT

Further I'd tried by provinding body parameter which contains a MemoryStream written by a '.gz' or '.pkl' file, and it giving me error as : "Error unmarshalling response back from AWS, HTTP content length exceeded 5246976 bytes."

EDIT 1/23/2018

Further I came up with the error message as

ERROR - model server - 'TypeError' object has no attribute 'message'

Thanks

Jerri answered 21/1, 2018 at 10:37 Comment(4)
Why exactly is this a sparkr question??Hetman
I have used SparkR to train the model insight Sagemaker notebook.Jerri
I'm not an expert in .NET, but you are missing the Body property that should hold the input for the inference: docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.htmlNalchik
Further I'd tried by provinding body parameter which contains a MemoryStream written by a '.gz ' file, and it giving me error as : "Error unmarshalling response back from AWS"Jerri
J
1

Later solved it by Encoding.ASCII.GetBytesas in below code.

 byte[] bytes = System.IO.File.ReadAllBytes(@"EXCEL_FILE_PATH");
    string listA = "";
    while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            listA = listA + line + "\n";
        }
    byte[] bytes = Encoding.ASCII.GetBytes(listA);
    request.Body = new MemoryStream(bytes);
    InvokeEndpointResponse response = sagemakerRunTimeClient.InvokeEndpoint(request);
    string predictions = Encoding.UTF8.GetString(response.Body.ToArray());
Jerri answered 26/2, 2018 at 9:32 Comment(0)
B
0

As far as I can see, your request is missing both the Body property, as suggested by Guy and the ContentType which must refer to the type of input data you are passing to Amazon SageMaker (see the code below; my input CSV file contains a single example).

byte[] content = File.ReadAllBytes("input.csv");
Amazon.SageMakerRuntime.Model.InvokeEndpointRequest request = new Amazon.SageMakerRuntime.Model.InvokeEndpointRequest();
request.EndpointName = "linear-learner-xxxxxxxx-xxxx";
request.ContentType = "text/csv";
request.Body = new MemoryStream(content);

AmazonSageMakerRuntimeClient awsClient = new AmazonSageMakerRuntimeClient(accessKey, secretKey);
Amazon.SageMakerRuntime.Model.InvokeEndpointResponse response = awsClient.InvokeEndpoint(request);

string predictions = Encoding.UTF8.GetString(response.Body.ToArray());

With regards to the 5246976 bytes limit, that is the API reaching the maximum allowed response body length, in the context of a single request. A way to avoid that is to execute multiple calls, rather than passing large batches of items for prediction.

If you are using Amazon SageMaker built-in algorithms, you can check the allowed data format for inputs and outputs at the following address:

https://docs.aws.amazon.com/sagemaker/latest/dg/common-info-all-im-models.html

Brinson answered 4/2, 2018 at 23:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.