Example #1
0
        public void Score(List <Data> TestingData)
        {
            int estimatedClass;

            for (int i = 0; i < TestingData.Count; ++i)
            {
                List <double> features = trainedModel.featureExtraction.Extract(TestingData[i].Points);
                List <double> filtered = new List <double>();
                for (int h = 0; h < features.Count; ++h)
                {
                    if (!trainedModel.Badass[h])
                    {
                        filtered.Add(features[h]);
                    }
                }

                estimatedClass = classifier.Classify(filtered);
                ++ConfussionMatrix[TestingData[i].Class, estimatedClass];
                if (estimatedClass != (int)TestingData[i].Class)
                {
                    ++MissedRows;
                }
            }
            Accuracy = ((double)(TestingData.Count - MissedRows) * 100) / TestingData.Count;
        }
Example #2
0
        private async Task HandlePhoto(MediaFile file)
        {
            var stream = file.GetStreamWithImageRotatedForExternalStorage();

            var memoryStream = new MemoryStream();

            stream.CopyTo(memoryStream);

            var bytes = memoryStream.ToArray();

            if (Connectivity.NetworkAccess == NetworkAccess.Internet)
            {
                try
                {
                    defaultClassifier.ClassificationCompleted += Classifier_ClassificationCompleted;

                    await defaultClassifier.Classify(bytes);

                    return;
                }
                catch (Exception ex)
                {
                }
            }

            offlineClassifier.ClassificationCompleted += Classifier_ClassificationCompleted;

            await offlineClassifier.Classify(bytes);
        }
Example #3
0
        private double CalculatePooledAUC(IClassifier classifier, Dataset dataset)
        {
            double final = 0;

            int labelSize = dataset.Metadata.Target.Values.Length;

            double[][] values = new double[labelSize][];
            int        index  = 0;

            for (int instanceIndex = 0; instanceIndex < dataset.Size; instanceIndex++)
            {
                values[index] = new double[2];

                Instance   instance    = dataset[instanceIndex];
                bool[]     actualFlags = instance.LabelFlags;
                Prediction prediction  = classifier.Classify(instance);


                for (int classIndex = 1; classIndex < labelSize; classIndex++)
                {
                    values[index][0] = prediction.Probabilities[classIndex];
                    values[index][1] = actualFlags[classIndex] ? 1 : 0;
                    index++;
                }
            }

            return(final);
        }
Example #4
0
        public void OrganizeDirectory(ClassifierArgs args)
        {
            var files = _storageUtilities.RetrieveMediaFiles(args);

            _classifier.Classify(files, args);
            _storageUtilities.MoveFiles(files);
        }
Example #5
0
        private double CalculateAverageAUC(IClassifier classifier, Dataset dataset)
        {
            double final = 0;

            int labelSize = dataset.Metadata.Target.Values.Length;

            double[][] values = new double[dataset.Size][];
            double     area   = 0;
            double     weight = 1.0 / (double)labelSize - 1;

            for (int classIndex = 0; classIndex < labelSize; classIndex++)
            {
                int index = 0;


                for (int instanceIndex = 0; instanceIndex < dataset.Size; instanceIndex++)
                {
                    values[index] = new double[2];

                    Instance instance = dataset[instanceIndex];

                    bool[]     actualFlags = instance.LabelFlags;
                    Prediction prediction  = classifier.Classify(instance);

                    values[index][0] = prediction.Probabilities[classIndex];
                    values[index][1] = actualFlags[classIndex] ? 1 : 0;
                    index++;
                }

                Curve curve = this._curveBuilder.CreateCurve(values);
                area += weight * curve.CalculateArea();
            }

            return(final);
        }
Example #6
0
        public int Classify(Image <Bgr, byte> colorImage, Image <Gray, byte> mono01Image, Image <Gray, byte> mono02Image, out double score)
        {
            var data       = extractor.ExtractDescriptor(colorImage, mono01Image, mono02Image, (int)TomatoType.UNCLASSIFIED);
            var prediction = classifier.Classify(data.Features);

            score = prediction.Item2;
            return(prediction.Item1);
        }
