示例#1
0
            private void Test()
            {
                ClassificationNetwork network = ClassificationNetwork.FromFile(this.options.NetworkFileName);

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

                using (TestImageProvider <string> dataProvider = this.options.CreateTestImageProvider(network))
                {
                    ////Context model = Context.FromRegex(@"\d{1,5}", CultureInfo.InvariantCulture);

                    ////int n = 0;
                    foreach (TestImage sample in dataProvider.Generate(network.AllowedClasses))
                    {
                        Interlocked.Increment(ref this.totalImages);

                        ////sample.Image.Save("e:\\temp\\" + sample.Label + "_" + n.ToString(CultureInfo.InvariantCulture) + ".bmp");
                        ////n++;

                        ////if (n < 171) continue;

                        this.localTimeCounter.Restart();

                        Tensor x = ImageExtensions.FromImage(
                            sample.Image,
                            null,
                            Shape.BWHC,
                            network.InputShape.GetAxis(Axis.X),
                            network.InputShape.GetAxis(Axis.Y));
                        IList <IList <(string Answer, float Probability)> > answers = network.Execute(x).Answers;
                        ////(IList<(string Answer, float Probability)> answers, _) = network.ExecuteSequence(x, model);

                        this.localTimeCounter.Stop();
                        long duration = this.localTimeCounter.ElapsedMilliseconds;

                        foreach (IList <(string answer, float probability)> answer in answers)
                        {
                            string text = answer.FirstOrDefault().answer;
                            float  prob = answer.FirstOrDefault().probability;

                            results.Add(new ClassificationResult <string>(
                                            sample.SourceId,
                                            text,
                                            string.Concat(sample.Labels),
                                            prob,
                                            prob >= 0.38f));

                            this.WriteLine(
                                null,
                                "({0})\tFile: {1} ... OK ({2} ms) {3} {4:F4}",
                                this.totalImages,
                                sample.SourceId.ToFileName(false),
                                duration,
                                text,
                                prob);
                        }

                        /*string answer = answers.Last().FirstOrDefault()?.Answer;
                         * int prob = (int)(((answers.Last().FirstOrDefault()?.Probability ?? 0.0f) * 100) + 0.5f);
                         *
                         * results.Add(new ClassificationResult<string>(
                         *  sample.SourceId,
                         *  answer,
                         *  string.Concat(sample.Labels),
                         *  prob,
                         *  prob >= 0.38f));
                         *
                         * ////this.Write(".");
                         * this.Write(
                         *  null,
                         *  "({0})\tFile: {1} ... OK ({4} ms) {2} {3:F4}",
                         *  this.totalImages,
                         *  sample.SourceId.ToFileName(false),
                         *  duration,
                         *  answer,
                         *  prob);*/
                    }
                }

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

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