Search in sources :

Example 1 with ConvolutionalIterationListener

use of org.deeplearning4j.ui.weights.ConvolutionalIterationListener in project deeplearning4j by deeplearning4j.

the class ManualTests method testCNNActivations2.

@Test
public void testCNNActivations2() throws Exception {
    int nChannels = 1;
    int outputNum = 10;
    int batchSize = 64;
    int nEpochs = 10;
    int iterations = 1;
    int seed = 123;
    log.info("Load data....");
    DataSetIterator mnistTrain = new MnistDataSetIterator(batchSize, true, 12345);
    DataSetIterator mnistTest = new MnistDataSetIterator(batchSize, false, 12345);
    log.info("Build model....");
    MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().seed(seed).iterations(iterations).regularization(true).l2(0.0005).learningRate(//.biasLearningRate(0.02)
    0.01).weightInit(WeightInit.XAVIER).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).updater(Updater.NESTEROVS).momentum(0.9).list().layer(0, new ConvolutionLayer.Builder(5, 5).nIn(nChannels).stride(1, 1).nOut(20).activation(Activation.IDENTITY).build()).layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2).stride(2, 2).build()).layer(2, new ConvolutionLayer.Builder(5, 5).stride(1, 1).nOut(50).activation(Activation.IDENTITY).build()).layer(3, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2).stride(2, 2).build()).layer(4, new DenseLayer.Builder().activation(Activation.RELU).nOut(500).build()).layer(5, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nOut(outputNum).activation(Activation.SOFTMAX).build()).backprop(true).pretrain(false);
    // The builder needs the dimensions of the image along with the number of channels. these are 28x28 images in one channel
    new ConvolutionLayerSetup(builder, 28, 28, 1);
    MultiLayerConfiguration conf = builder.build();
    MultiLayerNetwork model = new MultiLayerNetwork(conf);
    model.init();
    /*
        ParallelWrapper wrapper = new ParallelWrapper.Builder(model)
            .averagingFrequency(1)
            .prefetchBuffer(12)
            .workers(2)
            .reportScoreAfterAveraging(false)
            .useLegacyAveraging(false)
            .build();
        */
    log.info("Train model....");
    model.setListeners(new ConvolutionalIterationListener(1));
    //((NativeOpExecutioner) Nd4j.getExecutioner()).getLoop().setOmpNumThreads(8);
    long timeX = System.currentTimeMillis();
    //        nEpochs = 2;
    for (int i = 0; i < nEpochs; i++) {
        long time1 = System.currentTimeMillis();
        model.fit(mnistTrain);
        //wrapper.fit(mnistTrain);
        long time2 = System.currentTimeMillis();
        log.info("*** Completed epoch {}, Time elapsed: {} ***", i, (time2 - time1));
    }
    long timeY = System.currentTimeMillis();
    log.info("Evaluate model....");
    Evaluation eval = new Evaluation(outputNum);
    while (mnistTest.hasNext()) {
        DataSet ds = mnistTest.next();
        INDArray output = model.output(ds.getFeatureMatrix(), false);
        eval.eval(ds.getLabels(), output);
    }
    log.info(eval.stats());
    mnistTest.reset();
    log.info("****************Example finished********************");
}
Also used : Evaluation(org.deeplearning4j.eval.Evaluation) MnistDataSetIterator(org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator) DataSet(org.nd4j.linalg.dataset.DataSet) ConvolutionalIterationListener(org.deeplearning4j.ui.weights.ConvolutionalIterationListener) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) ConvolutionLayerSetup(org.deeplearning4j.nn.conf.layers.setup.ConvolutionLayerSetup) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) LFWDataSetIterator(org.deeplearning4j.datasets.iterator.impl.LFWDataSetIterator) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) MnistDataSetIterator(org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator) Test(org.junit.Test)

Example 2 with ConvolutionalIterationListener

use of org.deeplearning4j.ui.weights.ConvolutionalIterationListener in project deeplearning4j by deeplearning4j.

the class ManualTests method testCNNActivationsVisualization.

/**
     * This test is for manual execution only, since it's here just to get working CNN and visualize it's layers
     *
     * @throws Exception
     */
