Exemple #1
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;
        }