Example #1
0
        public static int[,] ParsePicture(string path, PictureBox pictBox = null)
        {
            int[,] ret = null;
            if (!File.Exists(path))
            {
                throw new FileNotFoundException();
            }
            Image img = Image.FromFile(path);

            Bitmap bitmap = img as Bitmap;

            if (bitmap != null)
            {
                List <MyLine> hLines = new List <MyLine>();
                List <MyLine> vLines = new List <MyLine>();
                LoadPicture(ref bitmap, hLines, vLines);
                if (m_learnWindow != null)
                {
                    DisconnectPictureBox();
                }
                m_learnWindow = new LearnWindow(bitmap, hLines, vLines, pictBox);
                //m_learnWindow.ShowDialog();
                ret = m_learnWindow.RecognizeDigits();

                bitmap.Dispose();
            }
            return(ret);
        }
Example #2
0
        private void butTeach_Click(object sender, EventArgs e)
        {
            if (txtInputFolder.Text == "" || txtOutputFolder.Text == "" || numLearningRate.Value == 0)
            {
                MessageBox.Show("Incorrect parameter(s)", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int[] sizes = new int[] { LearnWindow.DIGIT_PATTERN_WIDTH *LearnWindow.DIGIT_PATTERN_HEIGHT, (int)numHidNeurons.Value, 10 };
            lock (m_cancelLock)
            {
                m_cancel = false;
            }
            PrintLine("Loading images...");
            progressBar1.Show();
            butTeach.Enabled     = false;
            butOk.Enabled        = false;
            butCancel.Enabled    = false;
            butStop.Enabled      = true;
            progressBar1.Minimum = 0;
            progressBar1.Style   = ProgressBarStyle.Continuous;
            progressBar1.Value   = 0;
            NeuralNetwork net  = new NeuralNetwork(sizes);
            var           data = new List <Tuple <MyVector, MyVector> >();
            DirectoryInfo dir  = new DirectoryInfo(txtInputFolder.Text);
            //DirectoryInfo dir = new DirectoryInfo(@"c:\Users\Luky\Downloads\t10k-images");
            List <FileInfo> files = new List <FileInfo>(dir.GetFiles("*.png", SearchOption.AllDirectories));

            files.AddRange(dir.GetFiles("*.jpg", SearchOption.AllDirectories));
            files.AddRange(dir.GetFiles("*.bmp", SearchOption.AllDirectories));
            files.AddRange(dir.GetFiles("*.jpeg", SearchOption.AllDirectories));
            progressBar1.Maximum = files.Count;
            foreach (var file in files)
            {
                using (Image temp = Image.FromFile(file.FullName))
                {
                    var bitmap = temp as Bitmap;
                    if (bitmap != null)
                    {
                        if (IsCancelled())
                        {
                            return;
                        }
                        if (bitmap.Width != LearnWindow.DIGIT_PATTERN_WIDTH ||
                            bitmap.Height != LearnWindow.DIGIT_PATTERN_HEIGHT)
                        {
                            continue;
                        }
                        MyVector input  = LearnWindow.BitmapToInputVector(bitmap);
                        int      output = int.Parse(file.Name.Substring(0, 1));
                        data.Add(new Tuple <MyVector, MyVector>(input, MyVector.UnitVector(10, output)));
                        Debug.Print("Picture num: {0}/{1} loaded.", data.Count, files.Count);
                        progressBar1.Value++;
                        //PrintLine(String.Format("Picture num: {0}/{1} loaded.", data.Count, files.Count)); // Very slow!!! TODO: Better loging console
                    }
                }
            }
            PrintLine("Network learning started...");
            progressBar1.Style = ProgressBarStyle.Marquee;
            net.StochasticGradientDescent(data.ToArray(), (int)numOfEpochs.Value, (int)numBatchSize.Value, (double)numLearningRate.Value, IsCancelled, PrintLine, txtOutputFolder.Text, (double)numLambda.Value, null, false, false, false, true);
            progressBar1.Hide();
            butTeach.Enabled  = true;
            butStop.Enabled   = false;
            butOk.Enabled     = true;
            butCancel.Enabled = true;
        }