コード例 #1
0
            private PointsOfInterestClassifier Learn(string sourcePath, string truthPath)
            {
                this.WriteLine(null, "Learning...");

                PointsOfInterestClassifier classifier = new PointsOfInterestClassifier();

                using (DirectoryDataProvider dataProvider = new DirectoryDataProvider())
                {
                    dataProvider.Add(sourcePath, false, truthPath, "#Class");

                    ClassifierProgress <TestImage> progress = new ClassifierProgress <TestImage>(
                        (source, index) =>
                    {
                        this.StopwatchRestart();
                    },
                        (source, index, answer, exception) =>
                    {
                        long duration = this.StopwatchStop();
                        Interlocked.Increment(ref this.totalImages);

                        this.Write(
                            null,
                            "({0})\tFile: {1} ... ",
                            index,
                            source.SourceId.ToFileName(false));

                        if (exception != null)
                        {
                            this.WriteLine(null, "ERROR.");
                            this.WriteException(null, exception);
                        }
                        else
                        {
                            this.WriteLine(null, "OK ({0} ms)", duration);
                        }
                    });

                    classifier.Train <TestImage>(
                        dataProvider.Generate(null),
                        (x, cancellationToken) => new ImageSource(x.SourceId, x.Image),
                        x => string.Concat(x.Labels),
                        progress,
                        CancellationToken.None);
                }

                return(classifier);
            }
コード例 #2
0
            private void Test(PointsOfInterestClassifier classifier, string sourcePath, string truthPath)
            {
                this.WriteLine(null, "Testing...");

                List <ClassificationResult <string> > results = new List <ClassificationResult <string> >();

                using (DirectoryDataProvider dataProvider = new DirectoryDataProvider())
                {
                    dataProvider.Add(sourcePath, false, truthPath, "#Class");

                    ClassifierProgress <TestImage> progress = new ClassifierProgress <TestImage>(
                        (source, index) =>
                    {
                        this.StopwatchRestart();
                    },
                        (source, index, answer, exception) =>
                    {
                        long duration = this.StopwatchStop();
                        Interlocked.Increment(ref this.totalImages);

                        this.Write(
                            null,
                            "({0})\tFile: {1} ... ",
                            index,
                            source.SourceId.ToFileName(false));

                        if (exception != null)
                        {
                            this.WriteLine(null, "ERROR.");
                            this.WriteException(null, exception);
                        }
                        else
                        {
                            this.WriteLine(
                                null,
                                "OK ({0} ms) {1} {2:F4}",
                                duration,
                                answer?.ClassName ?? string.Empty,
                                answer?.Confidence ?? 0.0);
                        }
                    });

                    foreach ((TestImage image, Answer answer) in classifier.Classify <TestImage>(
                                 dataProvider.Generate(null),
                                 (x, cancellationToken) => new ImageSource(x.SourceId, x.Image),
                                 progress,
                                 CancellationToken.None))
                    {
                        results.Add(new ClassificationResult <string>(
                                        answer.Id,
                                        answer.ClassName,
                                        string.Concat(image.Labels),
                                        answer.Confidence,
                                        answer.Confidence >= 0.2f));
                    }
                }

                // write report
                ClassificationReport <string> testReport = new ClassificationReport <string>(results);

                using (StreamWriter outputFile = new StreamWriter(Console.OpenStandardOutput()))
                ////using (StreamWriter outputFile = File.CreateText(this.options.OutputFileName))
                {
                    ClassificationReportWriter <string> .WriteReport(outputFile, testReport, ClassificationReportMode.All);
                }
            }