Java Code Examples for org.deeplearning4j.nn.graph.ComputationGraph#evaluate()

The following examples show how to use org.deeplearning4j.nn.graph.ComputationGraph#evaluate() . 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: TestModels.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private static void showTrainingPrecision(ComputationGraph vgg16, String classesNumber) throws IOException {
    File[] carTrackings = new File("CarTracking").listFiles();
    for (File carTracking : carTrackings) {
        if (carTracking.getName().contains(classesNumber)) {
            DataSetIterator dataSetIterator = ImageUtils.createDataSetIterator(carTracking,
                    Integer.parseInt(classesNumber), 64);
            Evaluation eval = vgg16.evaluate(dataSetIterator);
            log.info(eval.stats());
        }
    }
}
 
Example 2
Source File: TrainCifar10Model.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private void testResults(ComputationGraph cifar10, DataSetIterator testIterator, int iEpoch, String modelName) throws IOException {
        if (iEpoch % TEST_INTERVAL == 0) {
            Evaluation eval = cifar10.evaluate(testIterator);
            log.info(eval.stats());
            testIterator.reset();
        }
//        TestModels.TestResult test = TestModels.test(cifar10, modelName);
//        log.info("Test Results >> " + test);
    }
 
Example 3
Source File: TransferLearningVGG16.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private void evalOn(ComputationGraph vgg16Transfer, DataSetIterator testIterator, int iEpoch) throws IOException {
    log.info("Evaluate model at iteration " + iEpoch + " ....");
    Evaluation eval = vgg16Transfer.evaluate(testIterator);
    log.info(eval.stats());
    testIterator.reset();

}
 
Example 4
Source File: TestUtil.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Perform simple holdout with a given percentage
 *
 * @param clf Classifier
 * @param data Full dataset
 * @param p Split percentage
 */
public static void holdout(
    Dl4jMlpClassifier clf, Instances data, double p, AbstractInstanceIterator aii)
    throws Exception {

  holdout(clf, data, p);
  Instances[] split = splitTrainTest(data, p);

  Instances test = split[1];
  final DataSetIterator testIter = aii.getDataSetIterator(test, 42);
  final ComputationGraph model = clf.getModel();
  logger.info("DL4J Evaluation: ");
  org.deeplearning4j.eval.Evaluation evaluation = model.evaluate(testIter);
  logger.info(evaluation.stats());
}
 
Example 5
Source File: TestUtil.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Perform simple holdout with a given percentage
 *
 * @param clf Classifier
 * @param data Full dataset
 * @param p Split percentage
 */
public static void holdout(
    Dl4jMlpClassifier clf, Instances data, double p, AbstractInstanceIterator aii)
    throws Exception {

  holdout(clf, data, p);
  Instances[] split = splitTrainTest(data, p);

  Instances test = split[1];
  final DataSetIterator testIter = aii.getDataSetIterator(test, 42);
  final ComputationGraph model = clf.getModel();
  logger.info("DL4J Evaluation: ");
  org.deeplearning4j.eval.Evaluation evaluation = model.evaluate(testIter);
  logger.info(evaluation.stats());
}
 
Example 6
Source File: EvalTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiOutputEvalSimple(){
    Nd4j.getRandom().setSeed(12345);

    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(12345)
            .graphBuilder()
            .addInputs("in")
            .addLayer("out1", new OutputLayer.Builder().nIn(4).nOut(3).activation(Activation.SOFTMAX).build(), "in")
            .addLayer("out2", new OutputLayer.Builder().nIn(4).nOut(3).activation(Activation.SOFTMAX).build(), "in")
            .setOutputs("out1", "out2")
            .build();

    ComputationGraph cg = new ComputationGraph(conf);
    cg.init();

    List<MultiDataSet> list = new ArrayList<>();
    DataSetIterator iter = new IrisDataSetIterator(30, 150);
    while(iter.hasNext()){
        DataSet ds = iter.next();
        list.add(new org.nd4j.linalg.dataset.MultiDataSet(new INDArray[]{ds.getFeatures()}, new INDArray[]{ds.getLabels(), ds.getLabels()}));
    }

    org.nd4j.evaluation.classification.Evaluation e = new org.nd4j.evaluation.classification.Evaluation();
    org.nd4j.evaluation.regression.RegressionEvaluation e2 = new org.nd4j.evaluation.regression.RegressionEvaluation();
    Map<Integer,org.nd4j.evaluation.IEvaluation[]> evals = new HashMap<>();
    evals.put(0, new org.nd4j.evaluation.IEvaluation[]{e});
    evals.put(1, new org.nd4j.evaluation.IEvaluation[]{e2});

    cg.evaluate(new IteratorMultiDataSetIterator(list.iterator(), 30), evals);

    assertEquals(150, e.getNumRowCounter());
    assertEquals(150, e2.getExampleCountPerColumn().getInt(0));
}
 
Example 7
Source File: EvalTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiOutputEvalCG(){
    //Simple sanity check on evaluation

    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder()
            .graphBuilder()
            .addInputs("in")
            .layer("0", new EmbeddingSequenceLayer.Builder().nIn(10).nOut(10).build(), "in")
            .layer("1", new LSTM.Builder().nIn(10).nOut(10).build(), "0")
            .layer("2", new LSTM.Builder().nIn(10).nOut(10).build(), "0")
            .layer("out1", new RnnOutputLayer.Builder().nIn(10).nOut(10).activation(Activation.SOFTMAX).build(), "1")
            .layer("out2", new RnnOutputLayer.Builder().nIn(10).nOut(20).activation(Activation.SOFTMAX).build(), "2")
            .setOutputs("out1", "out2")
            .build();

    ComputationGraph cg = new ComputationGraph(conf);
    cg.init();

    org.nd4j.linalg.dataset.MultiDataSet mds = new org.nd4j.linalg.dataset.MultiDataSet(
            new INDArray[]{Nd4j.create(10, 1, 10)},
            new INDArray[]{Nd4j.create(10, 10, 10), Nd4j.create(10, 20, 10)});

    Map<Integer,org.nd4j.evaluation.IEvaluation[]> m = new HashMap<>();
    m.put(0, new org.nd4j.evaluation.IEvaluation[]{new org.nd4j.evaluation.classification.Evaluation()});
    m.put(1, new org.nd4j.evaluation.IEvaluation[]{new org.nd4j.evaluation.classification.Evaluation()});

    cg.evaluate(new SingletonMultiDataSetIterator(mds), m);
}
 
