Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / UML

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

Rate me:
Please Sign up or sign in to vote.
4.76/5 (97 votes)
21 Oct 2009CPOL26 min read 373.2K   7.8K   356  
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.
Imports BrainNet.NeuralFramework

Public Class frmHandWriting
    Inherits System.Windows.Forms.Form

    Dim bmap As New Bitmap(100, 100)

    Dim myBnn As NeuralNetwork
    Dim ifMoving As Boolean = False

#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 picMain As System.Windows.Forms.PictureBox
    Friend WithEvents txtTrain As System.Windows.Forms.TextBox
    Friend WithEvents btnTrain As System.Windows.Forms.Button
    Friend WithEvents lblTrain As System.Windows.Forms.Label
    Friend WithEvents lblDetect As System.Windows.Forms.Label
    Friend WithEvents cmdDetect As System.Windows.Forms.Button
    Friend WithEvents txtDetect As System.Windows.Forms.TextBox
    Friend WithEvents gbMain As System.Windows.Forms.GroupBox
    Friend WithEvents cmdClose As System.Windows.Forms.Button
    Friend WithEvents cmdClear As System.Windows.Forms.Button
    Friend WithEvents tbMain As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents tbPage2 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 txtPattern As System.Windows.Forms.TextBox
    Friend WithEvents lvReport As System.Windows.Forms.ListView
    Friend WithEvents txtOutput As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.picMain = New System.Windows.Forms.PictureBox()
        Me.txtTrain = New System.Windows.Forms.TextBox()
        Me.btnTrain = New System.Windows.Forms.Button()
        Me.lblTrain = New System.Windows.Forms.Label()
        Me.lblDetect = New System.Windows.Forms.Label()
        Me.cmdDetect = New System.Windows.Forms.Button()
        Me.txtDetect = New System.Windows.Forms.TextBox()
        Me.gbMain = New System.Windows.Forms.GroupBox()
        Me.tbMain = New System.Windows.Forms.TabControl()
        Me.TabPage1 = New System.Windows.Forms.TabPage()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.tbPage2 = New System.Windows.Forms.TabPage()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.cmdClear = New System.Windows.Forms.Button()
        Me.cmdClose = New System.Windows.Forms.Button()
        Me.txtPattern = New System.Windows.Forms.TextBox()
        Me.lvReport = New System.Windows.Forms.ListView()
        Me.txtOutput = New System.Windows.Forms.TextBox()
        Me.gbMain.SuspendLayout()
        Me.tbMain.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.tbPage2.SuspendLayout()
        Me.SuspendLayout()
        '
        'picMain
        '
        Me.picMain.BackColor = System.Drawing.Color.White
        Me.picMain.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.picMain.Location = New System.Drawing.Point(16, 56)
        Me.picMain.Name = "picMain"
        Me.picMain.Size = New System.Drawing.Size(99, 99)
        Me.picMain.TabIndex = 0
        Me.picMain.TabStop = False
        '
        'txtTrain
        '
        Me.txtTrain.Location = New System.Drawing.Point(80, 120)
        Me.txtTrain.Name = "txtTrain"
        Me.txtTrain.Size = New System.Drawing.Size(64, 20)
        Me.txtTrain.TabIndex = 1
        Me.txtTrain.Text = ""
        '
        'btnTrain
        '
        Me.btnTrain.Location = New System.Drawing.Point(80, 152)
        Me.btnTrain.Name = "btnTrain"
        Me.btnTrain.Size = New System.Drawing.Size(64, 24)
        Me.btnTrain.TabIndex = 2
        Me.btnTrain.Text = "Train"
        '
        'lblTrain
        '
        Me.lblTrain.Location = New System.Drawing.Point(8, 16)
        Me.lblTrain.Name = "lblTrain"
        Me.lblTrain.Size = New System.Drawing.Size(136, 88)
        Me.lblTrain.TabIndex = 3
        Me.lblTrain.Text = "To Train the network, draw the letter in the 'Pattern' box using your mouse. Then" & _
        " enter the character you drawn in the below text box, and click 'Train' button"
        '
        'lblDetect
        '
        Me.lblDetect.Location = New System.Drawing.Point(8, 16)
        Me.lblDetect.Name = "lblDetect"
        Me.lblDetect.Size = New System.Drawing.Size(128, 80)
        Me.lblDetect.TabIndex = 6
        Me.lblDetect.Text = "To Run the network, draw a pattern on the Pattern picture box, and click Detect"
        '
        'cmdDetect
        '
        Me.cmdDetect.Location = New System.Drawing.Point(80, 152)
        Me.cmdDetect.Name = "cmdDetect"
        Me.cmdDetect.Size = New System.Drawing.Size(56, 24)
        Me.cmdDetect.TabIndex = 5
        Me.cmdDetect.Text = "Detect"
        '
        'txtDetect
        '
        Me.txtDetect.Location = New System.Drawing.Point(80, 120)
        Me.txtDetect.Name = "txtDetect"
        Me.txtDetect.Size = New System.Drawing.Size(56, 20)
        Me.txtDetect.TabIndex = 4
        Me.txtDetect.Text = ""
        '
        'gbMain
        '
        Me.gbMain.Controls.AddRange(New System.Windows.Forms.Control() {Me.tbMain, Me.picMain, Me.Label1, Me.cmdClear})
        Me.gbMain.Location = New System.Drawing.Point(8, 8)
        Me.gbMain.Name = "gbMain"
        Me.gbMain.Size = New System.Drawing.Size(320, 248)
        Me.gbMain.TabIndex = 7
        Me.gbMain.TabStop = False
        '
        'tbMain
        '
        Me.tbMain.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabPage1, Me.tbPage2})
        Me.tbMain.Location = New System.Drawing.Point(136, 16)
        Me.tbMain.Name = "tbMain"
        Me.tbMain.SelectedIndex = 0
        Me.tbMain.Size = New System.Drawing.Size(168, 216)
        Me.tbMain.TabIndex = 7
        '
        'TabPage1
        '
        Me.TabPage1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.txtTrain, Me.lblTrain, Me.btnTrain})
        Me.TabPage1.Location = New System.Drawing.Point(4, 22)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Size = New System.Drawing.Size(160, 190)
        Me.TabPage1.TabIndex = 0
        Me.TabPage1.Text = "Train"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 120)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(64, 16)
        Me.Label2.TabIndex = 5
        Me.Label2.Text = "Character"
        '
        'tbPage2
        '
        Me.tbPage2.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3, Me.lblDetect, Me.txtDetect, Me.cmdDetect})
        Me.tbPage2.Location = New System.Drawing.Point(4, 22)
        Me.tbPage2.Name = "tbPage2"
        Me.tbPage2.Size = New System.Drawing.Size(160, 190)
        Me.tbPage2.TabIndex = 1
        Me.tbPage2.Text = "Run"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(8, 120)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(64, 29)
        Me.Label3.TabIndex = 7
        Me.Label3.Text = "Detected Value"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(64, 16)
        Me.Label1.TabIndex = 4
        Me.Label1.Text = "Pattern :"
        '
        'cmdClear
        '
        Me.cmdClear.Location = New System.Drawing.Point(16, 200)
        Me.cmdClear.Name = "cmdClear"
        Me.cmdClear.Size = New System.Drawing.Size(96, 24)
        Me.cmdClear.TabIndex = 9
        Me.cmdClear.Text = "Clear"
        '
        'cmdClose
        '
        Me.cmdClose.Location = New System.Drawing.Point(664, 272)
        Me.cmdClose.Name = "cmdClose"
        Me.cmdClose.Size = New System.Drawing.Size(96, 24)
        Me.cmdClose.TabIndex = 8
        Me.cmdClose.Text = "Close"
        '
        'txtPattern
        '
        Me.txtPattern.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtPattern.ForeColor = System.Drawing.Color.Red
        Me.txtPattern.Location = New System.Drawing.Point(8, 264)
        Me.txtPattern.Multiline = True
        Me.txtPattern.Name = "txtPattern"
        Me.txtPattern.Size = New System.Drawing.Size(312, 232)
        Me.txtPattern.TabIndex = 9
        Me.txtPattern.Text = ""
        '
        'lvReport
        '
        Me.lvReport.Location = New System.Drawing.Point(336, 16)
        Me.lvReport.Name = "lvReport"
        Me.lvReport.Size = New System.Drawing.Size(416, 240)
        Me.lvReport.TabIndex = 10
        '
        'txtOutput
        '
        Me.txtOutput.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtOutput.ForeColor = System.Drawing.Color.Red
        Me.txtOutput.Location = New System.Drawing.Point(336, 264)
        Me.txtOutput.Multiline = True
        Me.txtOutput.Name = "txtOutput"
        Me.txtOutput.Size = New System.Drawing.Size(312, 232)
        Me.txtOutput.TabIndex = 11
        Me.txtOutput.Text = ""
        '
        'frmHandWriting
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(770, 536)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtOutput, Me.lvReport, Me.txtPattern, Me.cmdClose, Me.gbMain})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "frmHandWriting"
        Me.Text = "HandWriting Recognition"
        Me.gbMain.ResumeLayout(False)
        Me.tbMain.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.tbPage2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub frmHandWriting_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        myBnn = New NeuralNetwork()
        myBnn.AddInputLayer(100)
        myBnn.AddHiddenLayer(100)
        myBnn.AddOutputLayer(8)
        myBnn.ConnectLayers()



    End Sub

    Private Sub picMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picMain.Click

    End Sub

    Private Sub picMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMain.MouseDown
        If e.Button = MouseButtons.Left Then ifMoving = True
    End Sub
    Private Sub picMain_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMain.MouseUp
        ifMoving = False
    End Sub

    Private Sub picMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picMain.MouseMove
        If ifMoving Then
            Try
                bmap.SetPixel(e.X, e.Y, Color.Red)
                bmap.SetPixel(e.X - 1, e.Y - 1, Color.Red)
                bmap.SetPixel(e.X - 1, e.Y, Color.Red)
                bmap.SetPixel(e.X, e.Y - 1, Color.Red)
                bmap.SetPixel(e.X + 1, e.Y + 1, Color.Red)
                bmap.SetPixel(e.X + 1, e.Y, Color.Red)
                bmap.SetPixel(e.X, e.Y + 1, Color.Red)


                picMain.Invalidate()
            Catch
            End Try
        End If
    End Sub

    Private Sub picMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picMain.Paint
        e.Graphics.DrawImage(CType(bmap, Image), 0, 0)

    End Sub

    Private Sub btnTrain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTrain.Click
        Dim times As Long
        Dim sbuf As String

        'Check whether our input textbox has a valid character

        Dim inputChar As String = Trim(txtTrain.Text)


        If inputChar.Length <> 1 Then
            MsgBox("Enter a valid character in the text box")
            Exit Sub
        End If

        inputChar.ToUpper()

        If Asc(inputChar) < Asc("A") Or Asc(inputChar) > Asc("G") Then
            MsgBox("Enter a valid character between A and G in the text box")
            Exit Sub
        End If

        Dim newBmap As New Bitmap(bmap, 10, 10)


        'Just print the pattern to a textbox for us to view it
        sbuf = ""

        Dim i, j As Integer

        For i = 0 To 10 - 1
            For j = 0 To 10 - 1

                If newBmap.GetPixel(i, j).R = 255 Then
                    sbuf = sbuf & "1"

                Else
                    sbuf = sbuf & "0"

                End If
            Next
            sbuf = sbuf & vbCrLf

        Next

        Me.txtPattern.Text = sbuf

        newBmap.Dispose()

        'Start the training
        For times = 0 To 1000
            Train()
        Next
        Utility.Report(Me.lvReport, myBnn)


    End Sub



    Sub Run()
        Dim i As Integer, j As Integer
        Dim inputs As New ArrayList()


        Dim newBmap As New Bitmap(bmap, 10, 10)

        For i = 0 To 10 - 1
            For j = 0 To 10 - 1
                If newBmap.GetPixel(i, j).R = 255 Then
                    inputs.Add(1)
                Else
                    inputs.Add(0)
                End If
            Next
        Next

        newBmap.Dispose()

        myBnn.RunNetwork(inputs)
        Utility.Report(Me.lvReport, myBnn)





    End Sub


    Sub Train()

        Dim td As New TrainingData()

        Dim newBmap As New Bitmap(bmap, 10, 10)

        Dim i As Integer, j As Integer


        For i = 0 To 10 - 1
            For j = 0 To 10 - 1

                If newBmap.GetPixel(i, j).R = 255 Then
                    td.Inputs.Add(1)
                Else
                    td.Inputs.Add(0)
                End If
            Next
        Next


        Dim bin As String
        Dim inputChar As String = Trim(txtTrain.Text)



        bin = BinaryFromNumber(Asc(inputChar))


        For i = 0 To bin.Length - 1
            If bin.Chars(bin.Length - 1 - i) = "1" Then
                td.Outputs.Add(1)
            Else
                td.Outputs.Add(0)
            End If
        Next


        myBnn.TrainNetwork(td)

        newBmap.Dispose()







    End Sub


    Function BinaryFromNumber(ByVal bt As Byte) As String

        Dim binStr As String = ""

        Dim i
        For i = 0 To 7
            If ((2 ^ i) And bt) = (2 ^ i) Then
                binStr = "1" & binStr
            Else
                binStr = "0" & binStr
            End If

        Next

        Return binStr

    End Function

    Function NumberFromBinary(ByVal bin As String) As Integer

        Dim number As Integer = 0

        Dim i, bit
        For i = 0 To bin.Length - 1
            If bin.Chars(bin.Length - 1 - i) = "1" Then
                bit = 1
            Else
                bit = 0
            End If

            number = number + ((2 ^ i) * bit)

        Next

        Return number

    End Function

    Private Sub gbMain_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gbMain.Enter

    End Sub

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
        Me.bmap = New System.Drawing.Bitmap(100, 100)
        picMain.Invalidate()

    End Sub

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

        'Just print the pattern to a textbox for us to view it
        Dim sbuf As String = ""

        Dim i, j As Integer

        Dim newBmap As New Bitmap(bmap, 10, 10)
        For i = 0 To 10 - 1
            For j = 0 To 10 - 1

                Try
                    If newBmap.GetPixel(i, j).R = 255 Then
                        sbuf = sbuf & "1"
                    Else
                        sbuf = sbuf & "0"
                    End If
                Catch
                End Try
            Next

            sbuf = sbuf & vbCrLf

        Next

        Me.txtPattern.Text = sbuf

        newBmap.Dispose()


        Run()
        Dim output As ArrayList
        output = myBnn.GetOutput()
        Dim str As String = ""
        Dim val As Double

        For Each val In output
            str = str & Math.Round(val)
        Next


        Me.txtOutput.Text = NumberFromBinary(str)


    End Sub


    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Me.Close()

    End Sub

    Private Sub tbMain_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbMain.SelectedIndexChanged

    End Sub

    Private Sub lblTrain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTrain.Click

    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)


Written By
Architect
India India
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#

Comments and Discussions