/// <summary>
        /// Uses ML.NET to predict iris flower species based on input an trained model.
        /// </summary>
        /// <param name="runnerRequest">Request with input needed for iris flower species determination.</param>
        /// <returns>Iris flower species prediction result.</returns>
        public async Task <RunnerResponse> RunClassificationAsync(RunnerRequest runnerRequest)
        {
            try
            {
                var modelBuilder = new ModelBuilder();
                var trainedModel = await modelBuilder.TrainAsync();

                var modelMetrics = modelBuilder.Evaluate(trainedModel);

                var modelInput = runnerRequest.ModelInput
                                 .Select(p => new DataModel
                {
                    SepalLength = p.SepalLength,
                    SepalWidth  = p.SepalWidth,
                    PetalLength = p.PetalLength,
                    PetalWidth  = p.PetalWidth
                })
                                 .ToList();
                var modelOutput = modelBuilder.Predict(trainedModel, modelInput)
                                  .Select(p => new ModelOutput
                {
                    PredictedSpecies = p.PredictedLabels
                })
                                  .ToList();

                return(new RunnerResponse
                {
                    Success = true,
                    ModelOutput = modelOutput
                });
            }
            catch (Exception ex)
            {
                return(new RunnerResponse
                {
                    Success = false,
                    Message = ex.ToExceptionMessage()
                });
            }
        }
예제 #2
0
        private static async Task runIrisFlowerSpeciesDeterminationAsync()
        {
            var modelInput = new SpeciesDetermination.ModelInput
            {
                SepalLength = getIrisFlowerInputFloatValue(IrisFlowerInputType.SepalLength),
                SepalWidth  = getIrisFlowerInputFloatValue(IrisFlowerInputType.SepalWidth),
                PetalLength = getIrisFlowerInputFloatValue(IrisFlowerInputType.PetalLength),
                PetalWidth  = getIrisFlowerInputFloatValue(IrisFlowerInputType.PetalWidth)
            };

            var runnerRequest = new SpeciesDetermination.RunnerRequest
            {
                ModelInput = new List <SpeciesDetermination.ModelInput> {
                    modelInput
                }
            };

            var runnerResponse = await SpeciesDetermination.ModelRunner.Instance.RunClassificationAsync(runnerRequest);

            if (!runnerResponse.Success)
            {
                Console.WriteLine($"Iris flower species determination failed: {runnerResponse.Message}");
            }
        }