Example #7
0
 public OutputData <double[], double> ConvertProcessToOutput(ProcessData processData, IModel model)
 {
     return(new OutputData <double[], double>()
     {
         OriginalData = processData.Data[0, 0].ToRowMajorArray(),
         RealResult = GetFromOnehot(processData.Label[0, 0].ToRowMajorArray()),
         PredictedResult = GetFromOnehot(classifier.Classify(model.Predict(processData.Data))[0, 0].ToRowMajorArray())
     });
 }
Example #8
0
        /// <summary>The rollback for.</summary>
        /// <param name="exception">The exception.</param>
        /// <returns>The System.Boolean.</returns>
        public bool RollbackFor(Exception exception)
        {
            if (rollbackClassifier == null)
            {
                return(true);
            }

            return(rollbackClassifier.Classify(exception));
        }
Example #9
0
        private static int EvaluateClassifier(IClassifier net, IEnumerable <IDataPoint> testData)
        {
            var testResults = testData.Where(dataPoint =>
            {
                var networkOutput = net.Classify(dataPoint.Input).Enumerate().ToList();
                return(networkOutput.MaxIndex() == dataPoint.Output.Enumerate().MaxIndex());
            });

            return(testResults.Count());
        }
Example #10
0
        private static void Evaluate(IClassifier classifier, IEnumerable <Sms> verificationData)
        {
            var valid     = verificationData.Average(x => Validate(x.Label, classifier.Classify(x.Text)));
            var validHam  = verificationData.Where(x => x.Label == SmsLabel.Ham).Average(x => Validate(x.Label, classifier.Classify(x.Text)));
            var validSpam = verificationData.Where(x => x.Label == SmsLabel.Spam).Average(x => Validate(x.Label, classifier.Classify(x.Text)));

            Console.WriteLine($"Correctly classified: {valid * 100:F2}%");
            Console.WriteLine($"Correctly classified Ham: {validHam * 100:F2}%");
            Console.WriteLine($"Correctly classified Spam: {validSpam * 100:F2}%");
        }
        static double Quality(IClassifier classifier, IList <ClassifiedTrademark> controlSet)
        {
            int error = 0;

            foreach (var trademark in controlSet)
            {
                if (classifier.Classify(trademark.Image) != trademark.TrademarkClass)
                {
                    error++;
                }
            }
            return((double)error / controlSet.Count);              // the less the better
        }
        private void HandlePhoto(MediaFile photo)
        {
            if (photo == null)
            {
                return;
            }

            var stream = photo.GetStream();

            bytes = ReadFully(stream);

            classifier.ClassificationCompleted += Classifier_ClassificationCompleted;
            classifier.Classify(bytes);
        }
Example #13
0
        public int[] Classify(List <Data.Example> examples)
        {
            List <int> prediceted = new List <int>();

            Queue <Node> candidates = new Queue <Node>();

            foreach (string child in this._hierarchy.Root.Children)
            {
                candidates.Enqueue(this._hierarchy[child]);
            }


            while (candidates.Count != 0)
            {
                Node current = candidates.Dequeue();
                List <Tuple <string, IClassifier> > classifiers = this._modelMapping[current.Name];

                int[] values = new int[classifiers.Count];
                int   index  = 0;
                foreach (Tuple <string, IClassifier> tuple in classifiers)
                {
                    string      classifierName = tuple.Item1;
                    IClassifier classifier     = tuple.Item2;
                    Example     example        = examples.Find(e => e.Dataset.Metadata.DatasetName == classifierName);
                    int         localLabel     = classifier.Classify(example.Clone() as Example);
                    values[index] = localLabel;
                }

                IClassifier metaClassifier = this._metaModel[current.Name];
                Example     metaExample    = new Example(new Dataset(metaClassifier.MetaData), 0, values, -1);

                int label = metaClassifier.Classify(metaExample);


                if (label == 0)
                {
                    prediceted.Add(current.ValueIndex);
                    if (current.Children != null)
                    {
                        foreach (string child in current.Children)
                        {
                            candidates.Enqueue(this._hierarchy[child]);
                        }
                    }
                }
            }

            return(prediceted.Distinct().ToArray());
        }
Example #14
0
        public double Estimate(IClassifier classifier)
        {
            classifier.Train(problem.TrainingSet);
            Console.WriteLine(classifier.GetType().Name + " trained...");

            int classifiedSuccessfully = 0;

            foreach (var trademark in problem.ControlSet)
            {
                if (classifier.Classify(trademark.Image) == trademark.TrademarkClass)
                {
                    classifiedSuccessfully++;
                }
            }
            return((double)classifiedSuccessfully / problem.ControlSet.Count);
        }
Example #15
0
        private double CalculateWeightedAUC(IClassifier classifier, Dataset dataset)
        {
            double final = 0;

            int labelSize = dataset.Metadata.Target.Values.Length;

            double[][] values    = new double[dataset.Size][];
            double[]   areas     = new double[labelSize];
            double[]   frequency = new double[labelSize];
            double     weight    = 1.0 / (double)labelSize;
            double     total     = 0;

            for (int classIndex = 0; classIndex < labelSize; classIndex++)
            {
                int    index     = 0;
                double positives = 0;

                for (int instanceIndex = 0; instanceIndex < dataset.Size; instanceIndex++)
                {
                    values[index] = new double[2];

                    Instance instance = dataset[instanceIndex];

                    bool[]     actualFlags = instance.LabelFlags;
                    Prediction prediction  = classifier.Classify(instance);

                    values[index][0] = prediction.Probabilities[classIndex];
                    values[index][1] = actualFlags[classIndex] ? 1 : 0;
                    positives       += values[index][1];
                    index++;
                }

                Curve curve = this._curveBuilder.CreateCurve(values);
                areas[classIndex]     = curve.CalculateArea();
                frequency[classIndex] = positives;
                total += positives;
            }


            for (int classIndex = 0; classIndex < labelSize; classIndex++)
            {
                final += ((frequency[classIndex] / total) * areas[classIndex]);
            }

            return(final);
        }
        public List <Tuple <string, double> > Classify(Sentence sentence)
        {
            var options = new ClassifyOptions
            {
                ModelFilePath = _options.ModelFilePath,
                ModelDir      = _options.ModelDir,
                ModelName     = _options.ModelName
            };

            _classifier.LoadModel(options);

            var classes = _classifier.Classify(sentence, options);

            classes = classes.OrderByDescending(x => x.Item2).ToList();

            return(classes);
        }
Example #17
0
        // Executes the net with the data provided and shows the results
        public void Analyze(object sender, EventArgs e)
        {
            if (!HasAnalysisData)
            {
                MessageBox.Show("Please load analysis data first");
                return;
            }
            if (!HasTrainedClassifier)
            {
                MessageBox.Show("Please train the classifier first");
                return;
            }

            // Normalize the analysis data
            analysisData = analysisData.NormalizeToRange(-1, 1);

            // Analyze the data
            positiveResultData.Samples.Clear( );
            negativeResultData.Samples.Clear( );
            foreach (ISample sample in analysisData.Samples)
            {
                bool isGoodInvestment = classifier.Classify(sample, "good");

                if (isGoodInvestment)
                {
                    positiveResultData.Samples.Add(sample);
                }
                else
                {
                    negativeResultData.Samples.Add(sample);
                }
            }

            // Show results
            form.OverweightList.Items.Clear( );
            foreach (string description in PositiveResultDataDescriptions)
            {
                form.OverweightList.Items.Add(description);
            }
            form.UnderweightList.Items.Clear( );
            foreach (string description in NegativeResultDataDescriptions)
            {
                form.UnderweightList.Items.Add(description);
            }
        }
        public double Evaluate(IEnumerable <ProcessData> testCollection, IModel model, IClassifier classifier)
        {
            int correct = 0;
            int wrong   = 0;

            foreach (var item in testCollection)
            {
                if (classifier.Classify(model.Predict(item.Data)).Equals(item.Label))
                {
                    correct++;
                }
                else
                {
                    wrong++;
                }
            }
            return(((double)correct) / (double)(correct + wrong));
        }
        public int Classify(List <ClassifierInfo> classifiersInfo, Data.Example example)
        {
            IClassifier metaClassifier   = classifiersInfo.Last().Classifier;
            List <int>  predictionValues = new List <int>();

            for (int i = 0; i < classifiersInfo.Count - 1; i++)
            {
                IClassifier classifier = classifiersInfo[i].Classifier;
                int         label      = classifier.Classify(example).Label;
                predictionValues.Add(label);
            }

            Data.Example metaExample = new Data.Example(metaClassifier.Metadata, 0, predictionValues.ToArray(), -1);

            int finalPredction = metaClassifier.Classify(metaExample).Label;

            return(finalPredction);
        }
