Click here to Skip to main content
15,881,380 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 372.7K   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.
Public Class frmMain
    Inherits System.Windows.Forms.Form


    Private StopTraining 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 imlMain As System.Windows.Forms.ImageList
    Friend WithEvents tbMain As System.Windows.Forms.TabControl
    Friend WithEvents tabTraining As System.Windows.Forms.TabPage
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents cmdTrain As System.Windows.Forms.Button
    Friend WithEvents cmdClearAll As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents cmdLoad As System.Windows.Forms.Button
    Friend WithEvents cmdSave As System.Windows.Forms.Button
    Friend WithEvents cmdExit As System.Windows.Forms.Button
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents cmdDetect As System.Windows.Forms.Button
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents picImgDetect As System.Windows.Forms.PictureBox
    Friend WithEvents lvMain As System.Windows.Forms.ListView
    Friend WithEvents txtTrainTimes As System.Windows.Forms.TextBox
    Friend WithEvents pbTrain As System.Windows.Forms.ProgressBar
    Friend WithEvents cmdBrowseDetect As System.Windows.Forms.Button
    Friend WithEvents txtAsciiDetect As System.Windows.Forms.TextBox
    Friend WithEvents txtPatternDetect As System.Windows.Forms.TextBox
    Friend WithEvents cmdRestart As System.Windows.Forms.Button
    Friend WithEvents pnTrain As System.Windows.Forms.Panel
    Friend WithEvents lblTrain As System.Windows.Forms.Label
    Friend WithEvents cmdCancel As System.Windows.Forms.Button
    Friend WithEvents lblTrainInfo As System.Windows.Forms.Label
    Friend WithEvents lblTrainStart As System.Windows.Forms.Label
    Friend WithEvents pnNetwork As System.Windows.Forms.Panel
    Friend WithEvents picImgTrain As System.Windows.Forms.PictureBox
    Friend WithEvents cmdAddToQueue As System.Windows.Forms.Button
    Friend WithEvents cmdBrowse As System.Windows.Forms.Button
    Friend WithEvents txtAsciiValue As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents lblImage As System.Windows.Forms.Label
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox4 As System.Windows.Forms.GroupBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMain))
        Me.imlMain = New System.Windows.Forms.ImageList(Me.components)
        Me.tbMain = New System.Windows.Forms.TabControl()
        Me.tabTraining = New System.Windows.Forms.TabPage()
        Me.GroupBox2 = New System.Windows.Forms.GroupBox()
        Me.lblImage = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.cmdAddToQueue = New System.Windows.Forms.Button()
        Me.txtAsciiValue = New System.Windows.Forms.TextBox()
        Me.cmdBrowse = New System.Windows.Forms.Button()
        Me.picImgTrain = New System.Windows.Forms.PictureBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.txtTrainTimes = New System.Windows.Forms.TextBox()
        Me.cmdTrain = New System.Windows.Forms.Button()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.cmdClearAll = New System.Windows.Forms.Button()
        Me.lvMain = New System.Windows.Forms.ListView()
        Me.TabPage1 = New System.Windows.Forms.TabPage()
        Me.GroupBox4 = New System.Windows.Forms.GroupBox()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.txtPatternDetect = New System.Windows.Forms.TextBox()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.txtAsciiDetect = New System.Windows.Forms.TextBox()
        Me.GroupBox3 = New System.Windows.Forms.GroupBox()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.picImgDetect = New System.Windows.Forms.PictureBox()
        Me.cmdDetect = New System.Windows.Forms.Button()
        Me.cmdBrowseDetect = New System.Windows.Forms.Button()
        Me.pbTrain = New System.Windows.Forms.ProgressBar()
        Me.cmdLoad = New System.Windows.Forms.Button()
        Me.cmdSave = New System.Windows.Forms.Button()
        Me.cmdExit = New System.Windows.Forms.Button()
        Me.cmdRestart = New System.Windows.Forms.Button()
        Me.pnTrain = New System.Windows.Forms.Panel()
        Me.lblTrainStart = New System.Windows.Forms.Label()
        Me.cmdCancel = New System.Windows.Forms.Button()
        Me.lblTrainInfo = New System.Windows.Forms.Label()
        Me.lblTrain = New System.Windows.Forms.Label()
        Me.pnNetwork = New System.Windows.Forms.Panel()
        Me.tbMain.SuspendLayout()
        Me.tabTraining.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.GroupBox1.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.GroupBox4.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.pnTrain.SuspendLayout()
        Me.pnNetwork.SuspendLayout()
        Me.SuspendLayout()
        '
        'imlMain
        '
        Me.imlMain.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
        Me.imlMain.ImageSize = New System.Drawing.Size(20, 20)
        Me.imlMain.TransparentColor = System.Drawing.Color.Transparent
        '
        'tbMain
        '
        Me.tbMain.Appearance = System.Windows.Forms.TabAppearance.FlatButtons
        Me.tbMain.Controls.AddRange(New System.Windows.Forms.Control() {Me.tabTraining, Me.TabPage1})
        Me.tbMain.Location = New System.Drawing.Point(8, 61)
        Me.tbMain.Name = "tbMain"
        Me.tbMain.SelectedIndex = 0
        Me.tbMain.Size = New System.Drawing.Size(547, 378)
        Me.tbMain.TabIndex = 0
        '
        'tabTraining
        '
        Me.tabTraining.BackColor = System.Drawing.Color.White
        Me.tabTraining.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.tabTraining.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox2, Me.Label2, Me.txtTrainTimes, Me.cmdTrain, Me.GroupBox1})
        Me.tabTraining.Location = New System.Drawing.Point(4, 25)
        Me.tabTraining.Name = "tabTraining"
        Me.tabTraining.Size = New System.Drawing.Size(539, 349)
        Me.tabTraining.TabIndex = 0
        Me.tabTraining.Text = "Train This Network"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblImage, Me.Label1, Me.cmdAddToQueue, Me.txtAsciiValue, Me.cmdBrowse, Me.picImgTrain})
        Me.GroupBox2.Location = New System.Drawing.Point(7, 9)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(514, 102)
        Me.GroupBox2.TabIndex = 13
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Add Data To Training Queue"
        '
        'lblImage
        '
        Me.lblImage.BackColor = System.Drawing.Color.White
        Me.lblImage.Location = New System.Drawing.Point(8, 27)
        Me.lblImage.Name = "lblImage"
        Me.lblImage.Size = New System.Drawing.Size(114, 22)
        Me.lblImage.TabIndex = 1
        Me.lblImage.Text = "Image Or Pattern"
        '
        'Label1
        '
        Me.Label1.BackColor = System.Drawing.Color.White
        Me.Label1.Location = New System.Drawing.Point(253, 21)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(189, 20)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "Equivalent ASCII Character"
        '
        'cmdAddToQueue
        '
        Me.cmdAddToQueue.BackColor = System.Drawing.SystemColors.Control
        Me.cmdAddToQueue.Location = New System.Drawing.Point(399, 58)
        Me.cmdAddToQueue.Name = "cmdAddToQueue"
        Me.cmdAddToQueue.Size = New System.Drawing.Size(96, 24)
        Me.cmdAddToQueue.TabIndex = 6
        Me.cmdAddToQueue.Text = "Add To Queue"
        '
        'txtAsciiValue
        '
        Me.txtAsciiValue.Location = New System.Drawing.Point(251, 61)
        Me.txtAsciiValue.Name = "txtAsciiValue"
        Me.txtAsciiValue.Size = New System.Drawing.Size(106, 20)
        Me.txtAsciiValue.TabIndex = 4
        Me.txtAsciiValue.Text = ""
        '
        'cmdBrowse
        '
        Me.cmdBrowse.BackColor = System.Drawing.SystemColors.Control
        Me.cmdBrowse.Location = New System.Drawing.Point(50, 57)
        Me.cmdBrowse.Name = "cmdBrowse"
        Me.cmdBrowse.Size = New System.Drawing.Size(67, 24)
        Me.cmdBrowse.TabIndex = 5
        Me.cmdBrowse.Text = "Browse"
        '
        'picImgTrain
        '
        Me.picImgTrain.BackColor = System.Drawing.Color.White
        Me.picImgTrain.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.picImgTrain.Location = New System.Drawing.Point(10, 59)
        Me.picImgTrain.Name = "picImgTrain"
        Me.picImgTrain.Size = New System.Drawing.Size(20, 20)
        Me.picImgTrain.TabIndex = 10
        Me.picImgTrain.TabStop = False
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(229, 319)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(81, 18)
        Me.Label2.TabIndex = 12
        Me.Label2.Text = "Times"
        '
        'txtTrainTimes
        '
        Me.txtTrainTimes.Location = New System.Drawing.Point(166, 316)
        Me.txtTrainTimes.Name = "txtTrainTimes"
        Me.txtTrainTimes.Size = New System.Drawing.Size(57, 20)
        Me.txtTrainTimes.TabIndex = 11
        Me.txtTrainTimes.Text = "1000"
        '
        'cmdTrain
        '
        Me.cmdTrain.BackColor = System.Drawing.SystemColors.Control
        Me.cmdTrain.Location = New System.Drawing.Point(11, 313)
        Me.cmdTrain.Name = "cmdTrain"
        Me.cmdTrain.Size = New System.Drawing.Size(143, 24)
        Me.cmdTrain.TabIndex = 7
        Me.cmdTrain.Text = "Start Training"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdClearAll, Me.lvMain})
        Me.GroupBox1.Location = New System.Drawing.Point(9, 114)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(520, 191)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Training Queue"
        '
        'cmdClearAll
        '
        Me.cmdClearAll.BackColor = System.Drawing.SystemColors.Control
        Me.cmdClearAll.Location = New System.Drawing.Point(401, 154)
        Me.cmdClearAll.Name = "cmdClearAll"
        Me.cmdClearAll.Size = New System.Drawing.Size(107, 24)
        Me.cmdClearAll.TabIndex = 8
        Me.cmdClearAll.Text = "Clear All"
        '
        'lvMain
        '
        Me.lvMain.Location = New System.Drawing.Point(7, 19)
        Me.lvMain.Name = "lvMain"
        Me.lvMain.Size = New System.Drawing.Size(506, 129)
        Me.lvMain.SmallImageList = Me.imlMain
        Me.lvMain.TabIndex = 1
        Me.lvMain.View = System.Windows.Forms.View.List
        '
        'TabPage1
        '
        Me.TabPage1.BackColor = System.Drawing.Color.White
        Me.TabPage1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.TabPage1.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox4, Me.GroupBox3})
        Me.TabPage1.Location = New System.Drawing.Point(4, 25)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Size = New System.Drawing.Size(539, 349)
        Me.TabPage1.TabIndex = 1
        Me.TabPage1.Text = "Detect Using Network"
        '
        'GroupBox4
        '
        Me.GroupBox4.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label4, Me.txtPatternDetect, Me.Label5, Me.txtAsciiDetect})
        Me.GroupBox4.Location = New System.Drawing.Point(6, 119)
        Me.GroupBox4.Name = "GroupBox4"
        Me.GroupBox4.Size = New System.Drawing.Size(518, 213)
        Me.GroupBox4.TabIndex = 21
        Me.GroupBox4.TabStop = False
        Me.GroupBox4.Text = "Results"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 43)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(157, 20)
        Me.Label4.TabIndex = 15
        Me.Label4.Text = "Detected ASCII Character"
        '
        'txtPatternDetect
        '
        Me.txtPatternDetect.Location = New System.Drawing.Point(234, 83)
        Me.txtPatternDetect.Name = "txtPatternDetect"
        Me.txtPatternDetect.ReadOnly = True
        Me.txtPatternDetect.Size = New System.Drawing.Size(162, 20)
        Me.txtPatternDetect.TabIndex = 19
        Me.txtPatternDetect.Text = ""
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(233, 43)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(157, 20)
        Me.Label5.TabIndex = 18
        Me.Label5.Text = "Equivalent Pattern"
        '
        'txtAsciiDetect
        '
        Me.txtAsciiDetect.Location = New System.Drawing.Point(18, 83)
        Me.txtAsciiDetect.Name = "txtAsciiDetect"
        Me.txtAsciiDetect.ReadOnly = True
        Me.txtAsciiDetect.Size = New System.Drawing.Size(127, 20)
        Me.txtAsciiDetect.TabIndex = 16
        Me.txtAsciiDetect.Text = ""
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3, Me.picImgDetect, Me.cmdDetect, Me.cmdBrowseDetect})
        Me.GroupBox3.Location = New System.Drawing.Point(7, 9)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(514, 102)
        Me.GroupBox3.TabIndex = 20
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "Detect Pattern"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(12, 34)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(151, 22)
        Me.Label3.TabIndex = 11
        Me.Label3.Text = "Image Or Pattern To Detect"
        '
        'picImgDetect
        '
        Me.picImgDetect.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.picImgDetect.Location = New System.Drawing.Point(17, 66)
        Me.picImgDetect.Name = "picImgDetect"
        Me.picImgDetect.Size = New System.Drawing.Size(20, 20)
        Me.picImgDetect.TabIndex = 14
        Me.picImgDetect.TabStop = False
        '
        'cmdDetect
        '
        Me.cmdDetect.BackColor = System.Drawing.SystemColors.Control
        Me.cmdDetect.Location = New System.Drawing.Point(318, 59)
        Me.cmdDetect.Name = "cmdDetect"
        Me.cmdDetect.Size = New System.Drawing.Size(160, 25)
        Me.cmdDetect.TabIndex = 17
        Me.cmdDetect.Text = "Detect This Image Now"
        '
        'cmdBrowseDetect
        '
        Me.cmdBrowseDetect.BackColor = System.Drawing.SystemColors.Control
        Me.cmdBrowseDetect.Location = New System.Drawing.Point(74, 61)
        Me.cmdBrowseDetect.Name = "cmdBrowseDetect"
        Me.cmdBrowseDetect.Size = New System.Drawing.Size(96, 24)
        Me.cmdBrowseDetect.TabIndex = 13
        Me.cmdBrowseDetect.Text = "Browse"
        '
        'pbTrain
        '
        Me.pbTrain.Location = New System.Drawing.Point(16, 201)
        Me.pbTrain.Name = "pbTrain"
        Me.pbTrain.Size = New System.Drawing.Size(509, 20)
        Me.pbTrain.TabIndex = 13
        '
        'cmdLoad
        '
        Me.cmdLoad.Image = CType(resources.GetObject("cmdLoad.Image"), System.Drawing.Bitmap)
        Me.cmdLoad.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.cmdLoad.Location = New System.Drawing.Point(286, 7)
        Me.cmdLoad.Name = "cmdLoad"
        Me.cmdLoad.Size = New System.Drawing.Size(122, 32)
        Me.cmdLoad.TabIndex = 11
        Me.cmdLoad.Text = "Load Network"
        '
        'cmdSave
        '
        Me.cmdSave.Image = CType(resources.GetObject("cmdSave.Image"), System.Drawing.Bitmap)
        Me.cmdSave.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.cmdSave.Location = New System.Drawing.Point(149, 7)
        Me.cmdSave.Name = "cmdSave"
        Me.cmdSave.Size = New System.Drawing.Size(122, 32)
        Me.cmdSave.TabIndex = 10
        Me.cmdSave.Text = "Save Network"
        '
        'cmdExit
        '
        Me.cmdExit.Image = CType(resources.GetObject("cmdExit.Image"), System.Drawing.Bitmap)
        Me.cmdExit.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.cmdExit.Location = New System.Drawing.Point(423, 7)
        Me.cmdExit.Name = "cmdExit"
        Me.cmdExit.Size = New System.Drawing.Size(123, 32)
        Me.cmdExit.TabIndex = 12
        Me.cmdExit.Text = "Exit Program"
        '
        'cmdRestart
        '
        Me.cmdRestart.Image = CType(resources.GetObject("cmdRestart.Image"), System.Drawing.Bitmap)
        Me.cmdRestart.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.cmdRestart.Location = New System.Drawing.Point(5, 7)
        Me.cmdRestart.Name = "cmdRestart"
        Me.cmdRestart.Size = New System.Drawing.Size(124, 32)
        Me.cmdRestart.TabIndex = 13
        Me.cmdRestart.Text = "New Network"
        '
        'pnTrain
        '
        Me.pnTrain.BackColor = System.Drawing.SystemColors.Control
        Me.pnTrain.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblTrainStart, Me.cmdCancel, Me.lblTrainInfo, Me.lblTrain, Me.pbTrain})
        Me.pnTrain.Location = New System.Drawing.Point(9, 60)
        Me.pnTrain.Name = "pnTrain"
        Me.pnTrain.Size = New System.Drawing.Size(553, 379)
        Me.pnTrain.TabIndex = 14
        '
        'lblTrainStart
        '
        Me.lblTrainStart.Location = New System.Drawing.Point(22, 249)
        Me.lblTrainStart.Name = "lblTrainStart"
        Me.lblTrainStart.Size = New System.Drawing.Size(252, 22)
        Me.lblTrainStart.TabIndex = 17
        '
        'cmdCancel
        '
        Me.cmdCancel.Location = New System.Drawing.Point(413, 232)
        Me.cmdCancel.Name = "cmdCancel"
        Me.cmdCancel.Size = New System.Drawing.Size(110, 24)
        Me.cmdCancel.TabIndex = 16
        Me.cmdCancel.Text = "Cancel"
        '
        'lblTrainInfo
        '
        Me.lblTrainInfo.Location = New System.Drawing.Point(22, 284)
        Me.lblTrainInfo.Name = "lblTrainInfo"
        Me.lblTrainInfo.Size = New System.Drawing.Size(252, 14)
        Me.lblTrainInfo.TabIndex = 15
        '
        'lblTrain
        '
        Me.lblTrain.Location = New System.Drawing.Point(14, 163)
        Me.lblTrain.Name = "lblTrain"
        Me.lblTrain.Size = New System.Drawing.Size(252, 24)
        Me.lblTrain.TabIndex = 14
        Me.lblTrain.Text = "Training Progress"
        '
        'pnNetwork
        '
        Me.pnNetwork.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdRestart, Me.cmdSave, Me.cmdLoad, Me.cmdExit})
        Me.pnNetwork.Location = New System.Drawing.Point(10, 4)
        Me.pnNetwork.Name = "pnNetwork"
        Me.pnNetwork.Size = New System.Drawing.Size(550, 50)
        Me.pnNetwork.TabIndex = 15
        '
        'frmMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(568, 450)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.tbMain, Me.pnTrain, Me.pnNetwork})
        Me.MaximizeBox = False
        Me.Name = "frmMain"
        Me.Text = "Pattern Detection"
        Me.tbMain.ResumeLayout(False)
        Me.tabTraining.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox1.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.GroupBox4.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.pnTrain.ResumeLayout(False)
        Me.pnNetwork.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region


