Esempio n. 1
0
        public MnistImages(string myLabelsPath, string myImagesPath)
        {
            labelsPath = myLabelsPath;
            imagesPath = myImagesPath;

            BinaryReader labels = new BinaryReader(new FileStream(labelsPath, FileMode.Open));
            BinaryReader images = new BinaryReader(new FileStream(imagesPath, FileMode.Open));

            imagesHeader = ReverseBytes(images.ReadInt32());
            imageCount   = ReverseBytes(images.ReadInt32());
            x            = ReverseBytes(images.ReadInt32());
            y            = ReverseBytes(images.ReadInt32());

            lablesHeader = ReverseBytes(labels.ReadInt32());
            lablesCount  = ReverseBytes(labels.ReadInt32());

            for (int i = 0; i < imageCount; ++i)
            {
                MnistImage mi = new MnistImage(x, y);
                mi.Label = labels.ReadByte().ToString();
                mi.ReadImage(images);
                mi.Index = i;
                ImageList.Add(mi);
            }
        }
Esempio n. 2
0
        public void GenerateComposite(int MaxImagesToDisplay = 200)
        {
            fullWidth  = columns * Images.First().Bitmap.Width;
            fullHeight = ((Images.Count / columns) + 1) * Images.First().Bitmap.Height;
            Composite  = new Bitmap(fullWidth, fullHeight);
            int posX = 0;
            int posY = 0;

            ImageCount = Images.Count;
            if (ImageCount > MaxImagesToDisplay)
            {
                ImageCount = MaxImagesToDisplay;
            }
            for (int i = 0; i < ImageCount; ++i)
            {
                MnistImage m = Images[i];
                graphics = Graphics.FromImage(Composite);
                graphics.DrawImage(m.Bitmap, posX, posY);
                m.PosX      = posX;
                m.PosY      = posY;
                m.Rectangle = new Rectangle(posX, posY, m.Bitmap.Width, m.Bitmap.Height);
                posX       += m.Bitmap.Width;
                if (posX >= Composite.Width)
                {
                    posX  = 0;
                    posY += m.Bitmap.Height;
                }
            }
        }
        public static string PixelValues(MnistImage dImage)
        {
            // create a string, with embedded newlines, suitable
            // for display in a multi-line TextBox control
            string s = "";

            for (int i = 0; i < dImage.height; ++i)
            {
                for (int j = 0; j < dImage.width; ++j)
                {
                    s += dImage.pixels[i][j].ToString("X2") + " ";
                }
                s += Environment.NewLine;
            }
            return(s);
        }
        private void button2_Click(object sender, EventArgs e) // Display 'next' image
        {
            int        nextIndex = int.Parse(textBox4.Text);
            MnistImage currImage = trainImages[nextIndex];

            int    mag    = int.Parse(comboBox1.SelectedItem.ToString()); // magnification
            Bitmap bitMap = MakeBitmap(currImage, mag);

            pictureBox1.Image = bitMap;

            string pixelVals = PixelValues(currImage);

            textBox5.Text = pixelVals;

            textBox3.Text = textBox4.Text;              // update curr idx from old next idz
            textBox4.Text = (nextIndex + 1).ToString(); // ++next index

            listBox1.Items.Add("Curr image index = " + textBox3.Text + " label = " + currImage.label);
        }
Esempio n. 5
0
        public void EvaluateImage(Bitmap image)
        {
            if (Network != null)
            {
                if (TrainingImages != null)
                {
                    var        x          = TrainingImages.ImageList.First().Width;
                    var        y          = TrainingImages.ImageList.First().Height;
                    MnistImage mnistImage = new MnistImage(x, y);
                    mnistImage.GetPixels(image, x, y);

                    //var data = mnistImage.Pixels.Normalize(Byte.MaxValue + 1);
                    var data   = mnistImage.Pixels.ToArray();
                    var result = Network.EvaluateData(data);
                    ShowResult?.Invoke(result);
                    DisplayLayers(Network.Layers);
                    //ShowImage(mnistImage.Bitmap);
                    Log?.Invoke(String.Format("Image Evaluates to:{0}", result));
                }
            }
        }
        } // LoadData

        public static Bitmap MakeBitmap(MnistImage dImage, int mag)
        {
            // create a C# Bitmap suitable for display in a PictureBox control
            int      width  = dImage.width * mag;
            int      height = dImage.height * mag;
            Bitmap   result = new Bitmap(width, height);
            Graphics gr     = Graphics.FromImage(result);

            for (int i = 0; i < dImage.height; ++i)
            {
                for (int j = 0; j < dImage.width; ++j)
                {
                    int pixelColor = 255 - dImage.pixels[i][j];                   // white background, black digits
                    //int pixelColor = dImage.pixels[i][j]; // black background, white digits
                    Color c = Color.FromArgb(pixelColor, pixelColor, pixelColor); // gray scale
                    //Color c = Color.FromArgb(pixelColor, 0, 0); // red scale
                    SolidBrush sb = new SolidBrush(c);
                    gr.FillRectangle(sb, j * mag, i * mag, mag, mag); // fills bitmap via Graphics object
                }
            }
            return(result);
        }
        public static MnistImage[] LoadData(string pixelFile, string labelFile)
        {
            // Load MNIST training set of 60,000 images into memory
            // remove static to access listBox1
            int numImages = 60000;

            MnistImage[] result = new MnistImage[numImages];

            byte[][] pixels = new byte[28][];
            for (int i = 0; i < pixels.Length; ++i)
            {
                pixels[i] = new byte[28];
            }

            FileStream ifsPixels = new FileStream(pixelFile, FileMode.Open);
            FileStream ifsLabels = new FileStream(labelFile, FileMode.Open);

            BinaryReader brImages = new BinaryReader(ifsPixels);
            BinaryReader brLabels = new BinaryReader(ifsLabels);

            int magic1 = brImages.ReadInt32(); // stored as Big Endian

            magic1 = ReverseBytes(magic1);     // convert to Intel format

            int imageCount = brImages.ReadInt32();

            imageCount = ReverseBytes(imageCount);

            int numRows = brImages.ReadInt32();

            numRows = ReverseBytes(numRows);
            int numCols = brImages.ReadInt32();

            numCols = ReverseBytes(numCols);

            int magic2 = brLabels.ReadInt32();

            magic2 = ReverseBytes(magic2);

            int numLabels = brLabels.ReadInt32();

            numLabels = ReverseBytes(numLabels);

            // each image
            for (int di = 0; di < numImages; ++di)
            {
                for (int i = 0; i < 28; ++i) // get 28x28 pixel values
                {
                    for (int j = 0; j < 28; ++j)
                    {
                        byte b = brImages.ReadByte();
                        pixels[i][j] = b;
                    }
                }

                byte       lbl    = brLabels.ReadByte(); // get the label
                MnistImage dImage = new MnistImage(28, 28, pixels, lbl);
                result[di] = dImage;
            } // each image

            ifsPixels.Close(); brImages.Close();
            ifsLabels.Close(); brLabels.Close();

            return(result);
        } // LoadData