/// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            this.spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);

            // Initialize font
            this.font = new FontManager(this.Content, this.spriteBatch);

            // Initialize help
            this.help = new HelpManager(this.input, this.font);

            // Load background
            this.background = new Background(this.Content, this.GraphicsDevice, this.spriteBatch);

            // Load shockwave effect
            this.shockwave = new Shockwave(this.Content, this.GraphicsDevice, this.spriteBatch, this.input);

            // Load image distortion effect
            this.imageDistortion = new ImageDistortion(this.Content, this.GraphicsDevice, this.spriteBatch, this.input);

            // Load sine wave effect
            this.sineWaveDistortion = new SineWaveDistortion(this.Content, this.GraphicsDevice, this.spriteBatch, this.input);
        }
Пример #2
0
            private void Learn(int taskIndex, LearningTask task, CancellationToken cancellationToken)
            {
                using (StreamWriter logFile = File.CreateText(task.LogFileName))
                {
                    logFile.AutoFlush = true;

                    try
                    {
                        // report starting time
                        DateTime dateStarted = DateTime.Now;
                        this.WriteLine(logFile, string.Format(CultureInfo.InvariantCulture, "Started: {0}", dateStarted.ToString("G", CultureInfo.InvariantCulture)));

                        ClassificationNetwork net = File.Exists(task.Architecture) ?
                                                    ClassificationNetwork.FromFile(task.Architecture) :
                                                    ClassificationNetwork.FromArchitecture(task.Architecture, task.Classes, task.Classes, task.BlankClass);

                        // learning
                        Learn();
                        net.SaveToFile(task.OutputFileName, NetworkFileFormat.JSON);

                        // report finish time and processing interval
                        DateTime dateFinished = DateTime.Now;
                        this.WriteLine(logFile, string.Empty);
                        this.WriteLine(logFile, string.Format(CultureInfo.InvariantCulture, "Finished: {0:G}", dateFinished));
                        this.WriteLine(logFile, string.Format(CultureInfo.InvariantCulture, "Total time: {0:g}", TimeSpan.FromSeconds((dateFinished - dateStarted).TotalSeconds)));

                        void Learn()
                        {
                            this.WriteLine(logFile, "Learning...");

                            ImageDistortion filter = new ImageDistortion();
                            Stopwatch       timer  = new Stopwatch();

                            this.WriteLine(logFile, "  Epochs: {0}", task.Epochs);

                            this.WriteTrainerParameters(logFile, task.Trainer, task.Algorithm, task.Loss);

                            this.WriteLine(logFile, "Image distortion:");
                            this.WriteLine(logFile, "  Shift: {0}", task.Shift);
                            this.WriteLine(logFile, "  Rotate: {0}", task.Rotate);
                            this.WriteLine(logFile, "  Scale: {0}", task.Scale);
                            this.WriteLine(logFile, "  Crop: {0}", task.Crop);

                            Shape shape = net.InputShape;

                            using (TestImageProvider <string> dataProvider = task.CreateDataProvider(net))
                            {
                                using (TestImageProvider <string> testDataProvider = task.CreateTestDataProvider(net))
                                {
                                    ////int n = 0;
                                    for (int epoch = 0; epoch < task.Epochs; epoch++)
                                    {
                                        // run learning
                                        timer.Restart();

                                        TrainingResult result = task.Trainer.RunEpoch(
                                            epoch,
                                            net,
                                            GenerateLearnSamples(dataProvider, epoch),
                                            task.Algorithm,
                                            task.Loss,
                                            cancellationToken);

                                        timer.Stop();

                                        lock (this.logLocker)
                                        {
                                            string s = string.Format(
                                                CultureInfo.InvariantCulture,
                                                "Net: {0}, Epoch: {1}, Time: {2} ms, {3}",
                                                taskIndex,
                                                epoch,
                                                timer.ElapsedMilliseconds,
                                                result);

                                            this.Write(logFile, s);
                                            ////this.WriteDebugInformation(logFile);
                                            this.WriteLine(logFile, string.Empty);
                                        }

                                        // run testing
                                        string epochOutputFileName = string.Format(CultureInfo.InvariantCulture, task.EpochFileNameTemplate, epoch);

                                        // save network
                                        net.SaveToFile(epochOutputFileName, NetworkFileFormat.JSON);

                                        // run testing
                                        List <ClassificationResult <string> > results = new List <ClassificationResult <string> >();
                                        if (task.Loss is CTCLoss)
                                        {
                                            Context model = Context.FromRegex(@"\d", CultureInfo.InvariantCulture);

                                            foreach ((TestImage image, string[] labels) in GenerateTestSamples(testDataProvider))
                                            {
                                                if (image.Image.IsAllWhite())
                                                {
                                                    results.Add(new ClassificationResult <string>(
                                                                    image.SourceId,
                                                                    "0",
                                                                    string.Concat(labels),
                                                                    1.0f,
                                                                    true));
                                                }
                                                else
                                                {
                                                    Tensor x = ImageExtensions.FromImage(image.Image, null, Shape.BWHC, shape.GetAxis(Axis.X), shape.GetAxis(Axis.Y));
                                                    (string text, float prob) = net.ExecuteSequence(x, model).Answers.FirstOrDefault();

                                                    results.Add(new ClassificationResult <string>(
                                                                    image.SourceId,
                                                                    text,
                                                                    string.Concat(labels),
                                                                    prob,
                                                                    prob >= 0.38f));
                                                }
                                            }
                                        }
                                        else
                                        {
                                            foreach ((TestImage image, string[] labels) in GenerateTestSamples(testDataProvider))
                                            {
                                                if (image.Image.IsAllWhite())
                                                {
                                                    results.Add(new ClassificationResult <string>(
                                                                    image.SourceId,
                                                                    "0",
                                                                    string.Concat(labels),
                                                                    1.0f,
                                                                    true));
                                                }
                                                else
                                                {
                                                    Tensor x = ImageExtensions.FromImage(image.Image, null, Shape.BWHC, shape.GetAxis(Axis.X), shape.GetAxis(Axis.Y));

                                                    foreach (IList <(string answer, float probability)> answer in net.Execute(x).Answers)
                                                    {
                                                        string text = answer.FirstOrDefault().answer;
                                                        float  prob = answer.FirstOrDefault().probability;

                                                        results.Add(new ClassificationResult <string>(
                                                                        image.SourceId,
                                                                        text,
                                                                        string.Concat(labels),
                                                                        prob,
                                                                        prob >= 0.38f));
                                                    }
                                                }
                                            }
                                        }

                                        // write report
                                        ClassificationReport <string> testReport = new ClassificationReport <string>(results);
                                        this.Write(logFile, ClassificationReportWriter <string> .WriteReport(testReport, ClassificationReportMode.Summary));

                                        using (StreamWriter outputFile = File.CreateText(Path.ChangeExtension(epochOutputFileName, ".res")))
                                        {
                                            ClassificationReportWriter <string> .WriteReport(outputFile, testReport, ClassificationReportMode.All);
                                        }
                                    }
                                }

                                IEnumerable <(Tensor x, string[] labels)> GenerateLearnSamples(TestImageProvider <string> provider, int epoch)
                                {
                                    return(GenerateSamples(provider)
                                           .Where(x => !x.image.Image.IsAllWhite())
                                           .SelectMany(x =>
                                    {
                                        if (epoch == 0)
                                        {
                                            ////x.Image.Save("e:\\temp\\" + x.Id + "_" + n.ToString(CultureInfo.InvariantCulture) + "_.bmp");
                                        }

                                        return filter
                                        .Distort(
                                            x.image.Image,
                                            shape.GetAxis(Axis.X),
                                            shape.GetAxis(Axis.Y),
                                            task.Shift,
                                            task.Rotate && x.image.FontStyle != FontStyle.Italic,
                                            task.Scale,
                                            task.Crop)
                                        .Select(bitmap =>
                                        {
                                            if (epoch == 0)
                                            {
                                                ////Interlocked.Increment(ref n);
                                                ////bitmap.Save(@"d:\dnn\temp\" + n.ToString(CultureInfo.InvariantCulture) + ".bmp");
                                                ////bitmap.Save(@"d:\dnn\temp\" + (n).ToString(CultureInfo.InvariantCulture) + "_" + x.SourceId.Id + ".bmp");
                                            }

                                            return (ImageExtensions.FromImage(bitmap, null, Shape.BWHC, shape.GetAxis(Axis.X), shape.GetAxis(Axis.Y)), x.labels);
                                        });
                                    }));
                                }

                                IEnumerable <(TestImage image, string[] labels)> GenerateTestSamples(TestImageProvider <string> provider)
                                {
                                    return(GenerateSamples(provider)
                                           .AsParallel()
                                           .AsOrdered()
                                           .WithCancellation(cancellationToken)
                                           .WithMergeOptions(ParallelMergeOptions.AutoBuffered));
                                }

                                IEnumerable <(TestImage image, string[] labels)> GenerateSamples(TestImageProvider <string> provider)
                                {
                                    return(provider
                                           .Generate(net.AllowedClasses)
                                           .Select(x =>
                                    {
                                        string[] labels = x.Labels;
                                        if (!(task.Loss is CTCLoss))
                                        {
                                            int b = net.OutputShapes.First().GetAxis(Axis.B);
                                            if (labels.Length == 1 && b > 1)
                                            {
                                                labels = Enumerable.Repeat(labels[0], b).ToArray();
                                            }
                                        }

                                        return (x, labels);
                                    }));
                                }
                            }
                        }
                    }
                    finally
                    {
                        logFile.Flush();
                    }
                }
            }