Java Code Examples for org.neuroph.core.NeuralNetwork#createFromFile()

The following examples show how to use org.neuroph.core.NeuralNetwork#createFromFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassifierEvaluationSample.java    From NeurophFramework with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Evaluation evaluation = new Evaluation();
    evaluation.addEvaluator(new ErrorEvaluator(new MeanSquaredError()));

    String[] classNames = {"Virginica", "Setosa", "Versicolor"};

    MultiLayerPerceptron neuralNet = (MultiLayerPerceptron) NeuralNetwork.createFromFile("irisNet.nnet");
    DataSet dataSet = DataSet.createFromFile("data_sets/iris_data_normalised.txt", 4, 3, ",");

    evaluation.addEvaluator(new ClassifierEvaluator.MultiClass(classNames));
    evaluation.evaluate(neuralNet, dataSet);

    ClassifierEvaluator evaluator = evaluation.getEvaluator(ClassifierEvaluator.MultiClass.class);
    ConfusionMatrix confusionMatrix = evaluator.getResult();
    System.out.println("Confusion matrrix:\r\n");
    System.out.println(confusionMatrix.toString() + "\r\n\r\n");
    System.out.println("Classification metrics\r\n");
    ClassificationMetrics[] metrics = ClassificationMetrics.createFromMatrix(confusionMatrix);
    ClassificationMetrics.Stats average = ClassificationMetrics.average(metrics);
    for (ClassificationMetrics cm : metrics) {
        System.out.println(cm.toString() + "\r\n");
    }
    System.out.println(average.toString());

}
 
Example 2
Source File: ImageRecognitionSample.java    From NeurophFramework with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
      // load trained neural network saved with NeurophStudio (specify existing neural network file here)
      NeuralNetwork nnet = NeuralNetwork.createFromFile("MyImageRecognition.nnet");
      // get the image recognition plugin from neural network
      ImageRecognitionPlugin imageRecognition = (ImageRecognitionPlugin)nnet.getPlugin(ImageRecognitionPlugin.class);

      try {
            // image recognition is done here
            HashMap<String, Double> output = imageRecognition.recognizeImage(new File("someImage.jpg")); // specify some existing image file here
            System.out.println(output.toString());
      } catch(IOException ioe) {
          System.out.println("Error: could not read file!");
      } catch (VectorSizeMismatchException vsme) {
          System.out.println("Error: Image dimensions dont !");
      }
}
 
Example 3
Source File: RecognizeLetter.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {

        // User input parameters
//***********************************************************************************************************************************
        String networkPath = "C:/Users/Mihailo/Desktop/OCR/nnet/nnet-12-0.01.nnet"; // path to the trained network                *
        String letterPath = "C:/Users/Mihailo/Desktop/OCR/letters/259.png"; // path to the letter for recognition                   *
//***********************************************************************************************************************************
        
        NeuralNetwork nnet = NeuralNetwork.createFromFile(networkPath);
        ImageRecognitionPlugin imageRecognition = (ImageRecognitionPlugin) nnet.getPlugin(ImageRecognitionPlugin.class);
        Map<String, Double> output = imageRecognition.recognizeImage(new File(letterPath));
        System.out.println("Recognized letter: "+OCRUtilities.getCharacter(output));

    }
 
Example 4
Source File: TrainNetwork.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
public void train() {
    System.out.println("Training neural network... ");
    MultiLayerPerceptron neuralNet = (MultiLayerPerceptron) NeuralNetwork.createFromFile(config.getTrainedNetworkFileName());

    DataSet dataSet = DataSet.load(config.getNormalizedBalancedFileName());
    neuralNet.getLearningRule().addListener(this);
    neuralNet.learn(dataSet);
    System.out.println("Saving trained neural network to file... ");
    neuralNet.save(config.getTrainedNetworkFileName());
    System.out.println("Neural network successfully saved!");
}
 
Example 5
Source File: Evaluate.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
public void evaluate() {
    System.out.println("Evaluating neural network...");
    //Loading neural network from file
    MultiLayerPerceptron neuralNet = (MultiLayerPerceptron) NeuralNetwork.createFromFile(config.getTrainedNetworkFileName());

    //Load normalized balanced data set from file
    DataSet dataSet = DataSet.load(config.getTestFileName());

    //Testing neural network
    testNeuralNetwork(neuralNet, dataSet);

}
 
Example 6
Source File: RunExampleEvaluation.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    NeuralNetwork nnet = NeuralNetwork.createFromFile("irisNet.nnet");
    DataSet dataSet =  DataSet.createFromFile("data_sets/iris_data_normalised.txt", 4, 3, ",");
    
    Evaluation.runFullEvaluation(nnet, dataSet);
     
}
 
Example 7
Source File: OCRTextRecognition.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
/**
 * @param networkPath path of the trained neural network
 */
public void setNetworkPath(String networkPath) {
    nnet = NeuralNetwork.createFromFile(networkPath);
    plugin = (ImageRecognitionPlugin) nnet.getPlugin(ImageRecognitionPlugin.class);
}
 
Example 8
Source File: XorMultiLayerPerceptronSample.java    From NeurophFramework with Apache License 2.0 4 votes vote down vote up
/**
     * Runs this sample
     */
    public void run() {

        // create training set (logical XOR function)
        DataSet trainingSet = new DataSet(2, 1);
        trainingSet.add(new DataSetRow(new double[]{0, 0}, new double[]{0}));
        trainingSet.add(new DataSetRow(new double[]{0, 1}, new double[]{1}));
        trainingSet.add(new DataSetRow(new double[]{1, 0}, new double[]{1}));
        trainingSet.add(new DataSetRow(new double[]{1, 1}, new double[]{0}));

        // create multi layer perceptron
        MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, 2, 3, 1);
        myMlPerceptron.randomizeWeights(new WeightsRandomizer(new Random(123)));

        System.out.println(Arrays.toString(myMlPerceptron.getWeights()));

        myMlPerceptron.setLearningRule(new BackPropagation());

        myMlPerceptron.getLearningRule().setLearningRate(0.5);
        // enable batch if using MomentumBackpropagation
//        if( myMlPerceptron.getLearningRule() instanceof MomentumBackpropagation )
//        	((MomentumBackpropagation)myMlPerceptron.getLearningRule()).setBatchMode(false);

        LearningRule learningRule = myMlPerceptron.getLearningRule();
        learningRule.addListener(this);

        // learn the training set
        System.out.println("Training neural network...");
        myMlPerceptron.learn(trainingSet);

        // test perceptron
        System.out.println("Testing trained neural network");
        testNeuralNetwork(myMlPerceptron, trainingSet);

        // save trained neural network
        myMlPerceptron.save("myMlPerceptron.nnet");

        // load saved neural network
        NeuralNetwork loadedMlPerceptron = NeuralNetwork.createFromFile("myMlPerceptron.nnet");

        // test loaded neural network
        System.out.println("Testing loaded neural network");
        testNeuralNetwork(loadedMlPerceptron, trainingSet);
    }