Contents
Solution Architect: "We have a new project. We need to develop a brain tumor recognition system. I hope you can do that?"
Dumb (And Lazy) Programmer: "No. Oh, probably yes - let me search whether I can I get a component or library for that"
| The most important objective of this article series is to
- Demonstrate some practical uses of neural network programming
- Give you a fair idea regarding neurons, neural networks and their applications
- Introduce BrainNet library - an Artificial Neural Network library I developed - mainly using the .NET Framework.
BrainNet, as it is now, is not a commercial standard library - it is just in its beta stage. However, I would like to put BrainNet in front of the open source community - mainly to
- Create an awareness among programmers about Neural Network systems
- Initialize some discussions about practical applications of Neural Networks in emerging systems
In the process, I will explain how to develop some cool Neural Network applications as well. For example, even in this introductory article, you are learning how to develop two Neural Network programs
- DigitalNeuralGate - A Two input neural digital gate
- PatternDetector - A simple handwriting/pattern detection program
Who knows, sometimes these articles can trigger some new concepts in you, and that may even change the whole way we look at the world right now - Anyway, Good luck, Happy Coding!!
|
I am planning to write few articles, regarding Neural Networks and BrainNet Neural Network Library. In this article series, I will
- Give you tips regarding how to use this neural network library in your own projects.
- Explain in simple English what exactly is a neural network.
- Explain the concept of a neuron, and a neural network.
- Introduce and explain the programming model and design of the BrainNet library.
- Introduce and explain Neural XML (NXML), an XML based programming language (which is a part of BrainNet library), for creating, training and running Neural Networks.
In short, after reading this article series, you will
- Obtain a fair understanding regarding Neurons and neural networks
- Gain a good concept regarding intelligent systems
- Learn how to play with this neural network library to use it in your projects.
- Understand how to develop some cool neural network programs
When we discuss the BrainNet library, we should analyze
What we can do with this library
- The answer to this question will give you proper understanding regarding how to use the library in your projects.
What is inside the library or the actual implementation and design of the library
- The answer to this question will give you proper understanding regarding how to extend the library yourself, and modify it to suit your needs.
This is the first article in this series. This article tries to answer the first question - What we can do with this library. In this article,
- I'll give you a very high level view regarding neural networks
- I'll explain how to use BrainNet library in your projects to implement Neural Network logic.
Also in this article, we will discuss how to develop two applications using BrainNet library,
- DigitalNeuralGate - A Two input neural digital gate which can be trained to perform functions of various digital gates (like XOR, AND, OR etc)
- PatternDetector - A simple handwriting/pattern detection program which can analyze an image to detect it.
The future articles will give you more details - right now, my objective is to provide a good ground even for someone who don't know Neural Network programming at all. You can find the source code of all these projects in the related source code zip file.
Now, few words regarding the emerging trends and future computing.
These days Biologically inspired computing projects are getting very popular. They are used in various spheres, including learning and recognition systems, business prediction, data mining, pattern detection etc, to name a few.
This article is the first one in the series of articles related to biologically inspired computing. I am planning to discuss more topics like Genetic Algorithm, Conway's Game Of Life etc in my future articles. My most important objective is to create an awareness in the programming community regarding the possibilities of merging these diverse technologies and logics together - to invent better systems with more accuracy. For sure, the future is heading towards hybrid systems.
For me, the major inspiration in learning these topics is simply the 'natural' beauty in these topics. As we all know, nature is a mystery, and we can learn a lot from nature, and when we can transform this knowledge about nature to application (using computers), a programmer is attaining the level of an artist. Just as an artist gets inspired by nature, I believe that a scientist and a programmer can also get intuitions in the same way. If you ask a poet how he wrote a poem, he may say - "It came to me from a silent corner in my mind". Similarly - I believe - if you ask yourself how you got the most wonderful programming logic or idea you ever formulated in your life - you may utter the same words.
| Tip: I heard a story long ago, about the great Albert Einstein. Einstein got the spark of Relativity theory from an intuition that came to his mind - i.e, One day he thought, what may happen if we travel with a light ray (in the same speed), and see it from there. |
|
You can use the library straight away in your projects - even without understanding much regarding the actual theory behind neural networks. In this section
- I will explain some basic facts about neural networks
- We will develop a simple digital neural gate - i.e, a gate with two inputs and one output which can be trained to perform the functions of various gates like AND gate, OR gate, XOR gate etc.
You should understand some basic facts about neural networks before we begin.
- A Neural Network consists of various layers
- Each layer can any number of neurons in it.
Here are some basic facts about the structure of a neural network
- The first layer of the network is called an input layer, and it is here we apply the input
- The last layer is called the output layer, and it is from here we take the output.
- A neural network can have any number of hidden layers, between the input and output layer.
- In most neural network models, a neuron in one layer is connected to all neurons in the next layer.
Fig: A 2-2-1 Network
For example, in the above network, N1 and N2 are neurons in input layer, N3 and N4 are neurons in hidden layer, and N5 is the neuron in output layer. We provide the inputs to N1 and N2. Each neuron in each layer is connected to all neurons in next layer. The above network can be called a 2-2-1 network, based on the number of neurons in each layer.
Now, some basic facts about training.
- You can train a neural network by providing inputs and outputs.
- The network will actually learn from the inputs and outputs - this is explained in detail later.
- Once training is over, you can provide the inputs to obtain the outputs.
Now we will see how you can use the BrainNet library to develop a neural network, which can be trained to perform digital gate functions. We are going to create a 2-2-1 network - which means, a network with two input neurons, two hidden layer neurons and one output neuron - exactly as shown in the picture above. Then, we will see how to train this network to perform the functions of various two input digital gates - like AND gate, OR gate, XOR gate etc.
The important point to note is that, we can train the same network to learn the functions of various gates. The network will learn which output to produce for a given input, from the truth table of a gate - after a number of training rounds.
Note: This project is included in the source code zip attached above with this article. Extract the zip, open the solution in from Visual Studio.NET, set the startup project as "NeuralGate" and run the project.
The DigitalNeuralGate Class
To use BrainNet library in your project, you should create a reference from your project to the BrainNet.NeuralFramework.Dll library file.
Let us see the code of DigitalNeuralGate
class. In the constructor of the class, we are basically creating a Neural network with two neurons in first layer, two neurons in the hidden layer, and one neuron in the output layer. The Train
function will pass a training data object (consists of inputs and outputs) to the TrainNetwork
function of the library. The Run
function will pass an array list as input to the RunNetwork
function of the library.
Imports BrainNet.NeuralFramework
Public Class DigitalNeuralGate
Private network As BrainNet.NeuralFramework.INeuralNetwork
2-2-1 network </summary>
Public Sub New()
Dim factory As New BrainNet.NeuralFramework.BackPropNetworkFactory()
Dim layers As New ArrayList()
layers.Add(2)
layers.Add(2)
layers.Add(1)
network = factory.CreateNetwork(layers)
End Sub
Public Sub Train(ByVal input1 As Long, ByVal input2 As Long, _
ByVal output As Long)
Dim td As New TrainingData()
td.Inputs.Add(input1)
td.Inputs.Add(input2)
td.Outputs.Add(output)
network.TrainNetwork(td)
End Sub
Public Function Run(ByVal input1 As Long, ByVal input2 As Long) As Double
Dim inputs As New ArrayList()
inputs.Add(input1)
inputs.Add(input2)
Dim outputs As ArrayList = network.RunNetwork(inputs)
Return outputs(0)
End Function
End Class
The code is self explanatory, and it is heavily commented. However, here are some more points.
Explanation of code inside Sub New() - Creating a neural network using BrainNet library
You can create a network by creating an object of type BrainNet.NeuralFramework.BackPropNetworkFactory
and by calling the CreateNetwork
function of the factory object.
- Kindly have a look at the constructor of the class, we used the
CreateNetwork
function of the factory object of type BrainNet.NeuralFramework.BackPropNetworkFactory
to create our neural network object. - We provided the number of neurons in each layers as the input to the
CreateNetwork
function, using an ArrayList. - The
CreateNetwork
function will return an object of type BrainNet.NeuralFramework.INeuralNetwork
. - If you need to understand more about factory pattern (and its use), reading my article regarding Design Patterns [Click Here] may help.
Explanation of code inside Train() function
- Training can be done by calling the
TrainNetwork
function of the network. The input to the train network function is a TrainingData
object. A TrainingData
object consist of two array lists - Inputs and Outputs. - The number of elements in TrainingData.Inputs should match exactly with the number of neurons in your input layer.
- The number of elements in TrainingData.Outputs should match exactly with the number of neurons in your output layer.
Explanation of code inside Run() function
- You can call the
RunNetwork
function of the network, to run the network after training it. The input parameter to the Run function is an array list which consists of the inputs to the input layer. Again, the number of elements in this array list should match the number of neurons in input layer. - The Run function will return an array list which consists of the output values. The number of elements in this array list will be equal to the number of elements in the output layer.
A User Interface To Test Our DigitalNeuralGate Class
To test the digital neural gate, let us create a simple interface which can create a gate, read the inputs to train the gate, and obtain the output to display it.
Fig: User Interface To Test Our Gate
Here, we create a new object of our DigitalNeuralGate when the form loads (See the Form Load event in source code). Also, the user can create a new DigitalNeuralGate by clicking the 'Reset Gate' button. In the beginning, the Truth Table provided in the training text boxes are initialized to match the Truth Table of XOR gate (I hope you still remember simple Boolean Algebra). However, you can change the truth table by clicking the links, or you can provide custom truth table by entering directly in the text boxes. Run the project and see.
To begin with, Reset the Gate by clicking 'Reset Gate', and just click the 'Run Network' button and see the output. The output doesn't match the truth table output. Now, we can train the network using the values in the truth table. Click the 'Train 1000 Times' button and click the 'Run Network' button. You can see the output is getting closer to the expected output - that is, the network is learning. Do this a couple of times, and see the improvement in accuracy.
To try with a different truth table, Click 'Reset Gate', change the truth table, and repeat the above steps as required.
The source code is included in the zip file. Kindly open it and have a look at the project.
TrainOnce is a simple function which calls the Train function of the gate we just developed above.
Sub TrainOnce()
gate.Train(CLng(Me.inp11.Text), CLng(Me.inp12.Text), CLng(Me.out1.Text))
gate.Train(CLng(Me.inp21.Text), CLng(Me.inp22.Text), CLng(Me.out2.Text))
gate.Train(CLng(Me.inp31.Text), CLng(Me.inp32.Text), CLng(Me.out3.Text))
gate.Train(CLng(Me.inp41.Text), CLng(Me.inp42.Text), CLng(Me.out4.Text))
End Sub
This function handles the click event of 'Train 1000 times' button. It simply calls the above TrainOnce function 1000 times
Private Sub cmdTrain1000_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdTrain1000.Click
Dim i As Integer
Try
For i = 0 To 1000
TrainOnce()
Next
Catch ex As Exception
MsgBox("Error. Check whether the input is valid - " + ex.Message)
End Try
End Sub
This function handles the click event of 'Run Network' button, to run the network by providing inputs and setting the outputs to the output text boxes
Private Sub cmdRun_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdRun.Click
Try
rout1.Text = gate.Run(CLng(Me.rinp11.Text), CLng(Me.rinp12.Text))
rout2.Text = gate.Run(CLng(Me.rinp21.Text), CLng(Me.rinp22.Text))
rout3.Text = gate.Run(CLng(Me.rinp31.Text), CLng(Me.rinp32.Text))
rout4.Text = gate.Run(CLng(Me.rinp41.Text), CLng(Me.rinp42.Text))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Now, I hope, you have noticed a very important fact. That is:
- You haven't changed any algorithm inside the DigitalNeuralGate, but DigitalNeuralGate is producing almost correct outputs when you teach it with various truth table. For example, the logics, AND, OR, XOR etc can be implemented, by training the system externally with the logic, rather than changing the system internally.
| Important: Just keep this sentence in your mind for your life time (though it can be misleading right now) - "A Neural Network is not programmed to produce outputs, instead it is trained to perform a particular task" |
|
In this section, we just went through a very abstract overview regarding the capabilities, simplicity and flexibility of BrainNet neural network library.
BrainNet offers built in support for persistence of neural networks. For example, in the above case, after training a Gate, you may need to save its state to load it later. For this, you can use the NetworkSerializer
class in the BrainNet library.
To demonstrate this feature, let us add two functions to our DigitalNeuralGate class.
Public Sub Save(ByVal file As String)
Dim ser As New BrainNet.NeuralFramework.NetworkSerializer()
ser.SaveNetwork(file, network)
End Sub
Public Sub Load(ByVal file As String)
Dim ser As New BrainNet.NeuralFramework.NetworkSerializer()
ser.LoadNetwork(file, network)
End Sub
The SaveNetwork
method with in NetworkSerializer
class will save the network to a specified path, and the LoadNetwork
function will load the network back.
In the above example, we developed a simple application - a two input gate that can be trained to perform the function of any digital gate - using Brian Net library. Now it is time to go for something more exciting and powerful - a pattern/image detection program using BrainNet library. We provide a set of images as input to the network along with an ASCII character that corresponds to each input - and we will examine whether the network can predict a character when an arbitrary image is given.
Surprisingly, the project is pretty easy to develop. This is because, BrainNet library provides some functionalities to deal directly with images. This project will demonstrate:
- Built in support for image processing/detection and pattern processing in BrainNet library
- Built in support for advanced training using Training Queues in BrainNet library.
Before going to the code and explanation, let us see what the application really does. You can find the application and source code in the attached zip file. Load the solution in Microsoft Visual Studio.NET, set the startup project as PatternDetector, and run the project.
Run the program, and you will see the Pattern Detection dialog box. The pattern detection program can 'learn' the ASCII characters, corresponding to a bitmap (20 x 20 pixel size).
First of all, you need to train the network. To train the network, give some images and corresponding ASCII character value from the 'Train This Network' section.
Fig: Training - Adding images and corresponding character
To provide training data
- Click 'Browse' to load an image to the picture box (You can find some images in the 'bin' folder of PatternDetector - Also, you can create 20 x 20 monochrome images in Paintbrush if you want).
- Enter the ASCII character that corresponds to the image - for example, if you are loading image of character 'A', enter 'A' in the text box.
- Click 'Add To Queue' button
To train the network
- After adding the images to the training queue as explained earlier, click 'Start Training' button. Train the network at least 1000 times, for a below average accuracy. When you click the 'Start Training' button, training will start.
- You will see a progress bar, indicating the training progress.
Detecting A Pattern
- Once the training is completed, go to the 'Detect using Network' pane.
- Load an image by clicking the browse button, and click 'Detect This Image Now' button to detect the pattern
- If you trained the Network sufficient number of times, and if you provided enough samples, you will get the correct output.
Fig: Detecting The Image
The code of PatternDetector is pretty simple. If you can have a look at the code of frmMain.vb form, you will find three major functions:
InitNetwork
method to create/initialize the network TrainPattern
method to train the network DetectPattern
method to detect an image
The concept behind the program is pretty simple.
- We are using a 400-400-8 network (400 neurons in input layer, 400 neurons in hidden layer, and 8 neurons in output layer) to perform the required operations
- First of all, we will convert the 20 x 20 image (i.e, as you know a 20 x 20 image consists of 400 pixels) to an array of 1s and 0s. A white pixel is taken as 1 and a black pixel is taken as 0. This is fed to the input layer
- As you know, we should give the output along with the input, during training phase. For this, the character's ASCII value's binary representation is fed to the output layer.
Fortunately, any of these tasks are not so complex at all. This can be easily achieved using the built-in functionality of BrainNet library. Just have a look at the major functions with in PatternDetector.
Private network As BrainNet.NeuralFramework.INeuralNetwork
Sub InitNetwork()
Dim factory As New BrainNet.NeuralFramework.BackPropNetworkFactory()
Dim layers As ArrayList = New ArrayList()
layers.Add(400)
layers.Add(400)
layers.Add(8)
network = factory.CreateNetwork(layers)
End Sub
Sub TrainPattern()
Dim helper As BrainNet.NeuralFramework.NetworkHelper
helper = New BrainNet.NeuralFramework.NetworkHelper(network)
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)
helper.AddTrainingData(img, asciiVal)
Next
Dim rounds As Long = Val(Me.txtTrainTimes.Text)
StopTraining = False
AddHandler helper.TrainingProgress, AddressOf ShowProgress
helper.Train(rounds)
RemoveHandler helper.TrainingProgress, AddressOf ShowProgress
End Sub
Sub DetectPattern()
Dim imgHelper As New BrainNet.NeuralFramework.ImageProcessingHelper()
Dim input As ArrayList
input = imgHelper.ArrayListFromImage(Me.picImgDetect.Image)
Dim output As ArrayList
output = network.RunNetwork(input)
Dim patternHelper As New BrainNet.NeuralFramework.PatternProcessingHelper()
Dim character As String = Chr(patternHelper.NumberFromArraylist(output))
Dim bitpattern As String = patternHelper.PatternFromArraylist(output)
Me.txtAsciiDetect.Text = character
Me.txtPatternDetect.Text = bitpattern
End Sub
The code is heavily commented, but here is some more explanation.
Training Using Network Helper
Examine the TrainPattern
function. Instead of training the Network directly (as we did in the case of our Binary Neural Gate), we are using a Network Helper object to train the network. Using a network helper object, you can add images and the corresponding ASCII codes directly. AddTrainingData
method of NetworkHelper
class is gracefully overloaded, so that it can accept various parameters (more about this later).
Here, we are iterating each element in our list view (i.e, the training queue) - and add it to the helper. Then we initiate the training by calling the 'Train' method of the helper. The input to the 'Train' method is the number of rounds we need to train the network. For more details, have a look the help file of BrainNet library (included in the zip file).
PatternProcessingHelper and ImageProcessingHelper
Examine the DetectPattern
function. Here, we should provide the input to the network, to obtain the output. To convert an image to an array of 1s and 0s, we use the ArrayListFromImage
function inside the ImageProcessingHelper
class. After obtaining the output from the network, we should convert this to the equivalent ASCII code to display the character in the textbox - for this, we use the NumberFromArrayList
function in the PatternProcessingHelper
class. Similarly, the PatternFromArrayList
function converts an array list to a string (normally, a string of 1s and 0s).
Other than the above functions, the code for handling the user interface is also present in the PatternDetector project. Open the project, and have a look at the source code (it is commented heavily) for a better understanding.
That is it for the day. Congratulations for finishing the article with so much interest!!
I hope you enjoyed this article, and related projects. The attached zip file contains BrainNet Framework assembly file, BrainNet library Documentation in CHM format, and source code of the above two projects. Download and experiment.
Now, you can read the second part of this article, and download the entire BrainNet library source code. Read it here>> , or here>>
- Read all the articles I published so far at amazedsaint. - You'll find articles about Design Patterns, Neural Networks, Security, Hacking and more.
- You can subscribe to the XML atom feed of my technical articles blog, for tracking new posts. Click Here for the XML Atom Feed.
Visit my website here for a lot of tutorials and source code.
If you come across any bugs, post your comments here.