コード例 #1
0
        static async Task <byte[]> TestSageMakerEndpoint()
        {
            string awsAccessKey = "";
            string awsSecretKey = "";

            byte[] content = Encoding.ASCII.GetBytes("{\"instances\": [{\"data\": \"abandon\"}]}");

            InvokeEndpointRequest request = new InvokeEndpointRequest();

            request.Accept       = "application/json";
            request.ContentType  = "application/json";
            request.Body         = new MemoryStream(content);
            request.EndpointName = "Seq2SeqEndpoint-2019-05-09-10-10-22";

            AmazonSageMakerRuntimeClient client   = new AmazonSageMakerRuntimeClient(awsAccessKey, awsSecretKey, RegionEndpoint.USEast1);
            InvokeEndpointResponse       response = await client.InvokeEndpointAsync(request);

            return(response.Body.ToArray());
        }
コード例 #2
0
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            InvokeEndpointResponse response = new InvokeEndpointResponse();

            var ms = new MemoryStream();

            Amazon.Util.AWSSDKUtils.CopyStream(context.Stream, ms);
            ms.Seek(0, SeekOrigin.Begin);
            response.Body = ms;
            if (context.ResponseData.IsHeaderPresent("Content-Type"))
            {
                response.ContentType = context.ResponseData.GetHeaderValue("Content-Type");
            }
            if (context.ResponseData.IsHeaderPresent("x-Amzn-Invoked-Production-Variant"))
            {
                response.InvokedProductionVariant = context.ResponseData.GetHeaderValue("x-Amzn-Invoked-Production-Variant");
            }

            return(response);
        }
