📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using xpidea.neuro.net.patterns;
using xpidea.neuro.net.backprop;
namespace xpidea.neuro.net.examples.backprop.ocr
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
static public bool IsTerminated;
public class OCRNetwork: BackPropagationRPROPNetwork
{
private Form1 owner;
public OCRNetwork (Form1 owner, int[] nodesInEachLayer):base(nodesInEachLayer)
{
this.owner = owner;
}
private int OutputPatternIndex(Pattern pattern)
{
for (int i = 0; i<pattern.OutputsCount;i++)
if (pattern.Output[i] == 1 )
return i;
return -1;
}
public void AddNoiseToInputPattern(int levelPercent)
{
int i = ((NodesInLayer(0) - 1) * levelPercent)/100;
while (i > 0)
{
nodes[(int)(BackPropagationNetwork.Random(0, NodesInLayer(0) - 1))].Value = BackPropagationNetwork.Random(0, 100);
i--;
}
}
public int BestNodeIndex
{
get
{
int result = -1;
double aMaxNodeValue = 0;
double aMinError = double.PositiveInfinity;
for (int i = 0; i< this.OutputNodesCount;i++)
{
NeuroNode node = OutputNode(i);
if ((node.Value > aMaxNodeValue)||((node.Value >= aMaxNodeValue)&&(node.Error < aMinError)))
{
aMaxNodeValue = node.Value;
aMinError = node.Error;
result = i;
}
}
return result;
}
}
public override void Train(PatternsCollection patterns)
{
int iteration = 0;
if (patterns != null)
{
double error = 0;
int good = 0;
while (good < patterns.Count) // Train until all patterns are correct
{
if (Form1.IsTerminated) return;
error = 0;
owner.progressBar1.Value = good;
owner.label16.Text = "Training progress: " + ((good * 100)/owner.progressBar1.Maximum).ToString() + "%";
good = 0;
for (int i = 0; i<patterns.Count; i++)
{
for (int k = 0; k<NodesInLayer(0); k++)
nodes[k].Value = patterns[i].Input[k];
AddNoiseToInputPattern(owner.trackBar3.Value);
this.Run();
for (int k = 0;k< this.OutputNodesCount;k++)
{
error += Math.Abs(this.OutputNode(k).Error);
this.OutputNode(k).Error = patterns[i].Output[k];
}
this.Learn();
if (BestNodeIndex == OutputPatternIndex(patterns[i]))
good++;
iteration ++;
Application.DoEvents();
}
foreach (NeuroLink link in links) ((EpochBackPropagationLink)link).Epoch(patterns.Count);
if ((iteration%2) == 0)
owner.label17.Text = "AVG Error: " + (error / OutputNodesCount).ToString() + " Iteration: " + iteration.ToString();
}
owner.label17.Text = "AVG Error: " + (error / OutputNodesCount).ToString() + " Iteration: " + iteration.ToString();
}
}
}
public static int aMatrixDim = 10;
public static byte aFirstChar = (byte)'A';
public static byte aLastChar = (byte)'z';
public static int aCharsCount = aLastChar - aFirstChar +1;
public PatternsCollection trainingPatterns;
public OCRNetwork backpropNetwork;
#region Variables
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.TabPage tabPage3;
private System.Windows.Forms.TabPage tabPage4;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.TrackBar trackBar3;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.TrackBar trackBar4;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label label16;
private System.Windows.Forms.Label label17;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
#endregion
#region OtherJunk
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
label5.Text = "";
for (int i=0; i<aCharsCount; i++)
label5.Text += Convert.ToChar(aFirstChar + i) + " ";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.button1 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.button2 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.tabPage3 = new System.Windows.Forms.TabPage();
this.label17 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.label9 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.trackBar3 = new System.Windows.Forms.TrackBar();
this.button5 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.tabPage4 = new System.Windows.Forms.TabPage();
this.label16 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.trackBar4 = new System.Windows.Forms.TrackBar();
this.label4 = new System.Windows.Forms.Label();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
this.tabPage3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.trackBar3)).BeginInit();
this.tabPage4.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.trackBar4)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.pictureBox1.Location = new System.Drawing.Point(392, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(200, 266);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// tabControl1
//
this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.tabPage1,
this.tabPage2,
this.tabPage3,
this.tabPage4});
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(392, 266);
this.tabControl1.TabIndex = 1;
this.tabControl1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.tabControl1_KeyPress);
//
// tabPage1
//
this.tabPage1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.label5,
this.label1});
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(384, 240);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "Step 1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(112, 192);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(152, 24);
this.button1.TabIndex = 2;
this.button1.Text = "Generate training patterns";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label5
//
this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label5.Location = new System.Drawing.Point(16, 48);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(352, 104);
this.label5.TabIndex = 1;
this.label5.Text = "label5";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.InactiveCaption;
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.SystemColors.Window;
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(384, 32);
this.label1.TabIndex = 0;
this.label1.Text = " Step1: Generate neural network training patterns";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// tabPage2
//
this.tabPage2.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.label2});
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Size = new System.Drawing.Size(384, 240);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "Step 2";
//
// button2
//
this.button2.Location = new System.Drawing.Point(88, 128);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(200, 24);
this.button2.TabIndex = 6;
this.button2.Text = "Create the network";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.BackColor = System.Drawing.SystemColors.InactiveCaption;
this.label2.Dock = System.Windows.Forms.DockStyle.Top;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.ForeColor = System.Drawing.SystemColors.Window;
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(384, 32);
this.label2.TabIndex = 1;
this.label2.Text = " Step2: Create Backpropagation Neural Network";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// tabPage3
//
this.tabPage3.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label17,
this.progressBar1,
this.label9,
this.label8,
this.trackBar3,
this.button5,
this.button4,
this.button3,
this.label3});
this.tabPage3.Location = new System.Drawing.Point(4, 22);
this.tabPage3.Name = "tabPage3";
this.tabPage3.Size = new System.Drawing.Size(384, 240);
this.tabPage3.TabIndex = 2;
this.tabPage3.Text = "Step 3";
//
// label17
//
this.label17.Dock = System.Windows.Forms.DockStyle.Bottom;
this.label17.Location = new System.Drawing.Point(0, 200);
this.label17.Name = "label17";
this.label17.Size = new System.Drawing.Size(384, 16);
this.label17.TabIndex = 9;
this.label17.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// progressBar1
//
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.progressBar1.Location = new System.Drawing.Point(0, 216);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(384, 24);
this.progressBar1.TabIndex = 8;
//
// label9
//
this.label9.Location = new System.Drawing.Point(184, 120);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(176, 64);
this.label9.TabIndex = 7;
//
// label8
//
this.label8.Location = new System.Drawing.Point(184, 40);
this.label8.Name = "label8";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -