示例#1
0
        public static DigitImage[] Read(DataSet dataset)
        {
            FileStream fileStream;

            if (dataset == DataSet.Training)
            {
                fileStream = new FileStream(trainPath, FileMode.Open, FileAccess.Read);
            }
            else
            {
                fileStream = new FileStream(testPath, FileMode.Open, FileAccess.Read);
            }

            List <DigitImage> digitImageList = new List <DigitImage>();

            using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
            {
                string line;
                streamReader.ReadLine();

                while ((line = streamReader.ReadLine()) != null)
                {
                    string[] splitted = line.Split(',');

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

                    for (int i = 0; i < splitted.Length - 1; ++i)
                    {
                        pixels[i / 28][i % 28] = (byte)int.Parse(splitted[i + 1]);
                    }

                    byte       label = (dataset == DataSet.Training ? byte.Parse(splitted[0]) : (byte)10);
                    DigitImage d     = new DigitImage(pixels, label);
                    digitImageList.Add(d);
                }
            }

            return(digitImageList.ToArray());
        }
示例#2
0
        public static DigitImage[] ReadFromFile(DataSet dataSet, int count = -1)
        {
            DigitImage[] digitImages = null;
            string       pathImages  = "";
            string       pathLabels  = "";

            if (count != -1)
            {
                ImageCount  = count;
                digitImages = new DigitImage[ImageCount];
            }
            if (dataSet == DataSet.Training)
            {
                if (imageCount == -1)
                {
                    digitImages = new DigitImage[MaxTrainingImageCount];
                    ImageCount  = MaxTrainingImageCount;
                }

                pathImages = path_training_images;
                pathLabels = path_training_labels;
            }
            else
            {
                if (imageCount == -1)
                {
                    digitImages = new DigitImage[MaxTestingImageCount];
                    ImageCount  = MaxTestingImageCount;
                }

                pathImages = path_test_images;
                pathLabels = path_test_labels;
            }

            FileStream ifsLabels;
            FileStream ifsImages;

            try
            {
                //Console.WriteLine("\nBegin\n");

                ifsLabels =
                    new FileStream(pathLabels,
                                   FileMode.Open); // test labels

                ifsImages =
                    new FileStream(pathImages,
                                   FileMode.Open); // test images

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

                int magic1    = brImages.ReadInt32(); // discard
                int numImages = brImages.ReadInt32();
                int numRows   = brImages.ReadInt32();
                int numCols   = brImages.ReadInt32();

                int magic2    = brLabels.ReadInt32();
                int numLabels = brLabels.ReadInt32();

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

                // each test image
                //there are 10 000 images so you have this limit
                for (int di = 0; di < ImageCount; ++di)
                {
                    //Console.Clear();
                    for (int i = 0; i < 28; ++i)
                    {
                        for (int j = 0; j < 28; ++j)
                        {
                            byte b = brImages.ReadByte();
                            pixels[i][j] = b;
                        }
                    }

                    byte lbl = brLabels.ReadByte();

                    DigitImage dImage =
                        new DigitImage(pixels, lbl);
                    digitImages[di] = dImage;
                    //Console.WriteLine(dImage.ToString());
                    //Console.ReadLine();
                } // each image

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

                //Console.WriteLine("\nEnd\n");
                //Console.ReadLine();
                return(digitImages);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //Console.ReadLine();
            }
            return(null);
        }