#Region "Event Handlers"

    '<summary> Load an image </summary>
    Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
        Dim dlg As New OpenFileDialog()
        dlg.ShowDialog()

        If dlg.FileName = "" Then Exit Sub

        Try
            picImgTrain.Image = Image.FromFile(dlg.FileName)
        Catch ex As Exception
            MsgBox("Error: Invalid Image? " & ex.Message)
        End Try

    End Sub

    '<summary> Add to the list box </summary>
    Private Sub cmdAddToQueue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddToQueue.Click
        Try
            'We need the user to enter a proper image and corresponding ascii value
            If Len(Me.txtAsciiValue.Text) <> 1 Then Throw New Exception()

            imlMain.Images.Add(picImgTrain.Image)
            Me.lvMain.Items.Add(Me.txtAsciiValue.Text, imlMain.Images.Count - 1)
        Catch ex As Exception
            MsgBox("Error: Invalid image or ascii value")
        End Try

    End Sub

    '<summary> Clear all entries </summary>
    Private Sub cmdClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearAll.Click

        lvMain.Items.Clear()
        imlMain.Images.Clear()

    End Sub


    '<summary> Call back to show the progress bar, used from
    'the TrainPattern function </summary>
    Sub ShowProgress(ByVal CurrentRound As Long, ByVal MaxRound As Long, ByRef cancel As Boolean)

        Me.pbTrain.Maximum = MaxRound
        Me.pbTrain.Value = CurrentRound

        'Check whether our used clicked the cancel button
        If Me.StopTraining = True Then cancel = True
        lblTrainInfo.Text = CurrentRound & " rounds finished of " & MaxRound & " times"


    End Sub

    '<summary>We should initialze the network when the form loads </summary>
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitNetwork()
    End Sub


    '<summary>We should re-initialze the network here </summary>
    Private Sub cmdRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRestart.Click
        InitNetwork()
    End Sub


    '<summary>Load an image to detect it </summary>
    Private Sub cmdBrowseDetect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowseDetect.Click
        Dim dlg As New OpenFileDialog()
        dlg.ShowDialog()
        If dlg.FileName = "" Then Exit Sub

        Try
            picImgDetect.Image = Image.FromFile(dlg.FileName)
        Catch ex As Exception
            MsgBox("Error: Invalid Image? " & ex.Message)
        End Try
    End Sub

    '<summary>Start Detection </summary>
    Private Sub cmdDetect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDetect.Click

        Try
            DetectPattern()
        Catch ex As Exception
            MsgBox("Error: Invalid input image? " & ex.Message)
        End Try

    End Sub

    '<summary>Start Training </summary>
    Private Sub cmdTrain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTrain.Click

        'Initialize the training interface
        pnTrain.Visible = True
        pnNetwork.Visible = False
        tbMain.Visible = False

        lblTrainStart.Text = "Training Started At : " & Now

        'Start the training
        Try
            TrainPattern()
            StopTraining = True
            MsgBox("Training of the network completed at " & Now, MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox("Error while training. " & ex.Message)
        End Try



        'Reset the interface
        pnTrain.Visible = False
        tbMain.Visible = True
        pnNetwork.Visible = True


        'Reset the progress bar
        Me.pbTrain.Value = 0


    End Sub


    '<summary>Save our network to a file </summary>
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click


        'Serialize our network to a file
        Dim ser As New BrainNet.NeuralFramework.NetworkSerializer()

        Dim dlg As New SaveFileDialog()
        dlg.Filter = "XML Files|*.xml"
        dlg.DefaultExt = "xml"
        dlg.ShowDialog()

        Try
            If dlg.FileName <> "" Then
                ser.SaveNetwork(dlg.FileName, network)
                MsgBox("Saved to file " & dlg.FileName, MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MsgBox("Error: Invalid File? " & ex.Message)
        End Try

    End Sub

    '<summary>Load our network from a file </summary>
    Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
        'Serialize our network to a file
        Dim ser As New BrainNet.NeuralFramework.NetworkSerializer()

        Dim dlg As New OpenFileDialog()
        dlg.Filter = "XML Files|*.xml"
        dlg.ShowDialog()

        Try
            If dlg.FileName <> "" Then
                ser.LoadNetwork(dlg.FileName, network)
                MsgBox("File " & dlg.FileName & " loaded", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MsgBox("Error: Invalid File? " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    '<summary>User clicks this button to cancel training </summary>
    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        StopTraining = True
    End Sub

    '<summary>Unload the form and clean up </summary>
    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
        End
    End Sub

#End Region


#Region "Major Functions"

    'A private variable to hold our network.
    Private network As BrainNet.NeuralFramework.INeuralNetwork

    '<summary> Initialize our network </summary>
    Sub InitNetwork()

        'We are analyzing a 20x20 30el picture, so let us take the number
        'of total inputs as 20 x 20 = 400 neurons

        'So let us initialize a 400-400-8 network. I.e, 400 neurons in
        'input layer, 400 neurons in hidden layer and 8 neurons in output layer
        'We've chosen 8 neurons in output because we need 8 bits to
        'represent an ASCII character

        'Create the factory to create a Backward Propagation Neural Network
        'Backward Propagation neural network is a commonly used neural network model
        Dim factory As New BrainNet.NeuralFramework.BackPropNetworkFactory()

        'This is an arralist which holds the number of neurons in each layer
        Dim layers As ArrayList = New ArrayList()

        'We need 400 neurons in first layer
        layers.Add(400)
        'We need 400 neurons in the second layer (the second layer is the first
        'hidden layer)
        layers.Add(400)
        'We need 8 neurons in the output layer
        layers.Add(8)

        'Provide the arraylist as the parameter, to create a network
        network = factory.CreateNetwork(layers)

    End Sub

    '<summary> Routine to train the network </summary>
    Sub TrainPattern()

        'This routine demonstrates how easily you can train
        'a network using a NetworkHelper object

        'Here, we are using a NetworkHelper object to train the 
        'network.

        'Create a helper object
        Dim helper As BrainNet.NeuralFramework.NetworkHelper
        helper = New BrainNet.NeuralFramework.NetworkHelper(network)

        'A helper object helps you to train the network more
        'efficiently. First of all, you add each training data to the
        'Training Queue using the helper. For this, you can use the
        'AddTrainingData method of the helper

        'Next, you can call the Train function of the helper to
        'randomize entries to the training queue and train the network more
        'efficiently

        'Step 1 - Add the training data to the helper
        Dim item As ListViewItem

        For Each item In Me.lvMain.Items
            Dim img As Image = imlMain.Images(item.ImageIndex)
            Dim asciiVal As Long = Asc(item.Text)

            'The AddTrainingData method of Network helper helps you to
            'add an image and its corresponding ASCII value directly

            Dim patHelper As New BrainNet.NeuralFramework.PatternProcessingHelper()
            Dim imgHelper As New BrainNet.NeuralFramework.ImageProcessingHelper()


            Debug.WriteLine("Added : " & patHelper.PatternFromArraylist(imgHelper.ArrayListFromImage(img)))


            helper.AddTrainingData(img, asciiVal)
        Next

        'Step 2 - Train the network using the helper

        'Get the number of times
        Dim rounds As Long = Val(Me.txtTrainTimes.Text)



        'Add the handler of ShowProgress delegate, to get
        'the progress training progress
        StopTraining = False
        AddHandler helper.TrainingProgress, AddressOf ShowProgress
        'Start training
        helper.Train(rounds)
        RemoveHandler helper.TrainingProgress, AddressOf ShowProgress


    End Sub


    '<summary> Routine to detect an image </summary>
    Sub DetectPattern()



        'Step 1 : Convert the image to detect to an arraylist

        Dim imgHelper As New BrainNet.NeuralFramework.ImageProcessingHelper()
        Dim input As ArrayList

        input = imgHelper.ArrayListFromImage(Me.picImgDetect.Image)

        'Step 2: Run the network and obtain the output
        Dim output As ArrayList
        output = network.RunNetwork(input)

        'Step 3: Convert the output arraylist to long value
        'so that we will get the ascii character code

        Dim patternHelper As New BrainNet.NeuralFramework.PatternProcessingHelper()
        Dim character As String = Chr(patternHelper.NumberFromArraylist(output))
        Dim bitpattern As String = patternHelper.PatternFromArraylist(output)

        'Display the result
        Me.txtAsciiDetect.Text = character
        Me.txtPatternDetect.Text = bitpattern



    End Sub




#End Region



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