Example #1
0
 public Device(IFezHat hat, IDataService dataService)
 {
     _hat         = hat;
     _dataService = dataService;
     Controls     = new Controller();
     _mlService   = new MachineLearningService(_dataService);
     CurrentState = State.Idle;
 }
Example #2
0
        [TestCaseSource("TestCases")]  // passing through testCaseSource, as NUnit does not allow passing of Enumerables
        public void TestSoftmaxOutput(IEnumerable <double> input)
        {
            var actualOutput = MachineLearningService.Softmax(input);
            var softmaxSum   = actualOutput.Sum();

            // check if the softmax output sum to 1
            Assert.AreEqual(softmaxSum, 1.0f, Epsilon);

            // check if the softmax results respect the original ordinal information
            var originalSorted = input.Select((x, i) => new KeyValuePair <double, double>(x, i))
                                 .OrderBy(x => x.Key);

            var originalIdx = originalSorted.Select(x => x.Value).ToList(); // the index of items that ranked in increasing value

            var answerSorted = actualOutput.Select((x, i) => new KeyValuePair <double, double>(x, i))
                               .OrderBy(x => x.Key);

            var answerIdx = answerSorted.Select(x => x.Value).ToList();  // the index of items that ranked in increasing value

            for (int i = 0; i < originalIdx.Count; i++)
            {
                Assert.AreEqual(originalIdx[i], answerIdx[i]);
            }
        }
Example #3
0
        /// <summary>
        /// Runs CoreML on the image in SKBitmap format .
        /// </summary>
        /// <returns>A <see cref="CategoryManager"/> that contains all observations detected from the specified
        /// <see cref="MLMultiArray"/>.</returns>
        /// <param name="input">The array representation of image after pre-processing </param>
        public CategoryManager Predict(MLMultiArray input)
        {
            MLMultiArray output1;
            MLMultiArray output2;

            (output1, output2) = RawPredict(input);

            // checking for expected dimensions
            if (output1.Shape == new nint[] { 4, NumAnchorBox, 1 })
            {
                // one anchoring box is represented by 4 points, no duplicating anchoring box
                return(null);
            }
            if (output2.Shape == new nint[] { 1, 1, NumClasses, 1, NumAnchorBox })
            {
                return(null);
            }

            // conversion from MLMultiArray to double readable content
            var outputFloats1 = new double[output1.Count]; // box prediction encoding 4 x 1917 x 1

            for (var i = 0; i < output1.Count; i++)
            {
                outputFloats1[i] = (double)output1[i];
            }

            var outputFloats2 = new double[output2.Count]; // confidence score output matrix : 1 x 1 x 23 x 1 x 1917

            for (var i = 0; i < output2.Count; i++)
            {
                outputFloats2[i] = (double)output2[i];
            }

            output1.Dispose();
            output2.Dispose();

            var cm = new CategoryManager();

            for (var ibox = 0; ibox < NumAnchorBox; ibox++)
            {
                var scoresOfClasses = new List <double>();
                for (var iclass = 0; iclass < NumClasses; iclass++)
                {
                    var offsetIndex = iclass * NumAnchorBox + ibox;
                    var logit       = outputFloats2[offsetIndex];
                    scoresOfClasses.Add(logit);
                }

                scoresOfClasses = MachineLearningService.Softmax(scoresOfClasses).ToList();

                for (var iclass = 0; iclass < NumClasses; iclass++)
                {
                    var confidence = scoresOfClasses[iclass];

                    var box = GenerateSKRect(
                        outputFloats1[BoxIndexOffset(0, ibox)],
                        outputFloats1[BoxIndexOffset(1, ibox)],
                        outputFloats1[BoxIndexOffset(2, ibox)],
                        outputFloats1[BoxIndexOffset(3, ibox)],
                        ibox);

                    if (iclass != 0 && IsWithinFrame(box))
                    {
                        var _ = new Observation(cm.GetOrCreate(iclass, ModelClassLabelEncoding.LookUpLabel(iclass)),
                                                confidence, box);
                    }
                }
            }
            return(cm);
        }
 public async Task Analyze()
 {
     MachineLearningService service = new MachineLearningService();
     await service.Analyze();
 }
        public ImagePrediction Get(string nomeImagem)
        {
            var retorno = new MachineLearningService().ClassificarImagem(nomeImagem);

            return(retorno); //
        }