Example 8
Source File: ScoreUtil.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get the evaluation
 * for the given model and test dataset
 * @param model the model to get the evaluation from
 * @param testData the test data to do the evaluation on
 * @return the evaluation object with accumulated statistics
 * for the current test data
 */
public static Evaluation getEvaluation(ComputationGraph model, MultiDataSetIterator testData) {
    if (model.getNumOutputArrays() != 1)
        throw new IllegalStateException("GraphSetSetAccuracyScoreFunction cannot be "
                        + "applied to ComputationGraphs with more than one output. NumOutputs = "
                        + model.getNumOutputArrays());

    return model.evaluate(testData);
}
 
Example 9
Source File: ScoreUtil.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get the evaluation
 * for the given model and test dataset
 * @param model the model to get the evaluation from
 * @param testData the test data to do the evaluation on
 * @return the evaluation object with accumulated statistics
 * for the current test data
 */
public static Evaluation getEvaluation(ComputationGraph model, DataSetIterator testData) {
    if (model.getNumOutputArrays() != 1)
        throw new IllegalStateException("GraphSetSetAccuracyScoreFunctionDataSet cannot be "
                        + "applied to ComputationGraphs with more than one output. NumOutputs = "
                        + model.getNumOutputArrays());

    return model.evaluate(testData);
}
 
Example 10
Source File: TestSparkComputationGraph.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 60000L)
public void testEvaluationAndRoc() {
    for( int evalWorkers : new int[]{1, 4, 8}) {
        DataSetIterator iter = new IrisDataSetIterator(5, 150);

        //Make a 2-class version of iris:
        List<DataSet> l = new ArrayList<>();
        iter.reset();
        while (iter.hasNext()) {
            DataSet ds = iter.next();
            INDArray newL = Nd4j.create(ds.getLabels().size(0), 2);
            newL.putColumn(0, ds.getLabels().getColumn(0));
            newL.putColumn(1, ds.getLabels().getColumn(1));
            newL.getColumn(1).addi(ds.getLabels().getColumn(2));
            ds.setLabels(newL);
            l.add(ds);
        }

        iter = new ListDataSetIterator<>(l);

        ComputationGraph cg = getBasicNetIris2Class();

        Evaluation e = cg.evaluate(iter);
        ROC roc = cg.evaluateROC(iter, 32);


        SparkComputationGraph scg = new SparkComputationGraph(sc, cg, null);
        scg.setDefaultEvaluationWorkers(evalWorkers);


        JavaRDD<DataSet> rdd = sc.parallelize(l);
        rdd = rdd.repartition(20);

        Evaluation e2 = scg.evaluate(rdd);
        ROC roc2 = scg.evaluateROC(rdd);


        assertEquals(e2.accuracy(), e.accuracy(), 1e-3);
        assertEquals(e2.f1(), e.f1(), 1e-3);
        assertEquals(e2.getNumRowCounter(), e.getNumRowCounter(), 1e-3);
        assertEquals(e2.falseNegatives(), e.falseNegatives());
        assertEquals(e2.falsePositives(), e.falsePositives());
        assertEquals(e2.trueNegatives(), e.trueNegatives());
        assertEquals(e2.truePositives(), e.truePositives());
        assertEquals(e2.precision(), e.precision(), 1e-3);
        assertEquals(e2.recall(), e.recall(), 1e-3);
        assertEquals(e2.getConfusionMatrix(), e.getConfusionMatrix());

        assertEquals(roc.calculateAUC(), roc2.calculateAUC(), 1e-5);
        assertEquals(roc.calculateAUCPR(), roc2.calculateAUCPR(), 1e-5);
    }
}
 
Example 11
Source File: EvaluationScoreFunction.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public double score(ComputationGraph graph, DataSetIterator iterator) {
    Evaluation e = graph.evaluate(iterator);
    return e.scoreForMetric(metric);
}
 
Example 12
Source File: EvaluationScoreFunction.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public double score(ComputationGraph graph, MultiDataSetIterator iterator) {
    Evaluation e = graph.evaluate(iterator);
    return e.scoreForMetric(metric);
}
 
Example 13
Source File: TestSetAccuracyScoreFunction.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public double score(ComputationGraph graph, DataSetIterator iterator) {
    Evaluation e = graph.evaluate(iterator);
    return e.accuracy();
}
 
Example 14
Source File: TestSetAccuracyScoreFunction.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public double score(ComputationGraph graph, MultiDataSetIterator iterator) {
    Evaluation e = graph.evaluate(iterator);
    return e.accuracy();
}
 
Example 15
Source File: TestSetF1ScoreFunction.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public double score(ComputationGraph graph, DataSetIterator iterator) {
    Evaluation e = graph.evaluate(iterator);
    return e.f1();
}
 
Example 16
Source File: TestSetF1ScoreFunction.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public double score(ComputationGraph graph, MultiDataSetIterator iterator) {
    Evaluation e = graph.evaluate(iterator);
    return e.f1();
}