Ejemplo n.º 1
0
        static void testRandomImage()
        {
            MNISTImage image = new MNISTImage();

            byte[] bytes = new byte[28 * 28];

            random.NextBytes(bytes);

            for (int m = 0; m < 28; m++)
            {
                for (int n = 0; n < 28; n++)
                {
                    image[m, n] = bytes[28 * m + n];
                }
            }

            Console.WriteLine("MNIST image:");
            Console.WriteLine(image.ToString());
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        private List <MNISTImage> loadImagesfile(String imagesFileName, int correctSize, String correctHeaderSignature)
        {
            if (imagesFileName == null)
            {
                throw new Exception("MNIST: imagesFileName is null.");
            }

            List <MNISTImage> images = new List <MNISTImage>();

            byte[] bytes = File.ReadAllBytes(imagesFileName);

            int totalSize = bytes.Length;

            if (totalSize != correctSize)
            {
                throw new Exception("MNIST: imagesFile has incorrect size." +
                                    " Number of bytes is " + totalSize.ToString());
            }

            String headerSignature =
                bytes[0].ToString() + ", " +
                bytes[1].ToString() + ", " +
                bytes[2].ToString() + ", " +
                bytes[3].ToString() + " | " +
                bytes[4].ToString() + ", " +
                bytes[5].ToString() + ", " +
                bytes[6].ToString() + ", " +
                bytes[7].ToString() + " | " +
                bytes[8].ToString() + ", " +
                bytes[9].ToString() + ", " +
                bytes[10].ToString() + ", " +
                bytes[11].ToString() + " | " +
                bytes[12].ToString() + ", " +
                bytes[13].ToString() + ", " +
                bytes[14].ToString() + ", " +
                bytes[15].ToString();

            if (headerSignature != correctHeaderSignature)
            {
                throw new Exception("MNIST: imagesFile has incorrect header signature." +
                                    " Header signature is " + headerSignature);
            }

            int        headerSize = 4 + 4 + 4 + 4;
            int        nrOfRows   = 28;
            int        rowSize    = 28;
            int        imageSize  = nrOfRows * rowSize;
            int        nrOfImages = (totalSize - headerSize) / imageSize;
            int        i0         = headerSize;
            MNISTImage mnistImage = null;

            for (int k = 0; k < nrOfImages; k++)
            {
                mnistImage = new MNISTImage();

                for (int m = 0; m < nrOfRows; m++)
                {
                    for (int n = 0; n < rowSize; n++)
                    {
                        mnistImage[m, n] = bytes[i0 + (k * imageSize) + ((m * rowSize) + n)];
                    }
                }

                images.Add(mnistImage);
            }

            return(images);
        }