private async void Controller_InputReady(object sender, VideoFrame args)
        {
            try
            {
                LogService.Write($"Evaluating model...");
                using (args)
                {
                    var output = await _model.EvaluateAsync(new mnistInput
                    {
                        Input3 = ImageFeatureValue.CreateFromVideoFrame(args)
                    });

                    var outputVector = output.Plus214_Output_0.GetAsVectorView().ToList();
                    var maxIndex     = outputVector.IndexOf(outputVector.Max());

                    NumberText = maxIndex.ToString();

                    var    topIndices       = MLHelper.GetTopLabelIndices(outputVector);
                    string topIndicesString = Environment.NewLine;
                    foreach (var topIndex in topIndices)
                    {
                        topIndicesString += $"{topIndex.LabelIndex}, Confidence: {topIndex.Confidence}" + Environment.NewLine;
                    }
                    LogService.Write(topIndicesString);
                }
            }
            catch (Exception ex)
            {
                // The WinML APIs are still in preview, so throw a visible exception so users can file a bug
                AppService.DisplayDialog(ex.GetType().Name, ex.Message);
                LogService.WriteException(ex);
            }
        }
Example #2
0
        private async Task EvaluteImageAsync(VideoFrame videoFrame)
        {
            var startTime = DateTime.Now;

            if (model == null)
            {
                var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Model/GoogLeNetPlaces.onnx"));

                if (modelFile != null)
                {
                    model = new GoogLeNetPlacesModel();
                    await MLHelper.CreateModelAsync(modelFile, model);
                }
            }
            var input = new GoogLeNetPlacesInput()
            {
                sceneImage = ImageFeatureValue.CreateFromVideoFrame(videoFrame)
            };

            try
            {
                var res = await model.EvaluateAsync(input) as GoogLeNetPlacesOutput;

                if (res != null)
                {
                    var results = new List <LabelResult>();
                    if (res.sceneLabelProbs != null)
                    {
                        var dict = res.sceneLabelProbs.FirstOrDefault();
                        foreach (var kv in dict)
                        {
                            results.Add(new LabelResult
                            {
                                Label  = kv.Key,
                                Result = (float)Math.Round(kv.Value * 100, 2)
                            });
                        }
                        results.Sort((p1, p2) =>
                        {
                            return(p2.Result.CompareTo(p1.Result));
                        });
                    }
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
                    {
                        var places                   = res.sceneLabel.GetAsVectorView().ToArray();
                        outputText.Text              = places.FirstOrDefault();
                        resultList.ItemsSource       = results;
                        previewControl.EvalutionTime = (DateTime.Now - startTime).TotalSeconds.ToString();
                    });
                }
            }
            catch (Exception ex)
            {
                await AlertHelper.ShowMessageAsync(ex.ToString());
            }
        }
        private async Task EvaluteImageAsync(VideoFrame videoFrame)
        {
            try
            {
                var startTime = DateTime.Now;
                if (model == null)
                {
                    var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Model/Resnet50.onnx"));

                    if (modelFile != null)
                    {
                        model = new ResNet50Model();
                        await MLHelper.CreateModelAsync(modelFile, model);
                    }
                }

                var input = new ResNet50ModelInput()
                {
                    image = videoFrame
                };

                var res = await model.EvaluateAsync(input) as ResNet50ModelOutput;

                if (res != null)
                {
                    var results = new List <LabelResult>();
                    foreach (var kv in res.classLabelProbs)
                    {
                        results.Add(new LabelResult
                        {
                            Label  = kv.Key,
                            Result = (float)Math.Round(kv.Value * 100, 2)
                        });
                    }
                    results.Sort((p1, p2) =>
                    {
                        return(p2.Result.CompareTo(p1.Result));
                    });
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
                    {
                        previewControl.EvalutionTime = (DateTime.Now - startTime).TotalSeconds.ToString();
                        outputText.Text        = res.classLabel.FirstOrDefault();
                        resultList.ItemsSource = results;
                    });
                }
            }
            catch (Exception ex)
            {
                await AlertHelper.ShowMessageAsync(ex.ToString());
            }
        }
Example #4
0
    public async Task LoadModelAsync()
    {
#if WINDOWS_UWP
        if (model == null)
        {
            var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Data/StreamingAssets/GoogLeNetPlaces.onnx"));

            if (modelFile != null)
            {
                model = new GoogLeNetPlacesModelModel();
                await MLHelper.CreateModelAsync(modelFile, model);
            }
        }
#endif
    }
