Click here to Skip to main content
11,417,750 members (47,928 online)
Click here to Skip to main content
Add your own
alternative version

Designing And Implementing A Neural Network Library For Handwriting Detection, Image Analysis etc.- The BrainNet Library - Full Code, Simplified Theory, Full Illustration, And Examples

, 21 Oct 2009 CPOL
This article will explain the actual concepts and implementation of Backward Propagation Neural Networks very easily - see project code and samples, like a simple pattern detector, a hand writing detection pad, an xml based neural network processing language etc in the source zip.
brainnet_src.zip
BrainNetIntro
NeuralGate
NeuralGate.vbproj.user
PatternDetector
PatternDetector.vbproj.user
TestImages
a1.bmp
b1.bmp
b2.bmp
c1.bmp
c2.bmp
D.bmp
D1.bmp
D2.bmp
E1.bmp
E2.bmp
Thumbs.db
Framework
BrainNet.suo
NetworkDataModel
Network.xsx
NeuralXML.xsx
NetworkIO.xsx
NeuralFramework.vbproj.user
nxml
nxml.vbproj.user
BrainNet.suo
Doc
Documentation.chm
Samples
HandWriting
HandWriting.suo
HandWriting.vbproj.user
Public Class frmWriteDetection
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents wgMain As HandWriting.PatternGrid
    Friend WithEvents txtTrain As System.Windows.Forms.TextBox
    Friend WithEvents cmdDetect As System.Windows.Forms.Button
    Friend WithEvents txtDetect As System.Windows.Forms.TextBox
    Friend WithEvents Initialize As System.Windows.Forms.Button
    Friend WithEvents txtGridSize As System.Windows.Forms.TextBox
    Friend WithEvents txtUnitSize As System.Windows.Forms.TextBox
    Friend WithEvents cmdAddState As System.Windows.Forms.Button
    Friend WithEvents cmdTrain As System.Windows.Forms.Button
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents TabPage3 As System.Windows.Forms.TabPage
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents cmdClearStates As System.Windows.Forms.Button
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents TabControl2 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage4 As System.Windows.Forms.TabPage
    Friend WithEvents TabPage5 As System.Windows.Forms.TabPage
    Friend WithEvents lvReport As System.Windows.Forms.ListView
    Friend WithEvents txtStates As System.Windows.Forms.TextBox
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Panel2 As System.Windows.Forms.Panel
    Friend WithEvents Panel3 As System.Windows.Forms.Panel
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents btnClear As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.wgMain = New HandWriting.PatternGrid()
        Me.txtTrain = New System.Windows.Forms.TextBox()
        Me.cmdAddState = New System.Windows.Forms.Button()
        Me.cmdDetect = New System.Windows.Forms.Button()
        Me.txtDetect = New System.Windows.Forms.TextBox()
        Me.txtGridSize = New System.Windows.Forms.TextBox()
        Me.txtUnitSize = New System.Windows.Forms.TextBox()
        Me.Initialize = New System.Windows.Forms.Button()
        Me.cmdTrain = New System.Windows.Forms.Button()
        Me.TabControl1 = New System.Windows.Forms.TabControl()
        Me.TabPage1 = New System.Windows.Forms.TabPage()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.TabPage2 = New System.Windows.Forms.TabPage()
        Me.txtStates = New System.Windows.Forms.TextBox()
        Me.cmdClearStates = New System.Windows.Forms.Button()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.TabPage3 = New System.Windows.Forms.TabPage()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.TabControl2 = New System.Windows.Forms.TabControl()
        Me.TabPage4 = New System.Windows.Forms.TabPage()
        Me.btnClear = New System.Windows.Forms.Button()
        Me.TabPage5 = New System.Windows.Forms.TabPage()
        Me.lvReport = New System.Windows.Forms.ListView()
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.Panel2 = New System.Windows.Forms.Panel()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Panel3 = New System.Windows.Forms.Panel()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.TabControl1.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.TabPage2.SuspendLayout()
        Me.TabPage3.SuspendLayout()
        Me.TabControl2.SuspendLayout()
        Me.TabPage4.SuspendLayout()
        Me.TabPage5.SuspendLayout()
        Me.Panel2.SuspendLayout()
        Me.Panel3.SuspendLayout()
        Me.SuspendLayout()
        '
        'wgMain
        '
        Me.wgMain.BackColor = System.Drawing.Color.White
        Me.wgMain.Location = New System.Drawing.Point(16, 42)
        Me.wgMain.Name = "wgMain"
        Me.wgMain.Size = New System.Drawing.Size(120, 120)
        Me.wgMain.TabIndex = 0
        '
        'txtTrain
        '
        Me.txtTrain.Location = New System.Drawing.Point(16, 56)
        Me.txtTrain.Name = "txtTrain"
        Me.txtTrain.Size = New System.Drawing.Size(167, 20)
        Me.txtTrain.TabIndex = 1
        Me.txtTrain.Text = ""
        '
        'cmdAddState
        '
        Me.cmdAddState.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.cmdAddState.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.cmdAddState.Location = New System.Drawing.Point(16, 96)
        Me.cmdAddState.Name = "cmdAddState"
        Me.cmdAddState.Size = New System.Drawing.Size(80, 24)
        Me.cmdAddState.TabIndex = 2
        Me.cmdAddState.Text = "Add State"
        '
        'cmdDetect
        '
        Me.cmdDetect.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.cmdDetect.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.cmdDetect.Location = New System.Drawing.Point(17, 98)
        Me.cmdDetect.Name = "cmdDetect"
        Me.cmdDetect.Size = New System.Drawing.Size(104, 24)
        Me.cmdDetect.TabIndex = 4
        Me.cmdDetect.Text = "Detect"
        '
        'txtDetect
        '
        Me.txtDetect.Location = New System.Drawing.Point(16, 56)
        Me.txtDetect.Name = "txtDetect"
        Me.txtDetect.Size = New System.Drawing.Size(120, 20)
        Me.txtDetect.TabIndex = 3
        Me.txtDetect.Text = ""
        '
        'txtGridSize
        '
        Me.txtGridSize.Location = New System.Drawing.Point(16, 152)
        Me.txtGridSize.Name = "txtGridSize"
        Me.txtGridSize.Size = New System.Drawing.Size(120, 20)
        Me.txtGridSize.TabIndex = 6
        Me.txtGridSize.Text = "10"
        '
        'txtUnitSize
        '
        Me.txtUnitSize.Location = New System.Drawing.Point(16, 64)
        Me.txtUnitSize.Name = "txtUnitSize"
        Me.txtUnitSize.Size = New System.Drawing.Size(120, 20)
        Me.txtUnitSize.TabIndex = 7
        Me.txtUnitSize.Text = "20"
        '
        'Initialize
        '
        Me.Initialize.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.Initialize.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.Initialize.Location = New System.Drawing.Point(16, 201)
        Me.Initialize.Name = "Initialize"
        Me.Initialize.Size = New System.Drawing.Size(104, 24)
        Me.Initialize.TabIndex = 8
        Me.Initialize.Text = "Initialize"
        '
        'cmdTrain
        '
        Me.cmdTrain.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.cmdTrain.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.cmdTrain.Location = New System.Drawing.Point(16, 304)
        Me.cmdTrain.Name = "cmdTrain"
        Me.cmdTrain.Size = New System.Drawing.Size(174, 24)
        Me.cmdTrain.TabIndex = 9
        Me.cmdTrain.Text = "Train 1000 Times"
        '
        'TabControl1
        '
        Me.TabControl1.Appearance = System.Windows.Forms.TabAppearance.FlatButtons
        Me.TabControl1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabPage1, Me.TabPage2, Me.TabPage3})
        Me.TabControl1.Location = New System.Drawing.Point(9, 49)
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.Size = New System.Drawing.Size(224, 368)
        Me.TabControl1.TabIndex = 10
        '
        'TabPage1
        '
        Me.TabPage1.BackColor = System.Drawing.Color.White
        Me.TabPage1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, Me.txtUnitSize, Me.txtGridSize, Me.Initialize})
        Me.TabPage1.Location = New System.Drawing.Point(4, 25)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Size = New System.Drawing.Size(216, 339)
        Me.TabPage1.TabIndex = 0
        Me.TabPage1.Text = "Initialize"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 120)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(168, 24)
        Me.Label2.TabIndex = 10
        Me.Label2.Text = "Grid Square Size"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 40)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(168, 24)
        Me.Label1.TabIndex = 9
        Me.Label1.Text = "Pixel Size"
        '
        'TabPage2
        '
        Me.TabPage2.BackColor = System.Drawing.Color.White
        Me.TabPage2.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtStates, Me.cmdClearStates, Me.Label3, Me.cmdAddState, Me.txtTrain, Me.cmdTrain})
        Me.TabPage2.Location = New System.Drawing.Point(4, 25)
        Me.TabPage2.Name = "TabPage2"
        Me.TabPage2.Size = New System.Drawing.Size(216, 339)
        Me.TabPage2.TabIndex = 1
        Me.TabPage2.Text = "Training"
        '
        'txtStates
        '
        Me.txtStates.Location = New System.Drawing.Point(16, 136)
        Me.txtStates.Multiline = True
        Me.txtStates.Name = "txtStates"
        Me.txtStates.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.txtStates.Size = New System.Drawing.Size(184, 160)
        Me.txtStates.TabIndex = 12
        Me.txtStates.Text = ""
        '
        'cmdClearStates
        '
        Me.cmdClearStates.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.cmdClearStates.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.cmdClearStates.Location = New System.Drawing.Point(112, 96)
        Me.cmdClearStates.Name = "cmdClearStates"
        Me.cmdClearStates.Size = New System.Drawing.Size(80, 24)
        Me.cmdClearStates.TabIndex = 11
        Me.cmdClearStates.Text = "Clear States"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 24)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(168, 24)
        Me.Label3.TabIndex = 10
        Me.Label3.Text = "Enter An ASCII Character"
        '
        'TabPage3
        '
        Me.TabPage3.BackColor = System.Drawing.Color.White
        Me.TabPage3.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label4, Me.txtDetect, Me.cmdDetect})
        Me.TabPage3.Location = New System.Drawing.Point(4, 25)
        Me.TabPage3.Name = "TabPage3"
        Me.TabPage3.Size = New System.Drawing.Size(216, 339)
        Me.TabPage3.TabIndex = 2
        Me.TabPage3.Text = "Detection"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 24)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(168, 24)
        Me.Label4.TabIndex = 11
        Me.Label4.Text = "Detected ASCII Character Value"
        '
        'TabControl2
        '
        Me.TabControl2.Appearance = System.Windows.Forms.TabAppearance.FlatButtons
        Me.TabControl2.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabPage4, Me.TabPage5})
        Me.TabControl2.Location = New System.Drawing.Point(251, 49)
        Me.TabControl2.Name = "TabControl2"
        Me.TabControl2.SelectedIndex = 0
        Me.TabControl2.Size = New System.Drawing.Size(480, 368)
        Me.TabControl2.TabIndex = 11
        '
        'TabPage4
        '
        Me.TabPage4.AutoScroll = True
        Me.TabPage4.BackColor = System.Drawing.SystemColors.Control
        Me.TabPage4.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnClear, Me.wgMain})
        Me.TabPage4.Location = New System.Drawing.Point(4, 25)
        Me.TabPage4.Name = "TabPage4"
        Me.TabPage4.Size = New System.Drawing.Size(472, 339)
        Me.TabPage4.TabIndex = 0
        Me.TabPage4.Text = "Writing Grid"
        '
        'btnClear
        '
        Me.btnClear.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.btnClear.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnClear.Location = New System.Drawing.Point(19, 10)
        Me.btnClear.Name = "btnClear"
        Me.btnClear.Size = New System.Drawing.Size(104, 24)
        Me.btnClear.TabIndex = 9
        Me.btnClear.Text = "Clear Pattern"
        '
        'TabPage5
        '
        Me.TabPage5.Controls.AddRange(New System.Windows.Forms.Control() {Me.lvReport})
        Me.TabPage5.Location = New System.Drawing.Point(4, 25)
        Me.TabPage5.Name = "TabPage5"
        Me.TabPage5.Size = New System.Drawing.Size(472, 339)
        Me.TabPage5.TabIndex = 1
        Me.TabPage5.Text = "Report"
        '
        'lvReport
        '
        Me.lvReport.Location = New System.Drawing.Point(8, 8)
        Me.lvReport.Name = "lvReport"
        Me.lvReport.Size = New System.Drawing.Size(457, 328)
        Me.lvReport.TabIndex = 6
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.Color.Gray
        Me.Panel1.Location = New System.Drawing.Point(239, 6)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(1, 422)
        Me.Panel1.TabIndex = 12
        '
        'Panel2
        '
        Me.Panel2.BackColor = System.Drawing.Color.Gray
        Me.Panel2.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label5})
        Me.Panel2.Location = New System.Drawing.Point(4, 9)
        Me.Panel2.Name = "Panel2"
        Me.Panel2.Size = New System.Drawing.Size(230, 30)
        Me.Panel2.TabIndex = 13
        '
        'Label5
        '
        Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label5.ForeColor = System.Drawing.Color.White
        Me.Label5.Location = New System.Drawing.Point(7, 5)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(170, 22)
        Me.Label5.TabIndex = 0
        Me.Label5.Text = "Neural Network"
        '
        'Panel3
        '
        Me.Panel3.BackColor = System.Drawing.Color.Gray
        Me.Panel3.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label6})
        Me.Panel3.Location = New System.Drawing.Point(247, 9)
        Me.Panel3.Name = "Panel3"
        Me.Panel3.Size = New System.Drawing.Size(480, 30)
        Me.Panel3.TabIndex = 14
        '
        'Label6
        '
        Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label6.ForeColor = System.Drawing.Color.White
        Me.Label6.Location = New System.Drawing.Point(12, 4)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(170, 22)
        Me.Label6.TabIndex = 1
        Me.Label6.Text = "Writing Grid"
        '
        'frmWriteDetection
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(736, 446)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel3, Me.Panel2, Me.Panel1, Me.TabControl2, Me.TabControl1})
        Me.MaximizeBox = False
        Me.Name = "frmWriteDetection"
        Me.Text = "Write Detection"
        Me.TabControl1.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.TabPage2.ResumeLayout(False)
        Me.TabPage3.ResumeLayout(False)
        Me.TabControl2.ResumeLayout(False)
        Me.TabPage4.ResumeLayout(False)
        Me.TabPage5.ResumeLayout(False)
        Me.Panel2.ResumeLayout(False)
        Me.Panel3.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub frmWriteDetection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        wgMain.Initialize(New Size(20, 20), 10, 8)
    End Sub


    Private Sub cmdDetect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDetect.Click

        Me.txtDetect.Text = Chr(wgMain.Detect())
        wgMain.Report(Me.lvReport)

    End Sub

    Private Sub Initialize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Initialize.Click
        Dim s As Integer = Val(Me.txtUnitSize.Text)
        wgMain.Initialize(New Size(s, s), Val(Me.txtGridSize.Text), 8)
    End Sub


    Private Sub cmdAddState_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddState.Click


        Try
            wgMain.AddStateFromGrid(Asc(Me.txtTrain.Text))

            Dim td As BrainNet.NeuralFramework.TrainingData = CType(wgMain.TrainingStates(wgMain.TrainingStates.Count - 1), BrainNet.NeuralFramework.TrainingData)

            Dim instr As String = "", outstr As String = ""

            Dim d As Double

            For Each d In td.Inputs
                instr = instr & d
            Next
            For Each d In td.Outputs
                outstr = outstr & d
            Next

            Me.txtStates.Text = txtStates.Text & instr & "=" & outstr & vbCrLf
        Catch ex As Exception
            MsgBox("Invalid inputs. " & ex.Message)
        End Try


    End Sub

    Private Sub cmdTrain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTrain.Click
        wgMain.Train(1000)

    End Sub

    Private Sub txtTrain_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTrain.TextChanged

    End Sub

    Private Sub cmdClearStates_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearStates.Click
        Me.txtStates.Text = ""
        Me.wgMain.ClearStates()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        wgMain.ClearDrawing()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.4 | Last Updated 21 Oct 2009
Article Copyright 2006 by Anoop Madhusudanan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid