⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.vb

📁 neural networks applications
💻 VB
📖 第 1 页 / 共 3 页
字号:


    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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -