예제 #1
0
        private void button5_Click(object sender, EventArgs e)
        {
            object selectedValue = itemsource[0];

            if (selectedValue != null)
            {
                Type selectedType = (Type)selectedValue;
                currFeatDisplay = Activator.CreateInstance(selectedType) as IFeatureDisplay;
                Type currFeatType = featTypeByDisplay[selectedType];
                selitem = extractorsByFeatType[currFeatType];
            }
            object selvalue = selitem[0];

            if (selvalue != null)
            {
                Type selType = (Type)selvalue;
                currExtractor = Activator.CreateInstance(selType) as IFeatureExtractor;
            }
            if (img == null)
            {
                MessageBox.Show("You must select an image!", "Displaying error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            pictureBox5.Image = img.Clone() as Bitmap;
            Graphics g = Graphics.FromImage(pictureBox5.Image);
            {
                var features = currExtractor.ExtractFeatures(img);
                currFeatDisplay.Show(features, g);
                button6.Visible = true;
                //groupBox6.Visible = true;
            }
        }
        public string Teach(AlgorithmDataModel teachingData)
        {
            DataSet dataSet;
            var files = teachingData.Files.Select(i => teachingData.Directory + i).ToArray();
            var forgeries = teachingData.Forgeries?.Select(i => teachingData.ForgeriesDirectory + i).ToArray();
            switch (teachingData.FeatureAlgorithmName)
            {
                case "RadonTransformFeatureExtractor":
                    _extractor = new RadonTransformFeatureExtractor(int.Parse(teachingData.FeatureAlgorithmParameters[0]));
                    dataSet = _extractor.GetTeachingDataset(files, forgeries);
                    break;
                default:
                    throw new ArgumentException("FeatureAlgorithmName");
            }

            switch (teachingData.ComputingAlgorithmName)
            {
                case "NeuralNetworkBp":
                    _engine = new NeuralNetworkBp(double.Parse(teachingData.ComputingAlgorithmParameters[0].Replace(".",",")),
                        double.Parse(teachingData.ComputingAlgorithmParameters[1].Replace(".", ",")));
                    _engine.Train(dataSet);
                    break;
                case "EncogNeuralNetwork":
                    _engine = new EncogNeuralNetwork(double.Parse(teachingData.ComputingAlgorithmParameters[0].Replace(".", ",")),
                        double.Parse(teachingData.ComputingAlgorithmParameters[1].Replace(".", ",")));
                    _engine.Train(dataSet);
                    break;
                default:
                    throw new ArgumentException("ComputingAlgorithmName");
            }

            return "OK";
        }
예제 #3
0
        private void cbxFeatureExtractors_SelectedValueChanged(object sender, EventArgs e)
        {
            object selectedValue = ((ComboBox)sender).SelectedItem;

            if (selectedValue != null)
            {
                Type selectedType = (Type)selectedValue;
                currExtractor = Activator.CreateInstance(selectedType) as IFeatureExtractor;
            }
        }
 private static IDistribution<string> CreateModel(IFeatureExtractor<string, Tuple<string, int>> featureExtractor, string document)
 {
     var features = featureExtractor.GetFeatures(document);
     var bag = new Bag<string>();
     foreach (var feature in features)
     {
         bag.Add(feature.Item1, feature.Item2);
     }
     var distribution = new Distribution<string>(bag);
     return distribution;
 }
예제 #5
0
 public PicklerApplication(
     IArgumentParser argumentParser,
     IFileLoader fileLoader,
     IFeatureExtractor featureExtractor,
     ITrxResultsParser trxResultsParser,
     IResultRenderer resultRenderer)
 {
     _argumentParser   = argumentParser;
     _fileLoader       = fileLoader;
     _featureExtractor = featureExtractor;
     _trxResultsParser = trxResultsParser;
     _resultRenderer   = resultRenderer;
 }
        private static IDistribution <string> CreateModel(IFeatureExtractor <string, Tuple <string, int> > featureExtractor, string document)
        {
            var features = featureExtractor.GetFeatures(document);
            var bag      = new Bag <string>();

            foreach (var feature in features)
            {
                bag.Add(feature.Item1, feature.Item2);
            }
            var distribution = new Distribution <string>(bag);

            return(distribution);
        }
        public static bool TryGetFeatureExtractor(Type modelType, out IFeatureExtractor featureExtractor)
        {
            featureExtractor = null;

            Type featureExtractorType;

            if (_modelTypeFeatureExtractorType.TryGetValue(modelType, out featureExtractorType))
            {
                featureExtractor = Activator.CreateInstance(featureExtractorType) as IFeatureExtractor;
            }

            return(featureExtractor != null);
        }
        public override string GetDetails(int indentLevel)
        {
            string indent = "";

            for (int i = 0; i < indentLevel; ++i)
            {
                indent += "\t";
            }

            IFeatureExtractor externalFeatureExtractor = InitializeExternalFeatureExtractor(typeof(TimeSliceDCM));

            return(base.GetDetails(indentLevel) + Environment.NewLine +
                   indent + "Time slice hours:  " + _timeSliceHours + Environment.NewLine +
                   indent + "Time slices per period:  " + _periodTimeSlices + Environment.NewLine +
                   indent + "External feature extractor (" + typeof(TimeSliceDCM) + "):  " + (externalFeatureExtractor == null ? "None" : externalFeatureExtractor.GetDetails(indentLevel + 1)));
        }
예제 #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Document"/> class.
 /// </summary>
 /// <param name="sentences">The sentences.</param>
 /// <param name="tokens">The tokens.</param>
 /// <param name="originalText">The original text.</param>
 /// <param name="featureExtractor">The feature extractor.</param>
 /// <param name="textSummarizer">The text summarizer.</param>
 /// <param name="tokenizer">The tokenizer.</param>
 /// <param name="tokenizerLanguage">The tokenizer language.</param>
 public Document(
     Sentence[] sentences,
     Token[] tokens,
     string originalText,
     IFeatureExtractor featureExtractor,
     ITextSummarizer textSummarizer,
     ITokenizer tokenizer,
     TokenizerLanguage tokenizerLanguage)
 {
     Sentences         = sentences;
     OriginalText      = originalText;
     Tokens            = tokens;
     TextSummarizer    = textSummarizer;
     FeatureExtractor  = featureExtractor;
     TokenizerLanguage = tokenizerLanguage;
     Tokenizer         = tokenizer;
     ID = Guid.NewGuid();
 }
예제 #10
0
            public TestContext()
            {
                var givenSectionExtractor = new GivenExtractor(new GivenParser());
                var whenSectionExtractor  = new WhenExtractor(new WhenParser());
                var thenSectionExtractor  = new ThenExtractor(new ThenParser());
                var tagParser             = new TagParser();
                var outlineParser         = new OutlineParser();
                var scenarioExtractor     = new ScenarioExtractor(
                    givenSectionExtractor,
                    whenSectionExtractor,
                    thenSectionExtractor,
                    outlineParser,
                    tagParser);

                _sut = new FeatureExtractor(
                    new FeatureParser(tagParser),
                    scenarioExtractor);
            }
예제 #11
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            // Read the Code Project article linked in README.md to figure out how to get the URL and API Key for this application
            if (!File.Exists("api.url") || string.IsNullOrEmpty(endpoint = File.ReadAllText("api.url")))
            {
                endpoint = ShowDialog("Please enter the endpoint of your Azure Web Service. ", "Language Detector");
                File.WriteAllText("api.url", endpoint);
            }
            if (!File.Exists("api.key") || string.IsNullOrEmpty(apikey = File.ReadAllText("api.key")))
            {
                apikey = ShowDialog("Please enter the API Key for your Azure Web Service. ", "Language Detector");
                File.WriteAllText("api.key", apikey);
            }

            // Initialize our chosen feature extractor and classifier
            fe = new LetterGroupFeatureExtractor(ngram);
            cs = new AzureClassifier(fe, endpoint, apikey);
            sp = new CodeSnippetParser();

            // Create a training set off of a lot of code samples
            // ExtractTrainingSetFromFolder();
        }
        public new static IEnumerable <Feature> GetAvailableFeatures(Area area)
        {
            foreach (Feature f in FeatureBasedDCM.GetAvailableFeatures(area))
            {
                yield return(f);
            }

            foreach (TimeSliceFeature f in Enum.GetValues(typeof(TimeSliceFeature)))
            {
                yield return(new Feature(typeof(TimeSliceFeature), f, null, null, f.ToString(), null));
            }

            IFeatureExtractor externalFeatureExtractor = InitializeExternalFeatureExtractor(typeof(TimeSliceDCM));

            if (externalFeatureExtractor != null)
            {
                foreach (Feature f in externalFeatureExtractor.GetAvailableFeatures(area))
                {
                    yield return(f);
                }
            }
        }
예제 #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Pipeline"/> class.
 /// </summary>
 /// <param name="normalizerManager">The normalizer manager.</param>
 /// <param name="pOSTagger">The p os tagger.</param>
 /// <param name="sentenceDetector">The sentence detector.</param>
 /// <param name="stemmer">The stemmer.</param>
 /// <param name="stopWordsManager">The stop words manager.</param>
 /// <param name="tokenizer">The tokenizer.</param>
 /// <param name="featureExtractor">The feature extractor.</param>
 /// <param name="textSummarizer">The text summarizer.</param>
 /// <param name="entityFinder">The entity finder.</param>
 /// <exception cref="ArgumentNullException">
 /// normalizerManager or pOSTagger or sentenceDetector or stemmer or stopWordsManager or
 /// tokenizer or featureExtractor or textSummarizer
 /// </exception>
 public Pipeline(
     INormalizerManager normalizerManager,
     IPOSTagger pOSTagger,
     ISentenceDetector sentenceDetector,
     IStemmer stemmer,
     IStopWordsManager stopWordsManager,
     ITokenizer tokenizer,
     IFeatureExtractor featureExtractor,
     ITextSummarizer textSummarizer,
     IEntityFinder entityFinder)
 {
     NormalizerManager = normalizerManager ?? throw new ArgumentNullException(nameof(normalizerManager));
     POSTagger         = pOSTagger ?? throw new ArgumentNullException(nameof(pOSTagger));
     SentenceDetector  = sentenceDetector ?? throw new ArgumentNullException(nameof(sentenceDetector));
     Stemmer           = stemmer ?? throw new ArgumentNullException(nameof(stemmer));
     StopWordsManager  = stopWordsManager ?? throw new ArgumentNullException(nameof(stopWordsManager));
     Tokenizer         = tokenizer ?? throw new ArgumentNullException(nameof(tokenizer));
     FeatureExtractor  = featureExtractor ?? throw new ArgumentNullException(nameof(featureExtractor));
     TextSummarizer    = textSummarizer ?? throw new ArgumentNullException(nameof(textSummarizer));
     EntityFinder      = entityFinder ?? throw new ArgumentNullException(nameof(entityFinder));
     SetLanguage(Languages.English);
 }
예제 #14
0
        public void createCSVDataset(IFeatureExtractor featureExtractor, string directoryPath, string destinationCSVFilePath)
        {
            StringBuilder csvDatasetHeader = new StringBuilder();
            StringBuilder csvDataset       = new StringBuilder();
            string        delimiter        = ",";
            bool          headerBuilt      = false;
            int           counter          = 0;

            Dictionary <string, double> fileFeatures;

            if (Directory.Exists(directoryPath))
            {
                foreach (string filePath in Directory.GetFiles(directoryPath))
                {
                    fileFeatures = featureExtractor.extractFeatures(filePath);
                    csvDataset.Append(filePath).Append(delimiter);
                    if (!headerBuilt)
                    {
                        csvDatasetHeader.Append("File path").Append(delimiter);
                    }
                    foreach (KeyValuePair <string, double> feature in fileFeatures.OrderBy(x => x.Key))
                    {
                        csvDataset.Append(feature.Value).Append(delimiter);

                        if (!headerBuilt)
                        {
                            csvDatasetHeader.Append(feature.Key).Append(delimiter);
                        }
                    }

                    csvDataset.Append("\n");
                    headerBuilt = true;
                    Console.WriteLine(String.Format("{0}. {1}", ++counter, filePath));
                }
            }
            writeCSVFile(csvDatasetHeader.Append("\n").Append(csvDataset).ToString(), destinationCSVFilePath);
        }
예제 #15
0
        public string Teach(AlgorithmDataModel teachingData)
        {
            DataSet dataSet;
            var     files     = teachingData.Files.Select(i => teachingData.Directory + i).ToArray();
            var     forgeries = teachingData.Forgeries?.Select(i => teachingData.ForgeriesDirectory + i).ToArray();

            switch (teachingData.FeatureAlgorithmName)
            {
            case "RadonTransformFeatureExtractor":
                _extractor = new RadonTransformFeatureExtractor(int.Parse(teachingData.FeatureAlgorithmParameters[0]));
                dataSet    = _extractor.GetTeachingDataset(files, forgeries);
                break;

            default:
                throw new ArgumentException("FeatureAlgorithmName");
            }

            switch (teachingData.ComputingAlgorithmName)
            {
            case "NeuralNetworkBp":
                _engine = new NeuralNetworkBp(double.Parse(teachingData.ComputingAlgorithmParameters[0].Replace(".", ",")),
                                              double.Parse(teachingData.ComputingAlgorithmParameters[1].Replace(".", ",")));
                _engine.Train(dataSet);
                break;

            case "EncogNeuralNetwork":
                _engine = new EncogNeuralNetwork(double.Parse(teachingData.ComputingAlgorithmParameters[0].Replace(".", ",")),
                                                 double.Parse(teachingData.ComputingAlgorithmParameters[1].Replace(".", ",")));
                _engine.Train(dataSet);
                break;

            default:
                throw new ArgumentException("ComputingAlgorithmName");
            }

            return("OK");
        }
예제 #16
0
        private void Init(Question question, List<Question> neighbours)
        {
            featureExtractor = new RandomFeatureExtractor();
            MathNet.Numerics.Distributions.ContinuousUniform normal = new MathNet.Numerics.Distributions.ContinuousUniform();

            tagSimWeights = (Vector)Vector.Build.Random(TAGFEATURENUM,normal);
            questionSimWeights = (Vector)Vector.Build.Random(QUESTIONFEATURENUM,normal);

            tagSimFeatures = featureExtractor.ExtractTagSim(candidates.Keys.ToList());
            questionSimFeatures = featureExtractor.ExtractQuestionSim(question, neighbours);
            tagSim = new DenseMatrix(candidates.Count, candidates.Count);
            questionsim = new DenseVector(neighbours.Count);
        }
예제 #17
0
        private static void DoCrossValidation(string folder, string[] trainingSet, string[] testSet, IFeatureExtractor[] featureExtractors,
                                            out double[] testErrors, out IFeatureExtractor bestExtractor, out double bestError)
        {
            testErrors = new double[featureExtractors.Length];
            bestExtractor = null;
            bestError = 2.0;
            for (var i = 0; i < featureExtractors.Length; i++)
            {
                var extractor = featureExtractors[i];
                double learningError;
                var classifier = TrainClassifier(folder, trainingSet, extractor, out learningError);
                var testError = EvaluateClassifier(folder, testSet, classifier);
                testErrors[i] = testError;

                if (testError < bestError)
                {
                    bestError = testError;
                    bestExtractor = extractor;
                }
            }
        }
예제 #18
0
 public Classifier(IFeatureExtractor featureExtractor, IPredictor predictor)
 {
     FeatureExtractor = featureExtractor;
     Predictor        = predictor;
 }
        protected override IEnumerable <FeatureVectorList> ExtractFeatureVectors(Prediction prediction, bool training, DateTime start, DateTime end)
        {
            Dictionary <TimeSliceFeature, string> featureId = new Dictionary <TimeSliceFeature, string>();

            foreach (Feature feature in Features.Where(f => f.EnumType == typeof(TimeSliceFeature)))
            {
                featureId.Add((TimeSliceFeature)feature.EnumValue, feature.Id);
            }

            List <TimeSliceFeature> threeHourIntervals = new List <TimeSliceFeature>(new TimeSliceFeature[] { TimeSliceFeature.LateNight, TimeSliceFeature.EarlyMorning, TimeSliceFeature.Morning, TimeSliceFeature.MidMorning, TimeSliceFeature.Afternoon, TimeSliceFeature.MidAfternoon, TimeSliceFeature.Evening, TimeSliceFeature.Night });

            int processorCount = Configuration.ProcessorCount;

            Configuration.ProcessorCount = 1; // all sub-threads (e.g., those in FeatureBasedDCM) should use 1 core, since we're multi-threading here
            Set <Thread>             threads      = new Set <Thread>(processorCount);
            long                     firstSlice   = (long)((training ? prediction.Model.TrainingStart.Ticks : prediction.PredictionStartTime.Ticks) / _timeSliceTicks);
            long                     lastSlice    = (long)((training ? prediction.Model.TrainingEnd.Ticks : prediction.PredictionEndTime.Ticks) / _timeSliceTicks);
            long                     ticksPerHour = new TimeSpan(1, 0, 0).Ticks;
            List <FeatureVectorList> completeFeatureVectorLists     = new List <FeatureVectorList>();
            List <FeatureVectorList> incompleteFeatureVectorLists   = new List <FeatureVectorList>();
            AutoResetEvent           emitCompleteFeatureVectorLists = new AutoResetEvent(false);
            IFeatureExtractor        externalFeatureExtractor       = InitializeExternalFeatureExtractor(typeof(TimeSliceDCM));

            for (int i = 0; i < processorCount; ++i)
            {
                Thread t = new Thread(new ParameterizedThreadStart(core =>
                {
                    for (long slice = firstSlice + (int)core; slice <= lastSlice; slice += processorCount)
                    {
                        Console.Out.WriteLine("Processing slice " + (slice - firstSlice + 1) + " of " + (lastSlice - firstSlice + 1));

                        DateTime sliceStart = new DateTime(slice * _timeSliceTicks);
                        DateTime sliceEnd   = sliceStart.Add(new TimeSpan(_timeSliceTicks - 1));
                        DateTime sliceMid   = new DateTime((sliceStart.Ticks + sliceEnd.Ticks) / 2L);

                        #region get interval features that are true for all points in the current slice
                        Dictionary <NumericFeature, int> threeHourIntervalFeatureValue = new Dictionary <NumericFeature, int>();
                        int startingThreeHourInterval = sliceStart.Hour / 3;                                                      // which 3-hour interval does the current slice start in?
                        int threeHourIntervalsTouched = (int)(((sliceEnd.Ticks - sliceStart.Ticks) / ticksPerHour) / 3) + 1;      // how many 3-hour intervals does the current slice touch?
                        int endingThreeHourInterval   = startingThreeHourInterval + threeHourIntervalsTouched - 1;                // which 3-hour interval does the current slice end in?
                        for (int k = 0; k < threeHourIntervals.Count; ++k)
                        {
                            TimeSliceFeature threeHourInterval = threeHourIntervals[k];
                            string id;
                            if (featureId.TryGetValue(threeHourInterval, out id))      // if the current model uses the current 3-hour interval as a feature
                            {
                                bool covered = false;
                                for (int interval = startingThreeHourInterval; !covered && interval <= endingThreeHourInterval; ++interval)
                                {
                                    if (interval % 8 == k)
                                    {
                                        covered = true;
                                    }
                                }

                                threeHourIntervalFeatureValue.Add(IdNumericFeature[id], covered ? 1 : 0);
                            }
                        }
                        #endregion

                        #region extract feature vectors
                        foreach (FeatureVectorList featureVectors in base.ExtractFeatureVectors(prediction, training, sliceStart, sliceEnd))
                        {
                            if (!featureVectors.Complete)
                            {
                                throw new Exception("Incomplete feature vectors received from base class extractor");
                            }

                            Console.Out.WriteLine("Extracting " + featureId.Count + " time slice features for " + featureVectors.Count + " points.");

                            foreach (FeatureVector featureVector in featureVectors)
                            {
                                Point point = featureVector.DerivedFrom as Point;

                                if (point.Time == DateTime.MinValue)
                                {
                                    point.Time = sliceMid;
                                }
                                else if ((long)(point.Time.Ticks / _timeSliceTicks) != slice)
                                {
                                    throw new Exception("Point should not be in slice:  " + point);
                                }

                                foreach (LAIR.MachineLearning.NumericFeature threeHourIntervalFeature in threeHourIntervalFeatureValue.Keys)
                                {
                                    featureVector.Add(threeHourIntervalFeature, threeHourIntervalFeatureValue[threeHourIntervalFeature]);
                                }

                                double percentThroughPeriod = (slice % _periodTimeSlices) / (double)(_periodTimeSlices - 1);
                                double radians = 2 * Math.PI * percentThroughPeriod;

                                foreach (TimeSliceFeature feature in featureId.Keys)
                                {
                                    if (feature == TimeSliceFeature.CosinePeriodPosition)
                                    {
                                        featureVector.Add(IdNumericFeature[featureId[feature]], Math.Cos(radians));
                                    }
                                    else if (feature == TimeSliceFeature.SinePeriodPosition)
                                    {
                                        featureVector.Add(IdNumericFeature[featureId[feature]], Math.Sin(radians));
                                    }
                                }
                            }

                            if (externalFeatureExtractor == null)
                            {
                                lock (completeFeatureVectorLists)
                                {
                                    completeFeatureVectorLists.Add(featureVectors);
                                    emitCompleteFeatureVectorLists.Set();
                                }
                            }
                            else
                            {
                                foreach (FeatureVectorList externalFeatureVectors in externalFeatureExtractor.ExtractFeatures(prediction, featureVectors, training, sliceStart, sliceEnd, false))
                                {
                                    if (externalFeatureVectors.Complete)
                                    {
                                        lock (completeFeatureVectorLists)
                                        {
                                            completeFeatureVectorLists.Add(externalFeatureVectors);
                                            emitCompleteFeatureVectorLists.Set();
                                        }
                                    }
                                    else
                                    {
                                        lock (incompleteFeatureVectorLists)
                                            incompleteFeatureVectorLists.Add(externalFeatureVectors);
                                    }
                                }
                            }
                        }
                        #endregion
                    }

                    lock (threads)
                        threads.Remove(Thread.CurrentThread);

                    emitCompleteFeatureVectorLists.Set();
                }));

                lock (threads) { threads.Add(t); }
                t.Start(i);
            }

            while (emitCompleteFeatureVectorLists.WaitOne())
            {
                lock (completeFeatureVectorLists)
                {
                    foreach (FeatureVectorList completeFeatureVectors in completeFeatureVectorLists)
                    {
                        yield return(completeFeatureVectors);
                    }

                    completeFeatureVectorLists.Clear();
                }

                lock (threads)
                    if (threads.Count == 0)
                    {
                        break;
                    }
            }

            // emit any remaining completed vectors, which might have arrived just before the last thread was removed (breaking out of the loop above)
            foreach (FeatureVectorList completeFeatureVectors in completeFeatureVectorLists)
            {
                yield return(completeFeatureVectors);
            }
            completeFeatureVectorLists.Clear();

            Configuration.ProcessorCount = processorCount;  // reset system-wide processor count since we're done with threads here

            foreach (FeatureVectorList incompleteFeatureVectors in incompleteFeatureVectorLists)
            {
                foreach (FeatureVectorList externalFeatureVectors in externalFeatureExtractor.ExtractFeatures(prediction, incompleteFeatureVectors, training, start, end, true))
                {
                    yield return(externalFeatureVectors);
                }
            }
        }
예제 #20
0
 static void ExtractAndDump(short[] signal, IFeatureExtractor featureExtractor, string folder, string fileName)
 {
     var feature = featureExtractor.Extract(signal);
     var dump = new WavFileInfo(DefaultSampleRate, feature);
     var filePath = string.Concat(folder, fileName, featureExtractor.FeatureName, ".wav");
     dump.Write(filePath);
 }
예제 #21
0
        private static SignalClassifier TrainClassifier(string folder, string[] trainingSet, IFeatureExtractor[] featureExtractors, out double learningError)
        {
            var classifier = new SignalClassifier(featureExtractors);
            foreach (var fileName in trainingSet)
            {
                var wavFile = WavFileInfo.Read(string.Concat(folder, fileName, ".wav"));
                var markerFile = MarkerFileInfo.Read(string.Concat(folder, fileName, ".mrk"));

                classifier.AddTrainingSignal(wavFile.Data, markerFile.Decompress(wavFile.Data.Length));
            }
            classifier.Learn(out learningError);
            return classifier;
        }
        protected override int GetNumFeaturesExtractedFor(Prediction prediction)
        {
            IFeatureExtractor externalFeatureExtractor = InitializeExternalFeatureExtractor(typeof(TimeSliceDCM));

            return(base.GetNumFeaturesExtractedFor(prediction) + Features.Where(f => f.EnumType == typeof(TimeSliceFeature)).Count() + (externalFeatureExtractor == null ? 0 : externalFeatureExtractor.GetNumFeaturesExtractedFor(prediction, typeof(TimeSliceDCM))));
        }
예제 #23
0
        private async Task <Stream> Pixelate(SKBitmap bitmap, SKEncodedOrigin origin, IFeatureExtractor featureExtractor)
        {
            // fix orientation if encoded origin is not TopLeft/Default
            bitmap = FixOrientation(bitmap, origin);

            // a surface is something like a table we need to actually draw stuff
            var surface = SKSurface.Create(new SKImageInfo(bitmap.Width, bitmap.Height));
            // get the canvas where we actually draw onto
            var canvas = surface.Canvas;

            canvas.Clear(SKColors.Transparent);
            // apply the original image to the canvas
            canvas.DrawBitmap(bitmap, 0, 0);

            // extract the image features
            switch (FaceProcessing)
            {
            case FaceProcessing.PixelateFaces:
            {
                var faces = (await featureExtractor.ExtractFacesAsync()).ToArray();
                Logger.OnExtractedFaces(_imagePath, faces);
                foreach (var face in faces)
                {
                    if (FaceOutline != null)
                    {
                        Pixelate(canvas, SKRectI.Create(face.X, face.Y, face.Width, face.Height), bitmap, FaceOutline,
                                 PixelSizeFunction);
                    }
                    else
                    {
                        Pixelate(canvas, SKRectI.Create(face.X, face.Y, face.Width, face.Height), bitmap,
                                 PixelSizeFunction);
                    }

                    Logger.OnPixelatedFace(_imagePath, face);
                }

                break;
            }

            case FaceProcessing.PixelatePersons:
            {
                var persons = (await featureExtractor.ExtractPersonsAsync()).ToArray();
                Logger.OnExtractedPersons(_imagePath, persons);
                foreach (var person in persons)
                {
                    if (FaceOutline != null)
                    {
                        Pixelate(canvas, SKRectI.Create(person.X, person.Y, person.Width, person.Height), bitmap,
                                 FaceOutline, PixelSizeFunction);
                    }
                    else
                    {
                        Pixelate(canvas, SKRectI.Create(person.X, person.Y, person.Width, person.Height), bitmap,
                                 PixelSizeFunction);
                    }

                    Logger.OnPixelatedPerson(_imagePath, person);
                }

                break;
            }

            case FaceProcessing.Skip:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            switch (CarProcessing)
            {
            case CarProcessing.PixelatePlatesAndTextOnCars:
            {
                var text = (await featureExtractor.ExtractTextAsync()).ToArray();
                Logger.OnExtractedText(_imagePath, text);
                var cars = (await featureExtractor.ExtractCarsAsync()).ToArray();
                Logger.OnExtractedCars(_imagePath, cars);
                var licensePlates = (await featureExtractor.ExtractLicensePlatesAsync()).ToArray();
                Logger.OnExtractedLicensePlates(_imagePath, licensePlates);

                var mergeDistance = (int)(bitmap.Width * MergeFactor);
                var merged        = ImagePatchClusterizer.Clusterize(text, mergeDistance).ToArray();

                foreach (var car in cars)
                {
                    foreach (var patch in merged)
                    {
                        // If patch is inside of the car borders
                        if (patch.Y >= car.Y &&
                            patch.X >= car.X &&
                            patch.Y <= car.Y + car.Height &&
                            patch.X <= car.X + car.Width &&
                            patch.Width <= car.Width + car.X - patch.X &&
                            patch.Height <= car.Height + car.Y - patch.Y)
                        {
                            if (PlateOutline != null)
                            {
                                Pixelate(canvas, SKRectI.Create(patch.X, patch.Y, patch.Width, patch.Height),
                                         bitmap, PlateOutline, PixelSizeFunction);
                            }
                            else
                            {
                                Pixelate(canvas, SKRectI.Create(patch.X, patch.Y, patch.Width, patch.Height),
                                         bitmap, PixelSizeFunction);
                            }

                            Logger.OnPixelatedText(_imagePath, patch);
                        }
                    }
                }

                foreach (var plate in licensePlates)
                {
                    if (PlateOutline != null)
                    {
                        Pixelate(canvas, SKRectI.Create(plate.X, plate.Y, plate.Width, plate.Height), bitmap,
                                 PlateOutline, PixelSizeFunction);
                    }
                    else
                    {
                        Pixelate(canvas, SKRectI.Create(plate.X, plate.Y, plate.Width, plate.Height), bitmap,
                                 PixelSizeFunction);
                    }

                    Logger.OnPixelatedLicensePlate(_imagePath, plate);
                }

                break;
            }

            case CarProcessing.PixelateCars:
            {
                var cars = (await featureExtractor.ExtractCarsAsync()).ToArray();
                Logger.OnExtractedCars(_imagePath, cars);
                foreach (var car in cars)
                {
                    if (PlateOutline != null)
                    {
                        Pixelate(canvas, SKRectI.Create(car.X, car.Y, car.Width, car.Height), bitmap, PlateOutline,
                                 PixelSizeFunction);
                    }
                    else
                    {
                        Pixelate(canvas, SKRectI.Create(car.X, car.Y, car.Width, car.Height), bitmap,
                                 PixelSizeFunction);
                    }

                    Logger.OnPixelatedCar(_imagePath, car);
                }

                break;
            }

            case CarProcessing.Skip:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(surface.Snapshot().Encode(OutputFormat, OutputQuality).AsStream());
        }
예제 #24
0
 private static SignalClassifier TrainClassifier(string folder, string[] trainingSet, IFeatureExtractor featureExtractor, out double learningError)
 {
     var featureExtractors = new[] { featureExtractor };
     return TrainClassifier(folder, trainingSet, featureExtractors, out learningError);
 }
예제 #25
0
 public AzureClassifier(IFeatureExtractor featureExtractor, string endpoint, string apikey)
 {
     fe            = featureExtractor;
     azureEndpoint = endpoint;
     azureApiKey   = apikey;
 }
        public static bool TryGetFeatureExtractor(Type modelType, out IFeatureExtractor featureExtractor)
        {
            featureExtractor = null;

            Type featureExtractorType;
            if (_modelTypeFeatureExtractorType.TryGetValue(modelType, out featureExtractorType))
                featureExtractor = Activator.CreateInstance(featureExtractorType) as IFeatureExtractor;

            return featureExtractor != null;
        }
 public SignalClassifier(IFeatureExtractor[] featureExtractors)
 {
     this.featureExtractors = featureExtractors;
     trainingExamples = new List<TrainingExample>();
 }
예제 #28
0
 public GestureClassifier(IFeatureExtractor extractor)
 {
     trainingSet = new List<Gesture>();
     FeatureExtractor = extractor;
 }
예제 #29
0
 private static void ExtractAll(string folder, string[] fileNames, IFeatureExtractor[] featureExtractors)
 {
     foreach (var fileName in fileNames)
     {
         var wavFile = WavFileInfo.Read(string.Concat(folder, fileName, ".wav"));
         if (wavFile.SampleRate != DefaultSampleRate)
         {
             throw new Exception(string.Format("File {0} has non-default sample rate", fileName));
         }
         foreach (var featureExtractor in featureExtractors)
         {
             ExtractAndDump(wavFile.Data, featureExtractor, folder, fileName);
         }
     }
 }
예제 #30
0
 private static void DumpCrossValidationErrors(string folder, IFeatureExtractor[] extractors, double[] testErrors)
 {
     using (var writer = new StreamWriter(string.Concat(folder, extractors[0].FeatureName, ".txt")))
     {
         writer.WriteLine("Cross-Validation errors:");
         for (var i = 0; i < testErrors.Length; i++)
         {
             writer.WriteLine("{0} {1}", extractors[i].ParamValue, testErrors[i]);
         }
     }
 }
예제 #31
0
 public void TrainingInstancesInit(List<Question> questions, List<List<Question>> questionNeighbours)
 {
     this.questions = questions;
     this.questionNeighbours = questionNeighbours;
     featureExtractor = new GoldenFeatureExtractor();
     for (int i = 0; i < questions.Count;i++ )
     {
         featureExtractor.GetTruth(questions[i].RelatedTags);
         ConstructCandidateTable(questionNeighbours[i]);
         SampleUnrelatedTag(questions[i],questionNeighbours[i]);
         ComputeQuestionSimFeature(questions[i],questionNeighbours[i]);
         ComputeTagSimFeature(instanceCandidates[instanceCandidates.Count-1]);
     }
 }