Exemple #1
0
        void ShowPrediction(ImageDescriptionPrediction imageDescriptionPrediction)
        {
            //Grab the first 5 predictions, format them for display, and show 'em
            InvokeOnMainThread(() =>
            {
                var message = $"{imageDescriptionPrediction.ModelName} thinks:\n";
                var topFive = imageDescriptionPrediction.predictions.Take(5);
                foreach (var prediction in topFive)
                {
                    var prob = prediction.Item1;
                    var desc = prediction.Item2;
                    message += $"{desc} : {prob.ToString("P") }\n";
                }

                ShowMessage(message);
            });
        }
        internal void Classify(UIImage source)
        {
            var model = models[currentModel];

            var pixelBuffer = source.Scale(sizeFor[model]).ToCVPixelBuffer();
            var imageValue  = MLFeatureValue.Create(pixelBuffer);

            var inputs = new NSDictionary <NSString, NSObject>(new NSString("image"), imageValue);

            NSError error, error2;
            var     inputFp = new MLDictionaryFeatureProvider(inputs, out error);

            if (error != null)
            {
                ErrorOccurred(this, new EventArgsT <string>(error.ToString()));
                return;
            }
            var outFeatures = model.GetPrediction(inputFp, out error2);

            if (error2 != null)
            {
                ErrorOccurred(this, new EventArgsT <string>(error2.ToString()));
                return;
            }

            var predictionsDictionary = outFeatures.GetFeatureValue("classLabelProbs").DictionaryValue;
            var byProbability         = new List <Tuple <double, string> >();

            foreach (var key in predictionsDictionary.Keys)
            {
                var description = (string)(NSString)key;
                var prob        = (double)predictionsDictionary[key];
                byProbability.Add(new Tuple <double, string>(prob, description));
            }
            //Sort descending
            byProbability.Sort((t1, t2) => t1.Item1.CompareTo(t2.Item1) * -1);

            var prediction = new ImageDescriptionPrediction();

            prediction.ModelName   = currentModel;
            prediction.predictions = byProbability;

            PredictionsUpdated(this, new EventArgsT <ImageDescriptionPrediction>(prediction));
        }