示例#1
0
 public bool LoadMinstFiles()
 {
     //clear Image Pattern List
     if (m_pImagePatterns != null)
     {
         m_pImagePatterns.Clear();
     }
     //close files if opened
     if (_bImageFileOpen)
     {
         load_ImageFile_stream.Close();
         _bImageFileOpen = false;
     }
     if (_bLabelFileOpen)
     {
         load_LabelFile_stream.Close();
         _bLabelFileOpen = false;
     }
     //load Mnist Images files.
     if (!MnistImageFileHeader())
     {
         MessageBox.Show("Can not open Image file");
         _MnistImageFileName = null;
         _bImageFileOpen     = false;
         _bDatabase          = false;
         return(false);
     }
     if (!MnistLabelFileHeader())
     {
         MessageBox.Show("Can not open label file");
         _MnistLabelFileName = null;
         _bLabelFileOpen     = false;
         _bDatabase          = false;
         return(false);
     }
     //check the value if image file and label file have been opened successfully
     if (_LabelFileBegin.nItems != _ImageFileBegin.nItems)
     {
         MessageBox.Show("Item numbers are different");
         CloseMinstFiles();
         _bDatabase = false;
         return(false);
     }
     m_pImagePatterns            = new List <ImagePattern>(_ImageFileBegin.nItems);
     _iRandomizedPatternSequence = new int[_ImageFileBegin.nItems];
     for (int i = 0; i < _ImageFileBegin.nItems; i++)
     {
         byte         m_nlabel;
         byte[]       m_pPatternArray = new byte[MyDefinations.g_cImageSize * MyDefinations.g_cImageSize];
         ImagePattern m_ImagePattern  = new ImagePattern();
         GetNextPattern(m_pPatternArray, out m_nlabel, i, true);
         m_ImagePattern.pPattern = m_pPatternArray;
         m_ImagePattern.nLabel   = m_nlabel;
         m_pImagePatterns.Add(m_ImagePattern);
     }
     _bDatabase = true;
     CloseMinstFiles();
     return(true);
 }
 static void ReadTraining()
 {
     for (int i = 0; i < TrainingSamples; ++i)
     {
         ImagePattern IP           = Bayesian_Classifier._MnistTrainingDatabase.m_pImagePatterns[i];
         int          LabeledClass = IP.nLabel;
         TrainingLabeledClass[i] = LabeledClass;
         ++NumberOfTrainingLabeledSamples[LabeledClass];
         for (int j = 0; j < NumberOfFeatures; ++j)
         {
             if (IP.pPattern[j] == 255)
             {
                 TrainingFeatures[i, j] = 1;
             }
             else
             {
                 TrainingFeatures[i, j] = 0;
             }
         }
     }
 }
        private void btn_Show_Click(object sender, EventArgs e)
        {
            Bitmap       Bmap = new Bitmap(28, 28);
            int          End = 28;
            int          i = 0, j = 0, k = 0;
            byte         PixelValue;
            ImagePattern IP = null;

            if (rdoBtn_TrainingSet.Checked)
            {
                IP = Bayesian_Classifier._MnistTrainingDatabase.m_pImagePatterns[(int)(numericUpDown1.Value - 1)];
            }
            else if (rdoBtn_TestingSet.Checked)
            {
                IP = Bayesian_Classifier._MnistTestingDatabase.m_pImagePatterns[(int)(numericUpDown1.Value - 1)];
            }
            while (i < 28)
            {
                k = 0;
                for (j = i * 28; j < End; j++)
                {
                    PixelValue = IP.pPattern[j];
                    if (chckBx_Threshold.Checked && PixelValue < 255)
                    {
                        PixelValue = 0;
                    }
                    Bmap.SetPixel(k, i, Color.FromArgb(PixelValue, PixelValue, PixelValue));
                    k++;
                }
                i++;
                End = (i + 1) * 28;
            }
            label1.Text       = IP.nLabel.ToString();
            pictureBox1.Image = (Image)Bmap;
            if (rdoBtn_TestingSet.Checked)
            {
                textBox1.Text = Bayesian_Classifier.TestingClassifiedClass[(int)(numericUpDown1.Value - 1)].ToString();
            }
        }