@Test
public void testCNNActivationsVisualization() throws Exception {
    final int numRows = 40;
    final int numColumns = 40;
    int nChannels = 3;
    int outputNum = LFWLoader.NUM_LABELS;
    int numSamples = LFWLoader.NUM_IMAGES;
    boolean useSubset = false;
    // numSamples/10;
    int batchSize = 200;
    int iterations = 5;
    int splitTrainNum = (int) (batchSize * .8);
    int seed = 123;
    int listenerFreq = iterations / 5;
    DataSet lfwNext;
    SplitTestAndTrain trainTest;
    DataSet trainInput;
    List<INDArray> testInput = new ArrayList<>();
    List<INDArray> testLabels = new ArrayList<>();
    log.info("Load data....");
    DataSetIterator lfw = new LFWDataSetIterator(batchSize, numSamples, new int[] { numRows, numColumns, nChannels }, outputNum, useSubset, true, 1.0, new Random(seed));
    log.info("Build model....");
    MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().seed(seed).iterations(iterations).activation(Activation.RELU).weightInit(WeightInit.XAVIER).gradientNormalization(GradientNormalization.RenormalizeL2PerLayer).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).learningRate(0.01).momentum(0.9).regularization(true).updater(Updater.ADAGRAD).useDropConnect(true).list().layer(0, new ConvolutionLayer.Builder(4, 4).name("cnn1").nIn(nChannels).stride(1, 1).nOut(20).build()).layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX, new int[] { 2, 2 }).name("pool1").build()).layer(2, new ConvolutionLayer.Builder(3, 3).name("cnn2").stride(1, 1).nOut(40).build()).layer(3, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX, new int[] { 2, 2 }).name("pool2").build()).layer(4, new ConvolutionLayer.Builder(3, 3).name("cnn3").stride(1, 1).nOut(60).build()).layer(5, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX, new int[] { 2, 2 }).name("pool3").build()).layer(6, new ConvolutionLayer.Builder(2, 2).name("cnn3").stride(1, 1).nOut(80).build()).layer(7, new DenseLayer.Builder().name("ffn1").nOut(160).dropOut(0.5).build()).layer(8, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nOut(outputNum).activation(Activation.SOFTMAX).build()).backprop(true).pretrain(false);
    new ConvolutionLayerSetup(builder, numRows, numColumns, nChannels);
    MultiLayerNetwork model = new MultiLayerNetwork(builder.build());
    model.init();
    log.info("Train model....");
    model.setListeners(Arrays.asList(new ScoreIterationListener(listenerFreq), new ConvolutionalIterationListener(listenerFreq)));
    while (lfw.hasNext()) {
        lfwNext = lfw.next();
        lfwNext.scale();
        // train set that is the result
        trainTest = lfwNext.splitTestAndTrain(splitTrainNum, new Random(seed));
        // get feature matrix and labels for training
        trainInput = trainTest.getTrain();
        testInput.add(trainTest.getTest().getFeatureMatrix());
        testLabels.add(trainTest.getTest().getLabels());
        model.fit(trainInput);
    }
    log.info("Evaluate model....");
    Evaluation eval = new Evaluation(lfw.getLabels());
    for (int i = 0; i < testInput.size(); i++) {
        INDArray output = model.output(testInput.get(i));
        eval.eval(testLabels.get(i), output);
    }
    INDArray output = model.output(testInput.get(0));
    eval.eval(testLabels.get(0), output);
    log.info(eval.stats());
    log.info("****************Example finished********************");
}
Also used : Evaluation(org.deeplearning4j.eval.Evaluation) DataSet(org.nd4j.linalg.dataset.DataSet) LFWDataSetIterator(org.deeplearning4j.datasets.iterator.impl.LFWDataSetIterator) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ConvolutionalIterationListener(org.deeplearning4j.ui.weights.ConvolutionalIterationListener) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) ConvolutionLayerSetup(org.deeplearning4j.nn.conf.layers.setup.ConvolutionLayerSetup) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) SplitTestAndTrain(org.nd4j.linalg.dataset.SplitTestAndTrain) LFWDataSetIterator(org.deeplearning4j.datasets.iterator.impl.LFWDataSetIterator) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) MnistDataSetIterator(org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator) Test(org.junit.Test)

Aggregations

LFWDataSetIterator (org.deeplearning4j.datasets.iterator.impl.LFWDataSetIterator)2 MnistDataSetIterator (org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator)2 Evaluation (org.deeplearning4j.eval.Evaluation)2 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)2 ConvolutionLayerSetup (org.deeplearning4j.nn.conf.layers.setup.ConvolutionLayerSetup)2 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)2 ConvolutionalIterationListener (org.deeplearning4j.ui.weights.ConvolutionalIterationListener)2 Test (org.junit.Test)2 INDArray (org.nd4j.linalg.api.ndarray.INDArray)2 DataSet (org.nd4j.linalg.dataset.DataSet)2 DataSetIterator (org.nd4j.linalg.dataset.api.iterator.DataSetIterator)2 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)1 ScoreIterationListener (org.deeplearning4j.optimize.listeners.ScoreIterationListener)1 SplitTestAndTrain (org.nd4j.linalg.dataset.SplitTestAndTrain)1