The GatuHomeLab (4.4) - The CatEyeDetector
Now is time to test our neural network! We got the master images for every situation we want to identify, trained the network, and we have save it to disk to use it with new images from the webcam or camera.
So we can make a java class to do the job. We will give the name of the neural network to load from disk, where are the images to analyze (directory path), and the downdimensions for the images (the size we want to reduce the original images for proper use with the neural network). Then will give us the result.
We will call the class UseNeuralNetwork, and these are the variables and constructors we need.
public class UseNeuralNetwork { public int downdimensionH=120; public int downdimensionW=200; public String workingdirectory="/home/ftpimages/fb/"; public String networkname="MotiNetwork"; public String prefix="ts"; public List NNoutput; //results public int[] ivalues; public String[] svalues; //constructor public UseMotiNetwork(){ //nothing... use default parameters } //constructor public UseMotiNetwork(int ddh,int ddw,String workdirec,String neuralnet){ this.downdimensionH=ddh; this.downdimensionW=ddw; this.workingdirectory=workdirec; this.networkname=neuralnet; } (...)
And next is the method that will do the job. These are the steps we are going to follow:
Load the neural network from disk (the one we have trained)
Load the images we want to evaluate
Compute and get the results in a List
This method we have called evaluateNN(). Let’s see how it works. First we have the load the serialized object from disk:
File netfile=new File(networkname); BasicNetwork network=null; try { network=(BasicNetwork)SerializeObject.load(netfile); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Then we have to prepare the dataset where we are going to load the images to evaluate.
//Datasets RGBDownsample downsample=new RGBDownsample(); ImageMLDataSet testset=new ImageMLDataSet(downsample,false,1,-1);
Again we will use the ImageLoader class for loading the names of the images and put it inside the dataset, do the downsampling and get it ready for the neural network.
ImageLoader test=new ImageLoader(workingdirectory,prefix); test.loadFileNames(); for(int j=0;j<test.fileNames.size();j++){ //load images File file=new File(workingdirectory+test.fileNames.get(j)); Image img=null; try { img=ImageIO.read(file); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ImageMLData data = new ImageMLData(img); testset.add(data); } testset.downsample(downdimensionH, downdimensionW);
We have to create a list where to store the results for each image evaluated, then get the result. We have follow the criteria of saying that if the output of one of the neurons (output layer) is bigger than 0.5, we consider that an Ok for that situation. Again here you can change this part and make it as per your own criteria.
NNoutput=new ArrayList(); //enter data in network and get output for(int j=0;j0.5){ if(result[0]>0.5){ nnresult="CatEats"; }else if(result[1]>0.5){ nnresult="CatDrinks"; }else if(result[2]>0.5){ nnresult="FoodBowlEmpty"; }else if(result[3]>0.5){ nnresult="FoodBowlFull"; } //System.out.println("Result "+nnresult); NNoutput.add(nnresult); File oldfile =new File(workingdirectory+test.fileNames.get(j)); File newfile =new File(workingdirectory+nnresult+"_"+test.fileNames.get(j)); oldfile.renameTo(newfile); } Encog.getInstance().shutdown();
What we also do is to change the name of the images we have used, so next time we do not process it again. And we give a prefix to the image, with the result from the neural network. Then it is easy later to get the images and see if the computer brain got it right or was something else completely. You will get surprised of how many get it right...











