The GatuHomeLab (4.3) - The CatEyeDetector
A Neural Network is normally used to recognize patterns. It is not use for cases where we have an input and we can determine the output using an equation, simple or complex... is for cases where we do not have means to get to this kind of equation, that should be extremely extremely complicated to find, and with multiple variables that we don’t even know how they behave... And one of the most remarkable features from Neural Networks is their ability to learn, adapting the output to different inputs.
Brain Neurons, (link)
See how easy it is for a computer to calculate 3256x8921... and how long will take for us to get the answer... but show a person a symbol, a face, a painting, and ask later to recognize it among others similar... and that is something that for a computer is not so easy to achieve. The neural network, like our brain, processes the information in parallel, and with many neurons working together, we can obtain really powerful results.
(Link)
Basically, the neurons are connected with the previous and next layer, and these connections has a certain weight, that is decided when the Neural Network is trained, in order to obtain a certain result at the end. That gives the power to recognize patterns, and give same outputs for different inputs, that share same pattern and is recognized by the Neural Network.
Now is time to configure the Neural Network. Will have three layers. And every layer has a number of neurons. The entry layer will have 120x200 neurons, that is the number of pixels for each image. You can change this to adapt to your image size.
The second layer is the hidden layer, and will have 100 neurons. Is not much advisable to use more hidden layers, but again I recommend you to check about neural networks and play with the size and number of hidden layers.
The third layer is the output, and will have four neurons, one for each estate we want to identify. The code is as follows:
// configure the neural network BasicNetwork network = new BasicNetwork(); int hiddenLayerNeuronsCount = 100; network.addLayer(new BasicLayer(null, true, downdimensionH*downdimensionW)); network.addLayer(new BasicLayer(new ActivationElliott(), true, hiddenLayerNeuronsCount)); network.addLayer(new BasicLayer(new ActivationElliott(), false, 4)); network.getStructure().finalizeStructure(); network.reset();
You have noticed the ActivationElliot function. This is a neural activation function, similar to Sigmoid or Hyperbolic Tangent, but more convenient because of its computation time, and that is an advantage to make the neural network converge faster during training.
We now train the neural network, using the images we have add to the training set, and we make iterations until we reach a certain error level. Then we have the neural network ready to use.
// train the neural network final Propagation train = new ResilientPropagation(network, training); int epoch = 0; do{ train.iteration(); System.out.println("Epoch("+epoch+") error: "+train.getError()); epoch++; }while(train.getError()>error); //close Encog Encog.getInstance().shutdown();
Why have we used Resilient Propagation to train the network? performance. It converges towards the solution faster than other methods. It is a good training method to use, and will work most of the times.
Last step will be to serialize the trained network and save it to disk.
//serialize Network and save to disk File networkfile=new File(workingdirectory+networkname); try { SerializeObject.save(networkfile,network); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Network saved ok: "+networkname);
In next post we will test the NN, how to evaluate an image and get the output from the Neural Network.