Example #5
0
        private async Task ApplyEffectAsync(VideoFrame frame)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                if (frame != null)
                {
                    var imageWidth = frame.SoftwareBitmap != null ? frame.SoftwareBitmap.PixelWidth :
                                     frame.Direct3DSurface.Description.Width;
                    var imageHeigth = frame.SoftwareBitmap != null ? frame.SoftwareBitmap.PixelHeight :
                                      frame.Direct3DSurface.Description.Height;

                    model = ModelList.SelectedItem as ModelInfo;
                    if (model == null)
                    {
                        model = models.FirstOrDefault();
                    }
                    if (model.Model == null)
                    {
                        var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(model.File));
                        if (modelFile != null)
                        {
                            model.Model = new FNSModel();
                            await MLHelper.CreateModelAsync(modelFile, model.Model);
                        }
                    }
                    var startTime = DateTime.Now;
                    var output    = await model.Model.EvaluateAsync(new FNSInput
                    {
                        inputImage = ImageFeatureValue.CreateFromVideoFrame(frame)
                    }) as FNSOutput;

                    if (output != null)
                    {
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            previewControl.EvalutionTime = (DateTime.Now - startTime).TotalSeconds.ToString();
                        });
                        var sbmp = await ImageHelper.GetImageFromTensorFloatDataAsync(output.outputImage, (uint)imageWidth,
                                                                                      (uint)imageHeigth, frame.SoftwareBitmap.DpiX, frame.SoftwareBitmap.DpiY);
                        sbmp     = SoftwareBitmap.Convert(sbmp, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);
                        var tsbs = new SoftwareBitmapSource();
                        await tsbs.SetBitmapAsync(sbmp);
                        previewImage.Source = tsbs;
                    }
                }
            });
        }
        public async void SetUpVM(IVideoFrameInputController controller)
        {
            // WinML requires Windows 10 build 17728 or higher to run: https://docs.microsoft.com/en-us/windows/ai/
            if (!MLHelper.IsMLAvailable())
            {
                IsEnabled = false;
                AppService.DisplayDialog(FeatureUtil.GetLocalizedText("WinMLNotAvailableTitle"), FeatureUtil.GetLocalizedText("WinMLNotAvailableDescription"));
                return;
            }

            LogService.Write($"Loading model {ModelFileName}...");
            var modelFile = await FileUtil.GetFileFromInstalledLocationAsync(MLHelper.ModelBasePath + ModelFileName);

            _model = await mnistModel.CreateFromStreamAsync(modelFile);

            _controller             = controller;
            _controller.InputReady += Controller_InputReady;

            IsEnabled = true;
        }
Example #7
0
        private void OpenFile(object parameter)
        {
            OpenFileDialog opd = new OpenFileDialog();

            opd.Filter = "XML File *.xml|*.xml";

            try
            {
                if (opd.ShowDialog() == true)
                {
                    XmlDocument xDoc = MLHelper.GetMLDocument(opd.FileName);

                    MLNode retNode = MLHelper.ParserML(xDoc.DocumentElement);
                    MyNodes.Add(retNode);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Parser Error!");
            }
        }
        private async Task EvaluteImageAsync(VideoFrame videoFrame, bool isImage)
        {
            try
            {
                var startTime = DateTime.Now;
                if (model == null)
                {
                    var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Model/TinyYOLO.onnx"));

                    if (modelFile != null)
                    {
                        model = new TinyYOLOModel();
                        await MLHelper.CreateModelAsync(modelFile, model);
                    }
                }

                var input = new TinyYOLOInput()
                {
                    image = ImageFeatureValue.CreateFromVideoFrame(videoFrame)
                };

                var res = await model.EvaluateAsync(input) as TinyYOLOOutput;

                if (res != null)
                {
                    var data  = res.grid.GetAsVectorView().ToList();
                    var boxes = model.ComputeBoundingBoxes(data);
                    await DrawDetectedObjectRectAsync(boxes, isImage);

                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
                    {
                        previewControl.EvalutionTime = (DateTime.Now - startTime).TotalSeconds.ToString();
                    });
                }
            }
            catch (Exception ex)
            {
                await AlertHelper.ShowMessageAsync(ex.ToString());
            }
        }
        public void Predict()
        {
            AdequacyLevelPrediction prediction = MLHelper.Predict <AdequacyLevelData, AdequacyLevelPrediction>(dataPath, parameters, (AdequacyLevelData)inputData);

            Console.WriteLine($" - Adequacy level for {inputData.ToString()} is: {prediction.PredictedLabels}");
        }