コード例 #3
0
        public async Task <IActionResult> Predict(string eventData, int SageMakerID)
        {
            Trigger entity = await _context.AlertTriggers.FindAsync(SageMakerID);

            if (entity == null || eventData == null)
            {
                return(StatusCode(404));
            }
            if (eventData.Contains(entity.Condtion) && eventData.Contains("R:301"))
            {
                string[] eventDataSplit = eventData.Split('|');
                List <GenericRecordHolder> genericRecordHolder = new List <GenericRecordHolder>
                {
                    new GenericRecordHolder
                    {
                        field1 = eventDataSplit[2],
                        field2 = eventDataSplit[0]
                    }
                };
                MemoryStream     sendingMemoryStream = new MemoryStream();
                CsvConfiguration config = new CsvConfiguration(CultureInfo.CurrentCulture)
                {
                    HasHeaderRecord = false
                };
                using (var streamWriter = new StreamWriter(sendingMemoryStream))
                {
                    using (var csvWriter = new CsvWriter(streamWriter, config))
                    {
                        csvWriter.WriteRecords(genericRecordHolder);
                    }
                }
                InvokeEndpointResponse invokeEndpointResponse = await _SageMakerClient.InvokeEndpointAsync(new InvokeEndpointRequest
                {
                    Accept       = "application/json",
                    ContentType  = "text/csv",
                    EndpointName = entity.EndpointName,
                    Body         = new MemoryStream(sendingMemoryStream.ToArray())
                });

                if (invokeEndpointResponse.HttpStatusCode.Equals(HttpStatusCode.OK))
                {
                    string       json = string.Empty;
                    MemoryStream receivingMemoryStream = new MemoryStream(invokeEndpointResponse.Body.ToArray())
                    {
                        Position = 0
                    };
                    using (StreamReader reader = new StreamReader(receivingMemoryStream))
                    {
                        json = reader.ReadToEnd();
                    }
                    IPInsightsPredictions predictions = JsonConvert.DeserializeObject <IPInsightsPredictions>(json);
                    TempData["Alert"]   = "Success";
                    TempData["Message"] = "The Machine Learning Model returned " + predictions.Predictions[0].Dot_product;
                    return(RedirectToAction("Streaming", new { InputID = entity.LinkedLogInputID }));
                }
                else
                {
                    TempData["Alert"]   = "Danger";
                    TempData["Message"] = "The Machine Learning Model is facing some issues at the moment...";
                    return(RedirectToAction("Streaming", new { InputID = entity.LinkedLogInputID }));
                }
            }
            else
            {
                TempData["Alert"]   = "Warning";
                TempData["Message"] = "This event cannot be inferred by the Machine Learning Model!";
                return(RedirectToAction("Streaming", new { InputID = entity.LinkedLogInputID }));
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            string accesKeyId      = "";
            string accessKeySecret = "";
            string trainingImage   = "";
            string roleArn         = "";

            string trainingJobName = $"ontology-training-job-{DateTime.UtcNow.Ticks}";
            string endpointName    = $"ontology-endpoint-{DateTime.UtcNow.Ticks}";

            using (AmazonS3Client client = new AmazonS3Client(accesKeyId, accessKeySecret, Amazon.RegionEndpoint.EUCentral1))
            {
                TransferUtility fileTransferUtility = new TransferUtility(client);

                // upload our csv training\test files

                using (AmazonSageMakerClient awsSageMakerClient = new AmazonSageMakerClient(accesKeyId, accessKeySecret, Amazon.RegionEndpoint.EUCentral1))
                {
                    CreateTrainingJobResponse response = awsSageMakerClient.CreateTrainingJobAsync(new CreateTrainingJobRequest()
                    {
                        AlgorithmSpecification = new AlgorithmSpecification()
                        {
                            TrainingInputMode = TrainingInputMode.File,
                            TrainingImage     = trainingImage
                        },
                        OutputDataConfig = new OutputDataConfig()
                        {
                            S3OutputPath = "https://s3.eu-central-1.amazonaws.com/sagemaker-ovechko/sagemaker/test-csv/output"
                        },
                        ResourceConfig = new ResourceConfig()
                        {
                            InstanceCount  = 1,
                            InstanceType   = TrainingInstanceType.MlM4Xlarge,
                            VolumeSizeInGB = 5
                        },
                        TrainingJobName = trainingJobName,
                        HyperParameters = new Dictionary <string, string>()
                        {
                            { "eta", "0.1" },
                            { "objective", "multi:softmax" },
                            { "num_round", "5" },
                            { "num_class", "3" }
                        },
                        StoppingCondition = new StoppingCondition()
                        {
                            MaxRuntimeInSeconds = 3600
                        },
                        RoleArn         = roleArn,
                        InputDataConfig = new List <Channel>()
                        {
                            new Channel()
                            {
                                ChannelName = "train",
                                DataSource  = new DataSource()
                                {
                                    S3DataSource = new S3DataSource()
                                    {
                                        S3DataType             = S3DataType.S3Prefix,
                                        S3Uri                  = "https://s3.eu-central-1.amazonaws.com/sagemaker-ovechko/sagemaker/test-csv/train/",
                                        S3DataDistributionType = S3DataDistribution.FullyReplicated
                                    }
                                },
                                ContentType     = "csv",
                                CompressionType = Amazon.SageMaker.CompressionType.None
                            },
                            new Channel()
                            {
                                ChannelName = "validation",
                                DataSource  = new DataSource()
                                {
                                    S3DataSource = new S3DataSource()
                                    {
                                        S3DataType             = S3DataType.S3Prefix,
                                        S3Uri                  = "https://s3.eu-central-1.amazonaws.com/sagemaker-ovechko/sagemaker/test-csv/validation/",
                                        S3DataDistributionType = S3DataDistribution.FullyReplicated
                                    }
                                },
                                ContentType     = "csv",
                                CompressionType = Amazon.SageMaker.CompressionType.None
                            }
                        }
                    }).Result;

                    string modelName = $"{trainingJobName}-model";

                    DescribeTrainingJobResponse info = new DescribeTrainingJobResponse()
                    {
                        TrainingJobStatus = TrainingJobStatus.InProgress
                    };

                    while (info.TrainingJobStatus == TrainingJobStatus.InProgress)
                    {
                        info = awsSageMakerClient.DescribeTrainingJobAsync(new DescribeTrainingJobRequest()
                        {
                            TrainingJobName = trainingJobName
                        }).Result;

                        if (info.TrainingJobStatus == TrainingJobStatus.InProgress)
                        {
                            Logger.Info("Training job creation is in progress...");
                            Thread.Sleep(10000);
                        }
                    }

                    Logger.Info($"Training job creation has been finished. With status {info.TrainingJobStatus.ToString()}. {info.FailureReason}");

                    if (info.TrainingJobStatus == TrainingJobStatus.Completed)
                    {
                        CreateModelResponse modelCreationInfo = awsSageMakerClient.CreateModelAsync(new CreateModelRequest()
                        {
                            ModelName        = modelName,
                            ExecutionRoleArn = roleArn,
                            PrimaryContainer = new ContainerDefinition()
                            {
                                ModelDataUrl = info.ModelArtifacts.S3ModelArtifacts,
                                Image        = trainingImage
                            }
                        }).Result;

                        string endpointConfigName = $"{endpointName}-config";

                        awsSageMakerClient.CreateEndpointConfigAsync(new CreateEndpointConfigRequest()
                        {
                            EndpointConfigName = endpointConfigName,
                            ProductionVariants = new List <ProductionVariant>()
                            {
                                new ProductionVariant()
                                {
                                    InstanceType         = ProductionVariantInstanceType.MlM4Xlarge,
                                    InitialVariantWeight = 1,
                                    InitialInstanceCount = 1,
                                    ModelName            = modelName,
                                    VariantName          = "AllTraffic"
                                }
                            }
                        });

                        CreateEndpointResponse endpointCreationInfo = awsSageMakerClient.CreateEndpointAsync(new CreateEndpointRequest()
                        {
                            EndpointConfigName = endpointConfigName,
                            EndpointName       = endpointName
                        }).Result;

                        EndpointStatus currentStatus = EndpointStatus.Creating;
                        while (currentStatus == EndpointStatus.Creating)
                        {
                            currentStatus = awsSageMakerClient.DescribeEndpointAsync(new DescribeEndpointRequest()
                            {
                                EndpointName = endpointName
                            }).Result.EndpointStatus;

                            if (currentStatus == EndpointStatus.Creating)
                            {
                                Logger.Info("Endpoint creation is in progress...");
                                Thread.Sleep(10000);
                            }
                        }

                        Logger.Info("Endpoint creation has been finished.");

                        if (currentStatus == EndpointStatus.InService)
                        {
                            using (AmazonSageMakerRuntimeClient sageMakerRuntimeClient = new AmazonSageMakerRuntimeClient(accesKeyId, accessKeySecret, Amazon.RegionEndpoint.EUCentral1))
                            {
                                using (MemoryStream ms = new MemoryStream())
                                {
                                    GetObjectResponse s3Response = client.GetObjectAsync("sagemaker-ovechko", "sagemaker/test-csv/test/test.csv").Result;
                                    s3Response.ResponseStream.CopyTo(ms);
                                    ms.Seek(0, SeekOrigin.Begin);
                                    using (StreamReader sr = new StreamReader(ms))
                                    {
                                        string csv = sr.ReadToEnd();
                                        csv = csv.Replace("", string.Empty);
                                        using (MemoryStream ms2 = new MemoryStream(Encoding.ASCII.GetBytes(csv)))
                                        {
                                            InvokeEndpointResponse endpointResponseInfo = sageMakerRuntimeClient.InvokeEndpointAsync(new InvokeEndpointRequest()
                                            {
                                                ContentType  = "text/csv",
                                                EndpointName = endpointName,
                                                Body         = ms2,
                                            }).Result;
                                            using (StreamReader sr2 = new StreamReader(endpointResponseInfo.Body))
                                            {
                                                string endpointResponseBody = sr2.ReadToEnd();
                                                Logger.Info(endpointResponseBody);
                                            }
                                        }
                                    }
                                }

                                Logger.Info("Performing clean up...");

                                awsSageMakerClient.DeleteEndpointAsync(new DeleteEndpointRequest()
                                {
                                    EndpointName = endpointName
                                });

                                awsSageMakerClient.DeleteEndpointConfigAsync(new DeleteEndpointConfigRequest()
                                {
                                    EndpointConfigName = endpointConfigName
                                });

                                awsSageMakerClient.DeleteModelAsync(new DeleteModelRequest()
                                {
                                    ModelName = modelName
                                });

                                Logger.Info("Clean up finished.");
                            }
                        }
                    }
                }
            }
            Console.ReadLine();
        }