Ejemplo n.º 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);
            });
        }
Ejemplo n.º 2
0
        internal void Classify(UIImage source)
        {
            var pixelBuffer = source.Scale(sizeForModel).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 = currentModel.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   = "VGG16";
            prediction.predictions = byProbability;

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