How to solve a simple real world application using a Neural Network (ANN).

Share

Machine learning and neural network is a sub field in computer science, which has gained a huge popularity today as a result of its vast range of applications.This could be applied in a simple and  effective manner,when conventional programming methods fail.

Artificial neural networks (ANNs) mimic the structure of the human brain. ANNs consist of a large number of interconnected artificial neurons.It is believed that trained neural networks are able to perform various activities successfully.

There are two main types of learning methods,

  1. Supervised learning.
  2. Unsupervised learning.

Let us focus on a practical implementation of a supervised learning method.We need to have a training dataset which contains input data and their corresponding classes in order to train a network using supervised learning.This can be done by following the steps below.

Step 1 – Pre processing input data

First,we consider a simple “Sinhala handwriting detection” system. In order to implement such a system we need data belongs to every class (In this example “ර”,”ග”,”ට”,”ප”). The following figure shows a unprocessed handwriting sample(left) and processed sample(right).

Raw vs processed input data
Raw vs processed input data

In order to use these as input data they should be extracted and categorized manually,they should be saved into separate images.This can be done in Matlab using the “image processing toolbox“.Some simple image processing codes are given below.

I=rgb2gray(I); %Conver to gray image I=im2bw(I,.1); %Thresholding using suitable threshold se=strel('disk',1,4); %Erotion/dialation/opening depending on the I=imdilate(~I,se); %situation

Categorization code.

a=imread('processed.jpg'); gray=rgb2gray(a); bw=im2bw(gray,0.1); imagesc(bw);axis off;axis image;colormap gray h = imrect(gca); setResizable(h,1); pos = wait(h); in=imcrop(bw,pos); se=strel('disk',1,4); in=imdilate(~in,se); imshow(in); stats = regionprops(in,'BoundingBox'); j=0; for k=1:length(stats) boxpos=stats(k).BoundingBox; if (boxpos(4)*boxpos(3)>200) rectangle('Position',boxpos,'EdgeColor','g', 'LineWidth', 3); imwrite(imcrop(in,boxpos),strcat('./testdata/',num2str(k),'.jpg')); %saves images %used to input to struct %input(j).img= imcrop(in,boxpos); %imput(j).type='R'; end end

ANN input data catagorization
Image categorization using regionprops

These separated segments are stored as 20×20 images along with their corresponding class inside a structured array which could be saved separately for future use as a ‘traindata.mat’ file.

There are some cases in which multiple images are used to create the input dataset,alternative ways of processing should be used.(Example here)

Step 2 – Creating the neural network

We need to first create a neural network according to the complexity of the problem.We can decide the number of inputs/outputs and the type of transfer functions by analyzing the problem. As we created 20×20 images we must have 400 input nodes and since we have 4 output classes we need at most 4 output nodes.

Many types of neural networks can be created in Matlab.(I found out that for this example a patternnet with 2 hidden layers containing 20 nodes each would do the trick)

net = patternnet([20 20]); %creating patternnet with 2 hidden layers net.divideParam.trainRatio = 1; net.divideParam.valRatio = 0; net.divideParam.testRatio = 0; %assigning all samples for training only net.trainParam.epochs=1000; %set stoping stopping criteria net.trainParam.goal = 0; net.trainParam.min_grad=0; view(net);

View(net) command can be used to visualize the created network to verify the architecture and the type of transfer functions used in each layer.

The network can be modified according to the user requirement if necessary using the following commands.

net.layers{2}.transferFcn = 'logsig'; % change transfer function in internal layers net.trainFcn = 'traingd'; %change the training method to gradient descend net.performFcn ='mse'; %Use Mean squared error to evaluate performance net.trainParam.time=inf; net.trainParam.showCommandLine=false;

Matlab View(net) output
View(net) command output of a patternnet([20 20])

Step 3 – Training a neural network using test data

In order to train the created network,we need to convert our input images in to a column vector (dimension 400×1) and target classes should be assigned binary values according to our preference. The following code was used to do the above mentioned conversion.

load('data.mat'); %traningdata was stored inside the 'data.mat'. %This should be imported before training) X=reshape(im2double(input(1).img),1,[]); T=[0 0 0 1]; %R 0001 %G 0010 %P 0100 %T 1000 for i=2:length(input) x=reshape(im2double(input(i).img),1,[]); if input(i).type=='R' t=[0 0 0 1]; elseif input(i).type=='G' t=[0 0 1 0]; elseif input(i).type=='P' t=[0 1 0 0]; elseif input(i).type=='T' t=[1 0 0 0]; end T=cat(1,T,t); X=cat(1,X,x); end [net,tr] = train(net,X',T');

Line 25 [net,tr] = train(net,X’,T’);can be used to train the current network over and over again until it reaches a satisfying state. During training the following GUI will pop and inform the current state of the training process.(Additional information can be accessed by using provided functions eg. plotconfution matrix)

Matlab nntrain GUI after training
nntrain GUI after training is terminated.(after 69 epochs)

Step 4 -Testing the ANN

We need a separate set of classified input data for testing the performance of the neural network. plotconfusion(T’,net(X’)) can be used to quantitatively represent the performance of the network after each training session.

Finally the trained network can be used to identify written text with trained classes. This could be  implemented by using more advanced image processing techniques to increase accuracy of correct classification.

Sample Code.

load('net.mat'); [file,path]=uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';... '*.*','All Files' },'Select Image'); if isequal(file,0) || isequal(path,0) % disp('User pressed cancel') else % disp(['User selected ', fullfile(path, file)]) I=imread(fullfile(path, file)); iorg=I; if size(I,3)>1 I=rgb2gray(I); I=im2bw(I,.1); se=strel('disk',1,4); I=imdilate(~I,se); end imagesc(I);axis image;axis off;colormap gray % h = imrect(gca); % setResizable(h,1); % pos = wait(h); end in=I; % in=imcrop(I,pos); stats = regionprops(in,'BoundingBox'); for k=1:length(stats) boxpos=stats(k).BoundingBox; if (boxpos(4)*boxpos(3)>400) rectangle('Position',boxpos,'EdgeColor','g', 'LineWidth', 3); jj= imresize(imcrop(in,boxpos),[20 20]); input_net=reshape(im2double(jj),1,[]); [a b]=max(net(input_net')); if b==1 lbl='T'; elseif b==2 lbl='P'; elseif b==3 lbl='G'; else lbl='R'; end iorg=insertObjectAnnotation(im2double(iorg),'rectangle',boxpos,lbl); end end imshow(iorg);

Using a trained network in Matlab

It should be noted that although we performed training by using a fixed size dataset; a well-trained ANN will be able to classify various sizes and types of similar letters.

As you can see by this simple example above, a neural network is a very powerful tool which can be used to classify data based on patterns. Supervised learning can be used to identify faces, types of cars and even to predict weather.

We should keep in mind that neural network outputs are unpredictable; hence testing and training are two essential steps in building a reliable ANN. The “network structure” also plays an important role in the performance of the network.

Feel free to contact the writer for any further clarification on this article.

References

http://in.mathworks.com/help/nnet/ref/plotconfusion.html

 
Tagged : / /