Example #10
0
        static void Main(string[] args)
        {
            string    logPath          = @"C:\Projects\Entelect 2017\CetoBot\CetoLearner\weightLog " + DateTime.Now.ToString("yyyyMMdd HHmmss") + ".csv";
            const int LOGGING_INTERVAL = 10;

            int[] roundAve = new int[LOGGING_INTERVAL];

            // section generate data points
            List <DataPoint>            dataList   = new List <DataPoint>();
            Dictionary <int, WeightSet> WeightSets = new Dictionary <int, WeightSet>();

            //List<WeightSet> WeightSets = new List<WeightSet>();


            string[] featureList =
            {
                "HitSpacesInRow",
                //"HitSpacesInRow1",
                //"HitSpacesInRow2",
                //"HitSpacesInRow3",
                //"HitSpacesInRow4",
                //"HitSpacesInRow5",
                //"OpenSpacesInRow",
                "AdjMisses",
                "NonMissSpaces1",
                "NonMissSpaces2",
                "NonMissSpaces3",
                "NonMissSpaces4",
                "NonMissSpaces5",
                "NoDiagHitsWithAdjHit"
            };

            MapGenerator tempMap = new MapGenerator(10, 10, 0);

            foreach (int len in tempMap.GetRemainingShipLengths())
            {
                WeightSets[len] = (new WeightSet(featureList));
            }
            string[] csvHeadings = { "GAMES,ROUNDS" };
            foreach (KeyValuePair <int, WeightSet> w in WeightSets)
            {
                foreach (string s in featureList)
                {
                    csvHeadings[0] += ("," + s + w.Key);
                }
            }
            File.WriteAllLines(logPath, csvHeadings);


            Console.WriteLine("Starting Learning...");
            const double STEP_SIZE = 0.01;

            int    GameCount = 0;
            double p         = 0.0;

            double[] partial = new double[featureList.Length];

            bool manualB          = false;
            bool printB           = false;
            bool continueToLearnB = true;


            while (continueToLearnB == true)
            {
                bool gameOverB = false;
                int  Rounds    = 0;

                // Create Empty Map
                MapGenerator myMap = new MapGenerator(10, 10, 0);


                while (gameOverB == false)
                {
                    Console.Clear();
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo keyPressed = Console.ReadKey();
                        if (keyPressed.KeyChar == 'm')
                        {
                            manualB = true;
                            printB  = true;
                        }
                        else if (keyPressed.KeyChar == 'a')
                        {
                            manualB = false;
                        }
                        else if (keyPressed.KeyChar == 's')
                        {
                            printB = true;
                        }
                        else if (keyPressed.KeyChar == 'c')
                        {
                            printB = false;
                            Console.Clear();
                        }
                    }


                    int minShipLen = myMap.GetMinimumRemainingShipLength();

                    Point shootAt = DecisionMaker.AimCannons(myMap, WeightSets[minShipLen], printB);
                    bool  isHitB  = myMap.ShootAtPoint(shootAt);


                    int j = 0;
                    foreach (string s in featureList)
                    {
                        partial[j] = 0.0;
                        DataPoint d = new DataPoint(myMap.CurrentMap, shootAt, isHitB ? 1 : -1);

                        double val = 0.0;
                        val         = (d.Class == 1 ? 1 : 0);
                        val        -= MLHelper.Probability(WeightSets[minShipLen], d.Features);
                        val        *= d.Features[s];
                        partial[j] += val;
                        WeightSets[minShipLen].Weights[s] = WeightSets[minShipLen].Weights[s] + STEP_SIZE * partial[j];
                        j++;
                    }


                    Rounds++;
                    if (printB == true)
                    {
                        Console.WriteLine();
                        Console.WriteLine();
                        myMap.PrintMap();
                        Console.WriteLine();
                        Console.WriteLine();
                        Console.WriteLine("******************************************");
                        Console.WriteLine("GAME: " + (GameCount + 1));
                        Console.WriteLine("ROUND: " + Rounds);
                        Console.WriteLine("REM SHIPS: " + myMap.GetRemainingShips().Length);
                        Console.WriteLine("MIN LEN SHIP: " + myMap.GetMinimumRemainingShipLength().ToString());
                        Console.WriteLine("FEATURES: ");
                        foreach (string s in featureList)
                        {
                            Console.WriteLine(s + ":\t\t" + WeightSets[minShipLen].Weights[s]);
                        }
                        Console.WriteLine("******************************************");
                        Console.WriteLine("");
                    }

                    if (manualB == true)
                    {
                        ConsoleKeyInfo keyPressed = Console.ReadKey();
                        if (keyPressed.KeyChar == 'a')
                        {
                            manualB = false;
                        }
                    }

                    if (myMap.GetRemainingShips().Length == 0)
                    {
                        gameOverB = true;
                    }
                }
                roundAve[GameCount % 10] = Rounds;
                GameCount++;
                if (printB == true)
                {
                    Console.WriteLine("******************************************");
                    Console.WriteLine("************* GAME END ***************");
                    Console.WriteLine("******************************************");

                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine("GAMES: " + (GameCount));
                    Console.WriteLine("ROUND COUNT: " + (Rounds));
                    foreach (KeyValuePair <int, WeightSet> w in WeightSets)
                    {
                        Console.WriteLine("MIN LEN SHIP: " + w.Key);
                        Console.WriteLine("FEATURES: ");
                        foreach (string s in featureList)
                        {
                            Console.WriteLine(s + ":\t\t" + WeightSets[w.Key].Weights[s]);
                        }
                        Console.WriteLine();
                    }
                }

                if (GameCount % 10 == 0)
                {
                    string[]      oldFile     = File.ReadAllLines(logPath);
                    List <string> fileStrings = new List <string>();
                    foreach (string s in oldFile)
                    {
                        fileStrings.Add(s);
                    }
                    string strToAdd = GameCount.ToString();
                    int    ave      = 0;
                    foreach (int i in roundAve)
                    {
                        ave += i;
                    }
                    ave       = ave / roundAve.Length;
                    strToAdd += ("," + ave);
                    foreach (KeyValuePair <int, WeightSet> w in WeightSets)
                    {
                        foreach (string s in featureList)
                        {
                            strToAdd += ("," + WeightSets[w.Key].Weights[s]);
                        }
                    }
                    fileStrings.Add(strToAdd);
                    File.WriteAllLines(logPath, fileStrings);
                }

                if (GameCount == int.MaxValue)
                {
                    Console.WriteLine("REACHED MAXIMUM LOOPS");
                    continueToLearnB = false;
                }

                if (manualB == true)
                {
                    ConsoleKeyInfo keyPressed = Console.ReadKey();
                    if (keyPressed.KeyChar == 'a')
                    {
                        manualB = false;
                    }
                }
            }

            Console.WriteLine("******************************************");
            Console.WriteLine("********LEARNING OVER*****************");
            Console.WriteLine("******************************************");
        }
        public List <Prediction> ComputeBoundingBoxes(IList <float> features)
        {
            if (features.Count == TinyYOLOModelModelOutput.ML_ARRAY_LENGTH)
            {
                var predictions = new List <Prediction>();

                var blockSize    = 32f;
                var gridHeight   = 13;
                var gridWidth    = 13;
                var boxesPerCell = 5;
                var numClasses   = 20;
                // The 416x416 image is divided into a 13x13 grid. Each of these grid cells
                // will predict 5 bounding boxes (boxesPerCell). A bounding box consists of
                // five data items: x, y, width, height, and a confidence score. Each grid
                // cell also predicts which class each bounding box belongs to.
                //
                // The "features" array therefore contains (numClasses + 5)*boxesPerCell
                // values for each grid cell, i.e. 125 channels. The total features array
                // contains 125x13x13 elements.

                // NOTE: It turns out that accessing the elements in the multi-array as
                // `features[[channel, cy, cx] as [NSNumber]].floatValue` is kinda slow.
                // It's much faster to use direct memory access to the features.
                var channelStride = 169;
                var yStride       = 13;
                var xStride       = 1;

                int Offset(int channel, int x, int y)
                {
                    return(channel * channelStride + y * yStride + x * xStride);
                }

                for (var cy = 0; cy < gridHeight; cy++)
                {
                    for (var cx = 0; cx < gridWidth; cx++)
                    {
                        for (var b = 0; b < boxesPerCell; b++)
                        {
                            var channel = b * (numClasses + 5);

                            var tx = features[Offset(channel, cx, cy)];
                            var ty = features[Offset(channel + 1, cx, cy)];
                            var tw = features[Offset(channel + 2, cx, cy)];
                            var th = features[Offset(channel + 3, cx, cy)];
                            var tc = features[Offset(channel + 4, cx, cy)];

                            // The predicted tx and ty coordinates are relative to the location
                            // of the grid cell; we use the logistic sigmoid to constrain these
                            // coordinates to the range 0 - 1. Then we add the cell coordinates
                            // (0-12) and multiply by the number of pixels per grid cell (32).
                            // Now x and y represent center of the bounding box in the original
                            // 416x416 image space.
                            var x = ((float)(cx) + MLHelper.Sigmoid(tx)) * blockSize;
                            var y = ((float)(cy) + MLHelper.Sigmoid(ty)) * blockSize;

                            // The size of the bounding box, tw and th, is predicted relative to
                            // the size of an "anchor" box. Here we also transform the width and
                            // height into the original 416x416 image space.
                            var w = MathF.Exp(tw) * Anchors[2 * b] * blockSize;
                            var h = MathF.Exp(th) * Anchors[2 * b + 1] * blockSize;

                            // The confidence value for the bounding box is given by tc. We use
                            // the logistic sigmoid to turn this into a percentage.
                            var confidence = MLHelper.Sigmoid(tc);

                            // Gather the predicted classes for this anchor box and softmax them,
                            // so we can interpret these numbers as percentages.
                            var classes = new float[numClasses];
                            for (var c = 0; c < numClasses; c++)
                            {
                                // The slow way:
                                //classes[c] = features[[channel + 5 + c, cy, cx] as [NSNumber]].floatValue

                                // The fast way:
                                classes[c] = features[Offset(channel + 5 + c, cx, cy)];
                            }

                            classes = MLHelper.Softmax(classes);

                            // Find the index of the class with the largest score.
                            //var bestClassScore = classes.Max();
                            //var detectedClass = 0;
                            //for (var i = 0; i < classes.Length; i++)
                            //{
                            //    if (classes[i] == bestClassScore)
                            //    {
                            //        detectedClass = i;
                            //        break;
                            //    }
                            //}
                            var res            = MLHelper.Argmax(classes);
                            var bestClassScore = res.Item2;
                            var detectedClass  = res.Item1;

                            // Combine the confidence score for the bounding box, which tells us
                            // how likely it is that there is an object in this box (but not what
                            // kind of object it is), with the largest class prediction, which
                            // tells us what kind of object it detected (but not where).
                            var confidenceInClass = bestClassScore * confidence;

                            // Since we compute 13x13x5 = 845 bounding boxes, we only want to
                            // keep the ones whose combined score is over a certain threshold.
                            if (confidenceInClass > confidenceThreshold)
                            {
                                var rect = new Rect((x - w / 2), (y - h / 2),
                                                    w, h);

                                var prediction = new Prediction(classIndex: detectedClass,
                                                                score: confidenceInClass,
                                                                rect: rect);
                                predictions.Add(prediction);
                            }
                        }
                    }
                }
                // We already filtered out any bounding boxes that have very low scores,
                // but there still may be boxes that overlap too much with others. We'll
                // use "non-maximum suppression" to prune those duplicate bounding boxes.
                return(NonMaxSuppression(boxes: predictions, limit: maxBoundingBoxes, threshold: iouThreshold));
            }
            return(null);
        }
