Пример #1
0
        Tuple <List <Matrix>, List <Matrix> > ReadMNISTTrainingData()
        {
            String trainDir = "C:\\Users\\ivans_000\\Desktop\\MASTER\\Spring2019\\Deep_Learning\\Assignment2_Sangines\\Data\\Training1000\\";
            //string trainDir = @"D:\csharp2018\DataSets\MNIST\MNISTBMP\Training1000";
            //string trainDir = @"D:\csharp2016\DeepLearning\MNIST\data\TrainingAll60000";
            //string trainDir = @"D:\csharp2018\DataSets\MNIST\MNISTBMP\data\TrainingAll60000";
            int           numFiles         = 0;
            int           dataIndex        = 0;
            DirectoryInfo dinfo            = new DirectoryInfo(trainDir);
            List <Matrix> InputDataList    = new List <Matrix>();
            List <Matrix> OutputLabelsList = new List <Matrix>();

            //count the number of files
            foreach (FileInfo fi in dinfo.GetFiles())
            {
                numFiles++;
            }

            foreach (FileInfo fi in dinfo.GetFiles())
            {
                Matrix Img   = new Matrix(28, 28);
                String fname = fi.FullName;
                Bitmap bmp   = new Bitmap(Image.FromFile(fname));
                if (ImageProc.IsGrayScale(bmp) == false) //make sure it is grayscale
                {
                    ImageProc.ConvertToGray(bmp);
                }
                for (int i = 0; i < bmp.Width; i++)
                {
                    for (int j = 0; j < bmp.Height; j++)
                    {   // rescale between 0 and 1
                        Img.D[i][j] = bmp.GetPixel(i, j).R / 255.0;
                    }
                }
                InputDataList.Add(Img);
                Matrix outputLabel = new Matrix(10, 1);
                String s1          = fi.Name;
                Char   output      = s1[0];
                int    classLabel  = (Convert.ToInt16(output) - 48); //will only work with numbers 0-9
                outputLabel.D[classLabel][0] = 1;                    // others are 0 by default
                OutputLabelsList.Add(outputLabel);

                dataIndex++;
                if ((dataIndex % 500) == 0)
                {
                    Console.WriteLine("iter: " + dataIndex);
                }
            }
            return(new Tuple <List <Matrix>, List <Matrix> >(InputDataList, OutputLabelsList));
        }
Пример #2
0
        Matrix ReadOneImage(string fname)
        {
            Matrix Img = new Matrix(28, 28);
            Bitmap bmp = new Bitmap(Image.FromFile(fname));

            if (ImageProc.IsGrayScale(bmp) == false) //make sure it is grayscale
            {
                ImageProc.ConvertToGray(bmp);
            }
            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {   // rescale between 0 and 1
                    Img.D[i][j] = bmp.GetPixel(i, j).R / 255.0;
                }
            }
            return(Img);
        }