private void AutoQueryButton_Click(object sender, EventArgs e) { double[] input = new double[784]; double[] answer = new double[10]; double[] output = new double[10]; string[] filePaths = Directory.GetFiles(@"MNIST Image\\", "*.bmp", SearchOption.AllDirectories); string image_addr; string image_name; Bitmap image; Bitmap percent_graph = new Bitmap(percent_box.Width * 999, percent_box.Height); double error_percent = 0; LogBox.Clear(); using (Graphics g = Graphics.FromImage(percent_graph)) { g.Clear(Color.Black); } for (int file = 0; file < filePaths.Length; file++) { error_percent = 0; image_addr = filePaths[file]; image_name = Path.GetFileName(image_addr); image = new Bitmap(image_addr); LookWindow.Image = image; LookWindow.Refresh(); for (int x = 0; x < answer.Length; x++) { if (network.neuron_arrays[network.neuron_arrays.Length - 1][x].name == "unnamed") { network.neuron_arrays[network.neuron_arrays.Length - 1][x].name = new DirectoryInfo(Directory.GetDirectories(@"MNIST Image")[x]).Name; } if (network.neuron_arrays[network.neuron_arrays.Length - 1][x].name == Path.GetFileName(Path.GetDirectoryName(image_addr))) { answer[x] = 1; } else { answer[x] = 0; } } for (int y = 0; y < 28; y++) { for (int x = 0; x < 28; x++) { input[x + y * x] = 255 * (1 - Convert.ToDouble(image.GetPixel(x, y).GetBrightness())); } } output = network.Query(input); //LogBox.Clear(); double sum = 0; bool mistake = false; for (int x = 0; x < output.Length; x++) { if (answer[x] == 1) { error_percent += (1 - output[x]) / answer.Length; } else { error_percent += output[x] / answer.Length; if (output[x] >= 0.5) { mistake = true; } } sum += output[x]; } if (mistake) { LogBox.Text += "Error in " + Path.GetFileName(filePaths[file]) + "\r\n"; } LogBox.Refresh(); if (error_percent > 1) { error_percent = 1; } try { ErrorLabel.Text = Convert.ToString(error_percent * 100).Substring(0, 5) + "%"; } catch (ArgumentOutOfRangeException) { ErrorLabel.Text = "100%"; } ErrorLabel.Refresh(); using (Graphics g = Graphics.FromImage(percent_graph)) { int a = Convert.ToInt32((percent_graph.Width / filePaths.Length) * file); int b = Convert.ToInt32(percent_graph.Height - percent_graph.Height * error_percent); int c = Convert.ToInt32(percent_graph.Width / filePaths.Length); int d = Convert.ToInt32(percent_graph.Height * error_percent); g.DrawRectangle(new Pen(Color.Green, 1), new Rectangle(a, b, c, d)); g.FillRectangle(new SolidBrush(Color.Green), new Rectangle(a, b, c, d)); } percent_box.Image = percent_graph; percent_box.Refresh(); } }
private void TrainButton_Click(object sender, EventArgs e) { int epochs = 100; double index = 0.3; string[] filePaths = Directory.GetFiles(@"MNIST Image\\", "*.bmp", SearchOption.AllDirectories); string image_addr; string image_name; Bitmap image; Bitmap percent_graph = new Bitmap(percent_box.Width * 999, percent_box.Height); double error_percent = 0; Random rand = new Random(); try { epochs = Convert.ToInt32(EpochBox.Text); } catch (FormatException) { epochs = 100; EpochBox.Text = "100"; } try { index = Convert.ToInt32(IndexBox.Text); } catch (FormatException) { index = 0.3; IndexBox.Text = "0,3"; } if (EpochBox.Text == "") { EpochBox.Text = "100"; } if (IndexBox.Text == "") { IndexBox.Text = "0,3"; } using (Graphics g = Graphics.FromImage(percent_graph)) { g.Clear(Color.Black); } for (int epoch = 0; epoch < epochs; epoch++) { error_percent = 0; for (int file = 0; file < filePaths.Length; file++) { image_addr = filePaths[rand.Next(0, filePaths.Length)]; image_name = Path.GetFileName(image_addr); image = new Bitmap(image_addr); LookWindow.Image = image; LookWindow.Refresh(); for (int x = 0; x < answer.Length; x++) { if (network.neuron_arrays[network.neuron_arrays.Length - 1][x].name == "unnamed") { network.neuron_arrays[network.neuron_arrays.Length - 1][x].name = new DirectoryInfo(Directory.GetDirectories(@"MNIST Image")[x]).Name; } if (network.neuron_arrays[network.neuron_arrays.Length - 1][x].name == Path.GetFileName(Path.GetDirectoryName(image_addr))) { answer[x] = 1; } else { answer[x] = 0; } } for (int y = 0; y < 28; y++) { for (int x = 0; x < 28; x++) { input[x + y * x] = 1 - Convert.ToDouble(image.GetPixel(x, y).GetBrightness()); } } output = network.Query(input); LogBox.Clear(); double sum = 0; for (int x = 0; x < output.Length; x++) { if (answer[x] == 1) { error_percent += (1 - output[x]) / filePaths.Length; } else { error_percent += output[x] / filePaths.Length; } sum += output[x]; } for (int x = 0; x < output.Length; x++) { LogBox.Text += network.neuron_arrays[network.neuron_arrays.Length - 1][x].name + " => " + Convert.ToString((output[x] / sum) * 100).Substring(0, 4) + "%\r\n"; } LogBox.Refresh(); network.Train(input, answer, index); //Thread.Sleep(500);} } if (error_percent > 1) { error_percent = 1; } try { ErrorLabel.Text = Convert.ToString(error_percent * 100).Substring(0, 5) + "%"; } catch (ArgumentOutOfRangeException) { ErrorLabel.Text = "100%"; } ErrorLabel.Refresh(); using (Graphics g = Graphics.FromImage(percent_graph)) { int a = Convert.ToInt32((percent_graph.Width / epochs) * epoch); int b = Convert.ToInt32(percent_graph.Height - percent_graph.Height * error_percent); int c = Convert.ToInt32(percent_graph.Width / epochs); int d = Convert.ToInt32(percent_graph.Height * error_percent); g.DrawRectangle(new Pen(Color.Green, 1), new Rectangle(a, b, c, d)); g.FillRectangle(new SolidBrush(Color.Green), new Rectangle(a, b, c, d)); } percent_box.Image = percent_graph; percent_box.Refresh(); } /*ouble[] input = {1.0, 0.5, -1.5}; * double[] answer = {0.20813168, 0.22549015, 0.99742175}; * double index = 0.3; * double[] output = new double[3]; * output = network.Query(input); * network.Train(input, answer, index); * output = network.Query(input);*/ }