Example #20
0
        protected override void ProcessMessageImpl(IMessageHeader header, ExtractedFileStatusMessage message, ulong tag)
        {
            bool   isClean = true;
            object resultObject;

            try
            {
                // We should only ever receive messages regarding anonymised images
                if (message.Status != ExtractedFileStatus.Anonymised)
                {
                    throw new ApplicationException($"Received a message with anonymised status of {message.Status}");
                }

                IFileInfo toProcess = _fileSystem.FileInfo.FromFileName(Path.Combine(_extractionRoot, message.ExtractionDirectory, message.OutputFilePath));

                if (!toProcess.Exists)
                {
                    throw new ApplicationException("IsIdentifiable service cannot find file " + toProcess.FullName);
                }

                var result = _classifier.Classify(toProcess);

                foreach (Failure f in result)
                {
                    Logger.Log(LogLevel.Info, $"Validation failed for {f.Resource} Problem Value:{f.ProblemValue}");
                    isClean = false;
                }
                resultObject = result;
            }
            catch (ApplicationException e)
            {
                // Catch specific exceptions we are aware of, any uncaught will bubble up to the wrapper
                ErrorAndNack(header, tag, "Error while processing AnonSuccessMessage", e);
                return;
            }

            _producer.SendMessage(new ExtractedFileVerificationMessage(message)
            {
                IsIdentifiable = !isClean,
                Report         = JsonConvert.SerializeObject(resultObject)
            }, header);

            Ack(header, tag);
        }
        public int Classify(List <ClassifierInfo> classifiersInfo, List <Data.Example> examples)
        {
            IClassifier metaClassifier   = classifiersInfo.Last().Classifier;
            List <int>  predictionValues = new List <int>();

            for (int i = 0; i < classifiersInfo.Count - 1; i++)
            {
                ClassifierInfo info    = classifiersInfo[i];
                Data.Example   example = examples.Find(e => info.Desc.Contains(e.Metadata.DatasetName));

                int prediction = info.Classifier.Classify(example).Label;
                predictionValues.Add(prediction);
            }

            Data.Example metaExample = new Data.Example(null, -1, predictionValues.ToArray(), -1);

            int finalPredction = metaClassifier.Classify(metaExample).Label;

            return(finalPredction);
        }
