Menu

Strange results of image detection

Help
Andrej K
2015-04-30
2015-05-01
  • Andrej K

    Andrej K - 2015-04-30

    Hello,

    first of all, thank you very much for developing such great environment. I'm trying to create a neural network to detect images. Here is detail info what I have installed:
    Product Version: NeurophStudio 201408251540
    Java: 1.7.0_71; Java HotSpot(TM) Server VM 24.71-b01
    Runtime: Java(TM) SE Runtime Environment 1.7.0_71-b14
    System: Windows 7 version 6.1 running on x86; Cp1250; sk_SK (neurophstudio)

    Following the tutorial at http://neuroph.sourceforge.net/image_recognition.html I was able to create a network, learn it and detect a picture (in NeurophStudio). Then I was able to load created network in java and detect picture. Now I'm trying to create a network runtime in java, learn it and detect a picture. But the results are not very good.

    I created an example with 3 neural networks:
    1. network - java runtime created network
    2. network - created in NeurophStudio, but without learned data
    3. network - created in NeurophStudio with learned data

    Here is the source code:

    import org.apache.commons.io.FilenameUtils;
    import org.neuroph.core.NeuralNetwork;
    import org.neuroph.core.data.DataSet;
    import org.neuroph.imgrec.ColorMode;
    import org.neuroph.imgrec.FractionRgbData;
    import org.neuroph.imgrec.ImageRecognitionHelper;
    import org.neuroph.imgrec.ImageRecognitionPlugin;

    import org.neuroph.imgrec.ImageUtilities;
    import org.neuroph.imgrec.image.Dimension;
    import org.neuroph.nnet.learning.MomentumBackpropagation;
    import org.neuroph.util.TransferFunctionType;

    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;

    public class main {

    public static void main(String[] args) {
    
        //
        // Create learning data
        //
        File folder = new File ("Pictures\\");
        List<String> imageLabels = new ArrayList<String> ();
    
        HashMap<String, BufferedImage> imagesMap = new HashMap<String, BufferedImage> ();
        for (File file : folder.listFiles ())
        {
            imageLabels.add(FilenameUtils.removeExtension(file.getName()));
            imagesMap.put(file.getName(), ImageUtilities.resizeImage (ImageUtilities.loadImage(file), 20, 20));           
        }
        Map<String, FractionRgbData> imageRgbData = ImageUtilities.getFractionRgbDataForImages (imagesMap);
        DataSet learningData = ImageRecognitionHelper.createTrainingSet(imageLabels, imageRgbData);
    
        // 
        // create neural networks
        //  
        ArrayList<Integer> layers = new ArrayList<Integer> ();layers.add (12);
        NeuralNetwork nn1 = ImageRecognitionHelper.createNewNeuralNetwork ("recognition", new Dimension (20, 20), ColorMode.FULL_COLOR, imageLabels, layers, TransferFunctionType.SIGMOID); // create my own network
        NeuralNetwork nn2 = NeuralNetwork.createFromFile("NeuralNetworks\\test_notTrained.nnet");   // load network created in NeurophStudio following tutorial at http://neuroph.sourceforge.net/image_recognition.html
        NeuralNetwork nn3 = NeuralNetwork.createFromFile("NeuralNetworks\\test_trained.nnet");      // load network created in NeurophStudio following tutorial at http://neuroph.sourceforge.net/image_recognition.html
    
        //
        // learn data
        //
        MomentumBackpropagation mb1 = (MomentumBackpropagation)nn1.getLearningRule();
        mb1.setLearningRate(0.2);
        mb1.setMaxError(0.1);
        mb1.setMomentum(0.7);
    
        MomentumBackpropagation mb2 = (MomentumBackpropagation)nn2.getLearningRule();
        mb2.setLearningRate(0.2);
        mb2.setMaxError(0.1);
        mb2.setMomentum(0.7);
    
        nn1.learn(learningData);
        nn2.learn(learningData);
    
        // get the image recognition plugin from neural network    
        ImageRecognitionPlugin ir1 = (ImageRecognitionPlugin)nn1.getPlugin(ImageRecognitionPlugin.class); 
        ImageRecognitionPlugin ir2 = (ImageRecognitionPlugin)nn2.getPlugin(ImageRecognitionPlugin.class);
        ImageRecognitionPlugin ir3 = (ImageRecognitionPlugin)nn3.getPlugin(ImageRecognitionPlugin.class);
    
        //
        // Try to check all learning images
        //
        System.out.println("-----------------------------------------------------------------------------------------------------");
        for (File file : folder.listFiles ())
        {
            System.out.println("Checking '" + FilenameUtils.removeExtension(file.getName()) + "'");
            System.out.println("Java Runtime created network     : " + RecognizeImage(ir1, file.getPath()));
            System.out.println("NS created network (not trained) : " + RecognizeImage(ir2, file.getPath()));
            System.out.println("NS created network (trained)     : " + RecognizeImage(ir3, file.getPath()));
            System.out.println("-----------------------------------------------------------------------------------------------------");
        }
    }
    
    /**
     * 
     * @param imageRecognition
     * @param imagePath
     */
    private static String RecognizeImage(ImageRecognitionPlugin imageRecognition, String imagePath) {
        String result = "";
        try {
            HashMap<String, Double> output = imageRecognition.recognizeImage(new File(imagePath));
    
            NumberFormat formatter = new DecimalFormat("#0.0"); 
            double maxPercent = Double.MIN_VALUE;
            for (Map.Entry<String, Double> entry : output.entrySet()) {
    
                if ( entry.getValue() > maxPercent){
                    maxPercent = entry.getValue(); 
                    result = entry.getKey() + " (" + formatter.format(maxPercent * 100) + " %)";
                }
            }
    
        } catch(IOException ioe) {
         ioe.printStackTrace();
        }
    
        return result;
    }
    

    }

    And here are the results:

    Neuron layer size counts vector = [1200, 12, 4]
    apr 30, 2015 12:21:01 PM org.neuroph.core.learning.LearningRule onStart
    INFO: Learning Started
    apr 30, 2015 12:21:01 PM org.neuroph.core.learning.LearningRule onStop
    INFO: Learning Stoped
    apr 30, 2015 12:21:01 PM org.neuroph.core.learning.LearningRule onStart
    INFO: Learning Started
    apr 30, 2015 12:21:01 PM org.neuroph.core.learning.LearningRule onStop
    INFO: Learning Stoped


    Checking 'bird'
    Java Runtime created network : bird (64,8 %)
    NS created network (not trained) : bird (45,0 %)
    NS created network (trained) : bird (89,0 %)


    Checking 'cat'
    Java Runtime created network : bird (64,7 %)
    NS created network (not trained) : bird (58,0 %)
    NS created network (trained) : cat (92,7 %)


    Checking 'dog'
    Java Runtime created network : dog (70,3 %)
    NS created network (not trained) : dog (73,7 %)
    NS created network (trained) : dog (88,2 %)


    Checking 'rose'
    Java Runtime created network : rose (60,8 %)
    NS created network (not trained) : rose (47,7 %)
    NS created network (trained) : rose (91,5 %)


    Besides the numbers are differemt, the 'cat' picture is wrongly detected. I suspect I did something wrong in the learning process, but I cannot figure it out.

    Could you please advice what can be wrong?

     

    Last edit: Andrej K 2015-04-30
  • Zoran Sevarac

    Zoran Sevarac - 2015-05-01

    Have you saved the trained network?

     
  • Andrej K

    Andrej K - 2015-05-01

    Hi Zoran, thanks for reply. What do you mean by "saving trained network"? Do you mean that I shall runtime in java:

    1. create neural network
    2. train it
    3. save it to disk

    and eveyrhing shall be ok?

    I have tried to save neural network after training (using function nn1.save("c:\temp.nnet");), but nothing changed.

    Did I misunderstood it?

     

Log in to post a comment.