Do some Brain Tumor detection using neural networks, in a very simple and easy manner. This is the story and source code of an XML based language, to help you create, train and run your own neural networks?

Load your neural network using nxml to Train it, and run it - and even save the trained network back so that you can use it later.

If you are a programmer

You can understand how to develop and implement an xml language specification for various purposes.

You can learn how easily you can create data structures in your project from xsd schemas.

You can gain some insights regarding neural network programming and implementation.

We will also see how Neural XML (NXML) can be used for an 'intelligent' task - i.e, for identifying images based on various criteria (with an example of an interesting 'pseudo' brain tumor detection example)

Before We Begin:

Just for using Neural XML and to understand these concepts, you don't need to understand a lot about Neural Network Theory or even about BrainNet library. How ever, if you are a beginner (not in programming, but in neural networks), I strongly recommend you to read the below tutorials as an introduction - because that will provide you a solid foundation to begin neural network programming, and to understand neural network concepts.

In this part, we will discuss what exactly is Neural XML, and how to use it.

As you already know, a neural network consists of various layers, and each layer has a number of neurons in it. Initially, we will train the neural network, by providing the inputs to the input neurons, and by providing the output to the output neurons. Once the neural network is trained, you can run it.

Now, an interesting example. Let us see how we can create a 2-2-1 neural network (2 neurons in input layer, 2 neurons in hidden layer, and 1 neuron in output layer) to represent a digital gate (like and AND gate, or OR gate), using NXML. Then, we will train our 2-2-1 network to perform functions like AND, OR, XOR etc. See the previous articles for more information about neural networks (read them here Part I and Part II). If you examine my previous article, you can see that how we did this earlier, but using .NET code. This time we are doing the same - but using our own language, that is NXML.

Fig: A Simple 2-2-1 Network

What We Are Going To Do:

Training and running an application using NXML involves,

Creating a neural network using nxml tool

Training the neural network you created

Running the neural network

Let us see how to do this.

Step 1: Creating A 2-2-1 Neural Network

To create a 2-2-1 neural network. You can use the -gen switch of nxml. The syntax is,

Syntax: nxml -gen NeuronsInLayer1,NeuronsInLayer2,NeuronsInLayerN filenameAs the first parameter, we will pass the number of neurons in each layer, separated with commas. In this case, it is 2,2,1 - i.e, 2 input neurons, 2 hidden layer neurons and 1 output neuron. The second parameter is the file name.

Run nxml with the following arguments to create a 2-2-1 network and store it in gate.xml

nxml -gen 2,2,1 gate.xml

<html> <head> </head> If you open and examine the gate.xml file generated by nxml, you will see the following information. Under the root tag 'Network', there are various 'Layer' tags. As you can see, the network consists of three layers, Layer0, Layer1 and Layer2. Layer0 consists of two neurons L0N0 (0th neuron in 0th layer) and L0N1 (1st neuron in 0th layer) and so on.

Now, just have the look at the neuron L1N0 (0th Neuron in Layer1). You can see a tag 'Connections' - which define which all neurons in previous layer is connected to this neuron.<html> Please note that each connection input holds a 'weight'.

Also, each neuron has a Bias, Output a<html>nd Delta value, which holds these parameters of the neuron.<html>

The above code, to train the network, is written according to the nxml specifications. Kindly see this brief introduction to NXML language.

NXML LANGUAGE - A BRIEF OVERVIEW

NXML language is pretty straight forward. To perform an action on a neural network, you should load it initially. Using NXML, you can specify which network to load, what operation to perform on the network, etc. The 'Network' tag is used to load an xml file from disk, like the one we generated earlier. The 'DataBlock' tag specifies which operation we should perform on the network.

For example, the following line specifies that the gate.xml should be loaded, and it should be saved as OR.xml after performing the operations.

<Network LoadPath="gate.xml" SaveOnFinish="true" SavePath="OR.xml">

The Network tag has the following attributes.

LoadPath - The path of the xml file which holds our network. In this case, it is Gate.xml

SaveOnFinish - Specify whether the network should be saved after a DataBlock operation is performed

SavePath - You can save the network to a new file path.

A DataBlock tag specifies which operation we are going to perform. For example, the following tag specifies that the network should be trained 2000 times, using the data inside this data block