Example #12
0
        private void Go()
        {
            var lijstje = DataSetGenerator.Generate();


            int size = 500;
            var img  = new Image <Rgba32>(size, size);

            MLImager.AddPointsToImage(img, lijstje);

            using (var fs = new FileStream("output.png", FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                img.SaveAsPng(fs);
            }



            lijstje.Shuffle();

            var testData  = lijstje.Take(lijstje.Count / 2).ToList();
            var trainData = lijstje.Skip(lijstje.Count / 2).ToList();


            foreach (var inp in INPUTS.INPUTSDICT)
            {
                state[inp.Key] = false;
            }

            state["x"] = true;
            state["y"] = true;
            //state["xTimesY"] = true;

            //var networkShape = new List<int>() { 2, 1, 1 };
            var networkShape = new List <int>()
            {
                2, 8, 8, 8, 8, 8, 8, 1
            };
            var inputIds = new List <string>()
            {
                "x", "y"
            };

            var network = NetworkBuilder.BuildNetwork(networkShape, Activations.TANH, Activations.TANH, null, inputIds, false);

            var w = Stopwatch.StartNew();

            for (int i = 0; i < 100000; i++)
            {
                foreach (var trainPoint in trainData)
                {
                    var input = MLHelper.ConstructInput(state, trainPoint.X, trainPoint.Y);
                    NetworkBuilder.ForwardProp(network, input);
                    NetworkBuilder.BackProp(network, trainPoint.Label, Errors.SQUARE);
                    if ((i + 1) % batchSize == 0)
                    {
                        NetworkBuilder.UpdateWeights(network, learningrate, regularizationRate);
                    }
                }
                lossTrain = MLHelper.GetLoss(network, state, trainData);
                lossTest  = MLHelper.GetLoss(network, state, testData);



                Console.WriteLine($"{i}: LossTrain: {lossTrain} LossTest: {lossTest}");


                if (w.Elapsed.TotalSeconds > 1)
                {
                    var img2 = MLImager.GenerateImage(network, state, size);
                    MLImager.AddPointsToImage(img2, lijstje);

                    try
                    {
                        using (var fs = new FileStream("output2.png", FileMode.Create, FileAccess.Write, FileShare.Read))
                        {
                            img2.SaveAsPng(fs);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    w.Restart();
                }



                //for (int y = 1; y < network.Count; y++)
                //{
                //    var layer = network[y];
                //    Console.WriteLine(y);
                //    foreach (var node in layer)
                //    {
                //        var sb = new StringBuilder();
                //        foreach (var link in node.InputLinks)
                //        {
                //            sb.Append($"Lnk: {link.Weight} ");
                //        }
                //        Console.WriteLine($"   {sb.ToString()}");
                //    }
                //    Console.WriteLine();
                //}

                //double cccc = 0;
                //foreach (var testPoint in testData)
                //{
                //    var input = ConstructInput(testPoint.X, testPoint.Y);
                //    var result = NetworkBuilder.ForwardProp(network, input);

                //    var res = Math.Abs(result - testPoint.Label);
                //    cccc += res;
                //}
                //Console.WriteLine($"Res: {cccc}");
            }
        }
Example #13
0
        public void Predict()
        {
            FlowerTypePrediction prediction = MLHelper.Predict <FlowerTypeData, FlowerTypePrediction>(dataPath, parameters, (FlowerTypeData)inputData);

            Console.WriteLine($" - Predicted flower type for {inputData.ToString()} is: {prediction.PredictedLabels}");
        }
Example #14
0
        private Command MakeMove(dynamic state)
        {
            Dictionary <string, double> weights = new Dictionary <string, double>();

            /*weights["NorthHit"] = 184983;
             * weights["NorthMiss"] = -12206;
             * weights["NorthOpen"] = -6000;
             *
             * weights["SouthHit"] = 184983;
             * weights["SouthMiss"] = -12206;
             * weights["SouthOpen"] = -6000;
             *
             * weights["EastHit"] = 184983;
             * weights["EastMiss"] = -12206;
             * weights["EastOpen"] = -6000;
             *
             * weights["WestHit"] = 184983;
             * weights["WestMiss"] = -12206;
             * weights["WestOpen"] = -6000;*/

            weights["NorthHit"]  = 0.996507500606418;
            weights["NorthMiss"] = -0.0671588524624563;
            weights["NorthOpen"] = -0.0534422400458033;

            weights["SouthHit"]  = 0.996507500606418;
            weights["SouthMiss"] = -0.0671588524624563;
            weights["SouthOpen"] = -0.0534422400458033;

            weights["EastHit"]  = 0.996507500606418;
            weights["EastMiss"] = -0.0671588524624563;
            weights["EastOpen"] = -0.0534422400458033;

            weights["WestHit"]  = 0.996507500606418;
            weights["WestMiss"] = -0.0671588524624563;
            weights["WestOpen"] = -0.0534422400458033;


            // Generate map from Json
            MapGenerator myMap = new MapGenerator(state);

            // Look for open space with highest probability
            Point            shootAt = new Point();
            bool             firstOpenSpaceFoundB = false;
            double           highestProb          = 0.0f;
            List <DataPoint> possiblePoints       = new List <DataPoint>();

            foreach (MapSpace m in myMap.CurrentMap.Cells)
            {
                if (m.State == SpaceState.Open)
                {
                    if (firstOpenSpaceFoundB == false)
                    {
                        firstOpenSpaceFoundB = true;
                        shootAt = m.Coords;
                    }
                    double prob = 0.0f;

                    DataPoint d = new DataPoint(myMap.CurrentMap, m.Coords);
                    prob = MLHelper.Probability(weights, d.Features);
                    prob = Math.Round(prob, 6);
                    //Log(String.Format("{0}",prob));
                    if (prob >= highestProb)
                    {
                        highestProb = prob;
                    }
                }
            }

            foreach (MapSpace m in myMap.CurrentMap.Cells)
            {
                if (m.State == SpaceState.Open)
                {
                    if (firstOpenSpaceFoundB == false)
                    {
                        firstOpenSpaceFoundB = true;
                        shootAt = m.Coords;
                    }
                    double prob = 0.0f;

                    DataPoint d = new DataPoint(myMap.CurrentMap, m.Coords);
                    prob = MLHelper.Probability(weights, d.Features);
                    prob = Math.Round(prob, 6);
                    if (prob == highestProb)
                    {
                        possiblePoints.Add(d);
                    }
                }
            }

            Random rnd = new Random();

            if (possiblePoints.Count > 0)
            {
                shootAt = possiblePoints[rnd.Next(0, possiblePoints.Count)].Coords;
            }


            return(new Command(Code.FireShot, shootAt.X, shootAt.Y));
        }
        private async void ImagePickerControl_ImagePreviewReceived(object sender, WindowsMLDemos.Common.UI.ImagePreviewReceivedEventArgs e)
        {
            try
            {
                myMap.Visibility = Visibility.Visible;
                if (rnModel == null)
                {
                    var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Model/RN1015k500.onnx"));

                    if (modelFile != null)
                    {
                        rnModel = new RN1015k500Model();
                        await MLHelper.CreateModelAsync(modelFile, rnModel, true);
                    }
                }
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                {
                    if (e.PreviewImage != null)
                    {
                        var output = await rnModel.EvaluateAsync(new RN1015k500Input
                        {
                            data = ImageFeatureValue.CreateFromVideoFrame(e.PreviewImage)//TensorFloat16Bit.CreateFromArray(new long[] { -1, 3, 224, 224 }, imageData)
                        }) as RN1015k500Output;


                        if (output != null)
                        {
                            var res = output.classLabel.GetAsVectorView().ToArray();
                            var b   = output.softmax_output?.ToList();
                            if (res.Length > 0)
                            {
                                var locationData = res[0].Split('\t');
                                var city         = locationData[0];
                                var lat          = float.Parse(locationData[1]);
                                var lon          = float.Parse(locationData[2]);
                                var sPoint       = new Geopoint(new BasicGeoposition
                                {
                                    Latitude  = lat,
                                    Longitude = lon
                                });


                                Geopoint pointToReverseGeocode = sPoint;

                                // Reverse geocode the specified geographic location.
                                MapLocationFinderResult result =
                                    await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);

                                // If the query returns results, display the name of the town
                                // contained in the address of the first result.
                                if (result.Status == MapLocationFinderStatus.Success)
                                {
                                    city = result.Locations[0].DisplayName;// + result.Locations[0].Address.FormattedAddress + result.Locations[0].Address.Country;
                                }
                                myMap.Center = sPoint;

                                myMap.Layers.Add(new MapElementsLayer
                                {
                                    ZIndex      = 1,
                                    MapElements = new List <MapElement>()
                                    {
                                        new MapIcon
                                        {
                                            Location = sPoint,
                                            NormalizedAnchorPoint = new Windows.Foundation.Point(0.5, 1),
                                            ZIndex = 0,
                                            Title  = city,
                                            CollisionBehaviorDesired = MapElementCollisionBehavior.RemainVisible,
                                            Visible = true
                                        }
                                    }
                                });
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                await AlertHelper.ShowMessageAsync(ex.ToString());
            }
            rnModel.Session.Dispose();
            rnModel.LearningModel.Dispose();
            rnModel = null;
        }