Example #22
0
        public override Prediction Classify(Data.Instance instance)
        {
            List <int> prediceted = new List <int>();

            Queue <Node> candidates = new Queue <Node>();

            foreach (string child in this._hierarchy.Root.Children)
            {
                candidates.Enqueue(this._hierarchy[child]);
            }

            while (candidates.Count != 0)
            {
                Node        current    = candidates.Dequeue();
                IClassifier classifier = this[current.Name];
                Prediction  prediction = classifier.Classify(instance);


                if (prediction.Label == 0)
                {
                    prediceted.Add(current.ValueIndex);
                    if (current.Children != null)
                    {
                        foreach (string child in current.Children)
                        {
                            candidates.Enqueue(this._hierarchy[child]);
                        }
                    }
                }
            }

            double[] probabilities = new double[instance.Metadata.Target.Values.Length];
            foreach (int index in prediceted)
            {
                probabilities[index] = 1;
            }

            Prediction final = new Prediction(-1, probabilities);

            return(final);
        }
        public EvaluationResult Evaluate(IClassifier classifier, TextDocument[] testSet, string targetTag)
        {
            var results = testSet
                .Select(x => new BusinessObjects.Evaluation
                {
                    DocumentId = x.Id,
                    Result = classifier.Classify(x),
                    ExpectedClass = x.Tags.Contains(targetTag) ? 1 : 0,
                })
                .ToArray();

            var total = results.Length;
            var errors = Get(results, r => r.Result.PredictedClass != r.ExpectedClass);

            var truePositives = Get(results, r => r.Result.PredictedClass == 1 && r.ExpectedClass == 1);
            var falsePositives = Get(results, r => r.Result.PredictedClass == 1 && r.ExpectedClass == 0);
            var falseNegatives = Get(results, r => r.Result.PredictedClass == 0 && r.ExpectedClass == 1);
            var trueNegatives = Get(results, r => r.Result.PredictedClass == 0 && r.ExpectedClass == 0);

            var accuracy = (total - errors.Length) * 1.0 / total;
            var precision = truePositives.Length * 1.0 / (truePositives.Length + falsePositives.Length);
            var recall = truePositives.Length * 1.0 / (truePositives.Length + falseNegatives.Length);
            var fscore = 2.0 * precision * recall / (precision + recall);
            if (double.IsNaN(fscore))
            {
                fscore = 0.0;
            }

            return new EvaluationResult
            {
                Accuracy = accuracy,
                Precision = precision,
                Recall = recall,
                FScore = fscore,
                TruePositives = truePositives,
                FalsePositives = falsePositives,
                TrueNegatives = trueNegatives,
                FalseNegatives = falseNegatives,
                Errors = errors,
            };
        }
Example #24
0
 private void ProcessClassify(object sender, byte[] stream)
 {
     Task.Run(async() => { await defaultClassifier.Classify(stream); });
 }
Example #25
0
 public IEnumerable <ClassificationSpan> Classify(ITextSnapshotLine textLine)
 {
     return(_classifier.Classify(textLine));
 }
 public MarketAction ClassifyRecord(TRecord record)
 {
     return(_classifier.Classify(record, Root));
 }
Example #27
0
        public MainPageViewModel()
        {
            using (var scope = Bootstrapper.Container.BeginLifetimeScope())
            {
                offlineInceptionV3Model = scope.Resolve <IClassifier>();
            }


            TakePhotoCommand = new Command(async() =>
            {
                var photo = await MediaPicker.CapturePhotoAsync();

                var stream = await photo.OpenReadAsync();

                var memoryStream = new MemoryStream();
                await stream.CopyToAsync(memoryStream);

                stream = await photo.OpenReadAsync();

                if (stream != null)
                {
                    Photo = ImageSource.FromStream(() => { return(stream); });
                    Console.WriteLine("Photo was taken");
                }
                else
                {
                    Console.WriteLine("Photo wasn't taken");
                }

                byte[] bytesArray = memoryStream.ToArray();

                offlineInceptionV3Model.ClassificationCompleted += Classifier_ClassificationCompleted;

                await offlineInceptionV3Model.Classify(bytesArray);
            });

            PickPhotoCommand = new Command(async() =>
            {
                var photo = await MediaPicker.PickPhotoAsync();

                var stream = await photo.OpenReadAsync();

                var memoryStream = new MemoryStream();
                await stream.CopyToAsync(memoryStream);

                stream = await photo.OpenReadAsync();

                if (stream != null)
                {
                    Photo = ImageSource.FromStream(() => { return(stream); });
                    Console.WriteLine("Photo was picked");
                }
                else
                {
                    Console.WriteLine("Photo wasn't picked");
                }

                byte[] bytesArray = memoryStream.ToArray();

                offlineInceptionV3Model.ClassificationCompleted += Classifier_ClassificationCompleted;

                await offlineInceptionV3Model.Classify(bytesArray);
            });
        }