<DataBlock Type="Train" TrainCount="2000">

It has the following attributes.

Type - The type of operation to perform. It can be either Train or Run

TrainCount - The number of times the operation should be performed. It is not required if Type attribute is Run.

Also, a data block holds the data which should be used to train the network. For e.g., In our above example, the first data block holds data equivalent to an OR truth table. Data inside a Data block can be either PatternData or ImageData. We are using PatternData here. Use of ImageData tag will be explained later.

Attributes of PatternData include

InputType - species the format that we provide as input. Input Type can be Pattern, Array, Number and Character.

InputValue - Input value represents the value we provide for as input to the neural network. This will be parsed based on the InputType.

For example, if Input Type is 'Pattern', then then each bit in the input value is fed directly to the neurons in the input layer. For example, in the following case, 1 and 1 will be input to the neurons in first layer, and 0 is the target output for the first neuron in output layer.

Because, 11 is the binary representation of 3. Please bear in mind that the maximum input value you can provide to a 2-2-1 network is 3 - because 3 is the highest number we can represent using 2 bits (A 2-2-1 network cannot take more than two inputs at a time)

If you want the values in decimal format (single), provide Array as the input or output type.

If you are using input type as character, you should need at least 8 neurons in input layer - because a character requires 8 bits to represent it. OutputValue tag is not considered, and the value over written, if the data block type which holds this pattern data is 'Run' instead of 'Train'.

As another example, let us check some valid inputs for a 3-3-2 neural network with 3 neurons in input layer, and 2 neurons in output layer.

Now, to train the neural network, we can invoke the nxml interpreter using the following command, in the command prompt.

nxml -start trainGate.n.xml

This causes the nxml interpreter to process trainGate.n.xml. This will interpret the three 'Network' tags, and the following files are generated - i.e, OR.xml, AND.xml and XOR.xml, (as specified in trainGate.n.xml). Have a look at these XML files, and see the change of values in connection weights, bias etc.

Step 3: Running the Neural Network

Now we have three trained neural networks. OR.xml which can perform functions of an OR gate, AND.xml which can perform functions of an AND gate and XOR.xml which can perform functions of an XOR gate.

Let us see how to 'run' these networks. Let us create a file, runGate.n.xml, to run these networks.

The only change is that, the Type of Datablock is changed to 'Run' from 'Train'. Also, we are not providing any OutputValues - because we expect the network to predict and write these output values. Kindly note that the network xml files (like or.xml, and.xml and xor.xml) are not undergoing any changes - because we are running the network, and not training it.

Now, change the Output Type from Pattern to Array, and see what happens. You will get the exact output values in comma separated decimals if there are more than one neuron in the output layer (in this case, there is only one neuron in the output layer). Pattern data type always rounds the output to 1 or 0.

Now, let us see how you can create a neural network, train it and run it to detect brain tumors from the image of brain, using Neural XML.

I'm just trying to make things interesting. Of course, please understand that the samples used in this illustration are not 'real' images of the brain - they are just some 'virtual' images, and is meant purely for demonstration.

What We Are Going To Do:

Assume that we have the following images, and assume that each image is an x-ray of the brain. Each image has a size of 16 x 16 = 256 pixels.

We are using 13 images to train the network. According to the density of white pixels on a corner, we are assigning a value or condition (0,1,2 or 3) to each image, starting counter clockwise from the bottom left corner. The value of each image is shown below.

(1) =0 (2) =0 (3) = 1 (4) =1 (5) =2 (6)=2

(7) =3 (8) =3 (9) =3 (10) =3 (11) =2 (12) =1 (13) =0

We are using the density of white pixels on a particular corner, to detect the possibility of Brain Tumor. For example, see the first image. It has more white pixels at bottom left.

We assume that,

If the density of white is at bottom left corner, the condition is 0

If the density of white is at bottom right, the condition is 1

If the density of white is at top right, the condition is 2

If the density of white is at top left, the condition is 3

Let us define the following possibilities.

Condition 0 - No problem, brain has no tumor

Condition 1 - Some what infected

Condition 2 - Infected

Condition 3 - Critical, should change the brain ;)

I.e, for example, image 1 and 2 has no problem, image 3 and 4 is some what infected, image 5 and 6 is infected, image 7 and 8 is critically infected and so on.

