Example #1
0
        /// <summary>
        /// Visualizes a few examples and their classification outcome to get an overall impression of the network's accuracy.
        /// </summary>
        /// <param name="network">The neural network to test.</param>
        /// <param name="characterSource">The character source to generate samples from.</param>
        /// <param name="rows">Amount of examples to show vertically.</param>
        /// <param name="columns">Amount of examples to show horizontally.</param>
        /// <returns>An object that can be assigned directly to an ImageControl's Source property.</returns>
        public static ImageSource DrawTestGallery(IOCRModel ocrModel, ICharacterSource characterSource, int rows, int columns)
        {
            const int inputScaleFact = 2;
            const int labelHeight    = 16;
            int       width          = (characterSource.ImageWidth * inputScaleFact + sectionPadding) * columns;
            int       height         = (characterSource.ImageHeight * inputScaleFact + labelHeight + sectionPadding) * rows;
            var       output         = new WriteableBitmap(width, height, 96.0, 96.0, PixelFormats.Rgb24, null);

            byte[] bytes   = new byte[width * height * 3];
            var    samples = characterSource.GenerateMulti(rows * columns);
            int    baseY   = 0;

            for (int i = 0; i < rows; i++)
            {
                int baseX = 0;
                for (int j = 0; j < columns; j++)
                {
                    var sample = samples[i * columns + j];
                    drawInput(sample.Image, bytes, inputScaleFact, baseX, baseY, width);
                    var  result  = ocrModel.ExecuteSingle(sample.Image);
                    bool correct = (result.MostConfident == sample.Character);
                    for (int y = baseY + characterSource.ImageHeight * inputScaleFact + labelHeight / 4;
                         y < baseY + characterSource.ImageHeight * inputScaleFact + labelHeight / 2; y++)
                    {
                        for (int x = baseX; x < baseX + characterSource.ImageWidth * inputScaleFact; x++)
                        {
                            bytes[(x + y * width) * 3]     = (byte)(correct ? 0 : 255);
                            bytes[(x + y * width) * 3 + 1] = (byte)(correct ? 255 : 0);
                            bytes[(x + y * width) * 3 + 2] = (byte)0;
                        }
                    }
                    baseX += characterSource.ImageWidth * inputScaleFact + sectionPadding;
                }
                baseY += characterSource.ImageHeight * inputScaleFact + labelHeight + sectionPadding;
            }

            output.WritePixels(new Int32Rect(0, 0, width, height), bytes, width * 3, 0);
            return(output);
        }