private void ButtonTrain_Click(object sender, EventArgs e) { FolderBrowserDialog op = new FolderBrowserDialog(); lbl0.Visible = lbl1.Visible = false; if (op.ShowDialog() == DialogResult.OK) { Random rand = new Random((int)DateTime.Now.Ticks); var files = System.IO.Directory .GetFiles(op.SelectedPath, "*.jpg", System.IO.SearchOption.AllDirectories) .OrderBy(a => rand.Next()).ToArray(); var paramFile = System.IO.Path.Combine(op.SelectedPath, "params.json"); Parameters parameters = null; if (System.IO.File.Exists(paramFile)) { parameters = Newtonsoft.Json.JsonConvert.DeserializeObject <Parameters>(System.IO.File.ReadAllText(paramFile)); } else { parameters = new Parameters(); } ParamsSet frm = new ParamsSet(parameters); if (frm.ShowDialog() != DialogResult.OK) { return; } var json = Newtonsoft.Json.JsonConvert.SerializeObject(parameters); System.IO.File.WriteAllText(System.IO.Path.Combine(op.SelectedPath, "params.json"), json); network = new Network(Threshold: parameters.Threshold, TimeStep: parameters.TimeStep, InitWeight: parameters.InitialWeight, Vartiation: parameters.Variation, PA: parameters.PA, NA: parameters.NA); List <Bitmap> images = new List <Bitmap>(10000); foreach (var f in files) { var img = (Bitmap)Bitmap.FromFile(f); { var imgs = img.Variation(0); images.AddRange(imgs); } } if (files.Length < 10) { ImageVariationForm frmVariation = new ImageVariationForm(images); if (frmVariation.ShowDialog() == DialogResult.Cancel) { return; } } var Gabors = new GaborFilter[images.Count]; Task.Run(() => { try { this.InvokeIfRequired(f => { f.Text = "Little Zhenik - making gabor filters"; }); this.InvokeIfRequired(f => { ButtonTrain.Enabled = false; progressBar1.Visible = true; }); Parallel.For(0, images.Count, i => { Gabors[i] = new GaborFilter(); Gabors[i].CreateFromBitmap(images[i]); }); this.InvokeIfRequired(f => { f.Text = "Little Zhenik"; }); } catch (Exception ex) { this.InvokeIfRequired(f => MessageBox.Show(ex.Message)); } }).ContinueWith(g => { { var epochs = parameters.Epoch; this.InvokeIfRequired(f => { progressBar1.Maximum = epochs * 2 / 3 * epochs * 1 / 3 * Gabors.Length; }); try { int p = 0; for (int i = 0; i < epochs * 2 / 3; i++) { for (int j = 0; j < Gabors.Length; j++) { for (int c = 0; c < epochs / 3; c++) { var t = network.Train(Gabors[j]); if (t == null) { this.InvokeIfRequired(f => { //MessageBox.Show(f,"threshold error"); this.Text = "Little Zhenik - Threshold"; i = epochs; j = Gabors.Length; c = epochs / 3; }); } if (p % 20 == 0) { this.InvokeIfRequired(f => { try { f.ShowResult(); progressBar1.Value = p; } catch { this.Text = "Little Zhenik - Threshold"; } }); Task.Delay(5).Wait(); } p++; } } } } catch (Exception ex) { this.InvokeIfRequired(f => MessageBox.Show(ex.Message)); } this.InvokeIfRequired(f => { progressBar1.Visible = false; ButtonTest.Enabled = true; ButtonTrain.Enabled = true; }); } }); } }
private void ButtonTrain_Click(object sender, EventArgs e) { FolderBrowserDialog op = new FolderBrowserDialog(); if (op.ShowDialog() == DialogResult.OK) { var files = System.IO.Directory.GetFiles(op.SelectedPath); List <Bitmap> images = new List <Bitmap>(10000); foreach (var f in files) { using (var img = (Bitmap)Bitmap.FromFile(f)) { var imgs = img.Variation(0); images.AddRange(imgs); } } ImageVariationForm frm = new ImageVariationForm(images); if (frm.ShowDialog() == DialogResult.Cancel) { return; } Gabors = new GaborFilter[images.Count]; Parallel.For(0, images.Count, i => { Gabors[i] = new GaborFilter(); Gabors[i].CreateFromBitmap(images[i]); }); Random rand = new Random(); var epochs = 70; progressBar1.Maximum = epochs * Gabors.Length; ButtonTrain.Enabled = false; progressBar1.Visible = true; Task.Run(() => { for (int i = 0; i < epochs; i++) { for (int j = 0; j < Gabors.Length; j++) { var t = network.Train(Gabors[j]); if (t == null) { this.InvokeIfRequired(f => { MessageBox.Show(f, "threshold error"); }); } this.InvokeIfRequired(f => { f.ShowResult(); progressBar1.Value = (i) * (Gabors.Length) + (j); }); Task.Delay(5).Wait(); } } this.InvokeIfRequired(f => { progressBar1.Visible = false; ButtonTest.Enabled = true; }); } ); } }