Once samples are selected and conditions defined, we will,

Create a network which can learn from the samples

Train the network with a number of samples (Training phase)

Give an image to the network, and ask it to predict the condition (Running Phase)

Let us see how we can do this using Neural XML.

Step 1: Create A Neural Network

To create a network, we should determine

The number of neurons in input layer.

The number of neurons in hidden layer and the number of hidden layers

The number of neurons in output layer.

Let us see how to decide this

Neurons in input layer - As I mentioned earlier, we are providing a 16 x 16 image as input. When we digitize this picture, we can see that there are 16 x 16=256 pixels in each image. So, let us take 256 neurons in input layer - we will feed the value of each pixel (1 for white pixel and 0 for black) to each neuron in the input layer.

Neurons in output layer - We have four conditions - condition 0 (00 in binary), condition 1 (01 in binary), condition 2 (10 in binary), and condition 3 (11 in binary) - so, let us take two neurons in the output layer, because the highest output value we need (i.e, 3) requires two bits to represent it.

Neurons in hidden layer - Let us blindly assume that we have one hidden layer, and it has the number of neurons equal to the neurons in the input layer.

Now, let us generate the network, and save it to a file named DensityDetect.xml

nxml -gen 256,256,2 DensityDetect.xml

This will create a file named DensityDetect.xml

Step 2: Training The Neural Network

For training the neural network, let us create an nxml file, train.n.xml. All images are in the samples folder, relative to the folder where train.n.xml resides.

Kindly note that, we used Image Data tag instead of Pattern Data tag. For Image Data tag, you can specify the InputFile attribute, which specifies the image. Along with the image path, you should specify the input image's width and height. If the image size is greater than this, the image will be resized to this size.

For each image, we specified the OutputValue as the number corresponding to the condition we discussed earlier. Now, let us start training. For example, for Image 1, the output value we provided to train the network is zero.

nxml -start train.n.xml

This will start the training. We are training the network 3000 times. It will take some time, so go and have a cup of coffee ;)

Step 3: Running The Neural Network

Once the network is trained, you can use it to check image samples. To illustrate this, let us create an nxml file to run the network. We are going to detect the following images.

(1) (2) (3) (4) (5) (6) (7) (8)

The run.n.xml file is given below. All files are in test folder, relative to the folder where run.n.xml resides. Please note that any of these images are not among the list of images we used for training.

Second, the your VB.Net projects do not have Option Strict on! After turning it on I got 122 errors

Third, I highly doubt scalibility (and hence practicle use) of nxml. Typical neural networks in practice are not just 2 inputs or even just 400 inputs, the one with 100,000 inputs and even more are very common. To put all these data in XMl and then loading and parsing it XMLDom would probably crash regular systems. I think best way would be to allow pointing Neural Network to table in a relational database. Of course, SQL Server 2005 has SSAS to exactly achieve this but it's less than perfect and not as flexible as having source code with you .

It would be great to see 4th article in this series that can explain advanced concepts like SOM and techniques to deal with large scale NN.

I have read some articles on combining fuzzy systems with Neural networks "neural-fuzzy", to allow combining existing human "expert" knowledge to help train, and then also seeing the resulting rules that the ANN comes up with as fuzzy logic rules, like "IF hot AND wet THEN use raincoat". So in your article above, example domain expertise would be:
"* If the density of white is at bottom left corner, the condition is 0
* If the density of white is at bottom right, the condition is 1
* If the density of white is at top right, the condition is 2
* If the density of white is at top left, the condition is 3"

The neural-fuzzy system would start with this initial knowledge, then refine and/or add new rules based on the training data which though the neural-fuzzy combination is translated from the ANN weights back into human understandable fuzzy "expert" rules. So it might after some training add the rules to the list:
"* If the density of white is at top left AND density of white is at bottom left corner, the condition will soon be 1".

How to actually combine Fuzzy and Neural Networks is a bit of a mystery. There is a reference book "Neuro-fuzzy and soft computing-a computational approach to learning and machine intelligence" by J.-S.R.Jang, C.-T.Sun and E.Mizutani that discusses some "popular" research methods I believe (ANFIS/CANFIS), but overall there does not appear to be much learning material around.