Search in sources :

Example 1 with CnnToFeedForwardPreProcessor

use of org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor in project deeplearning4j by deeplearning4j.

the class TestComputationGraphNetwork method testPreprocessorAddition.

@Test
public void testPreprocessorAddition() {
    //Also check that nIns are set automatically
    //First: check FF -> RNN
    ComputationGraphConfiguration conf1 = new NeuralNetConfiguration.Builder().graphBuilder().addInputs("in").setInputTypes(InputType.feedForward(5)).addLayer("rnn", new GravesLSTM.Builder().nOut(5).build(), "in").addLayer("out", new RnnOutputLayer.Builder().nOut(5).build(), "rnn").setOutputs("out").build();
    assertEquals(5, ((FeedForwardLayer) ((LayerVertex) conf1.getVertices().get("rnn")).getLayerConf().getLayer()).getNIn());
    assertEquals(5, ((FeedForwardLayer) ((LayerVertex) conf1.getVertices().get("out")).getLayerConf().getLayer()).getNIn());
    LayerVertex lv1 = (LayerVertex) conf1.getVertices().get("rnn");
    assertTrue(lv1.getPreProcessor() instanceof FeedForwardToRnnPreProcessor);
    LayerVertex lv2 = (LayerVertex) conf1.getVertices().get("out");
    assertNull(lv2.getPreProcessor());
    //Check RNN -> FF -> RNN
    ComputationGraphConfiguration conf2 = new NeuralNetConfiguration.Builder().graphBuilder().addInputs("in").setInputTypes(InputType.recurrent(5)).addLayer("ff", new DenseLayer.Builder().nOut(5).build(), "in").addLayer("out", new RnnOutputLayer.Builder().nOut(5).build(), "ff").setOutputs("out").build();
    assertEquals(5, ((FeedForwardLayer) ((LayerVertex) conf2.getVertices().get("ff")).getLayerConf().getLayer()).getNIn());
    assertEquals(5, ((FeedForwardLayer) ((LayerVertex) conf2.getVertices().get("out")).getLayerConf().getLayer()).getNIn());
    lv1 = (LayerVertex) conf2.getVertices().get("ff");
    assertTrue(lv1.getPreProcessor() instanceof RnnToFeedForwardPreProcessor);
    lv2 = (LayerVertex) conf2.getVertices().get("out");
    assertTrue(lv2.getPreProcessor() instanceof FeedForwardToRnnPreProcessor);
    //CNN -> Dense
    ComputationGraphConfiguration conf3 = new NeuralNetConfiguration.Builder().graphBuilder().addInputs("in").setInputTypes(InputType.convolutional(28, 28, 1)).addLayer("cnn", new ConvolutionLayer.Builder().kernelSize(2, 2).padding(0, 0).stride(2, 2).nOut(3).build(), //(28-2+0)/2+1 = 14
    "in").addLayer("pool", new SubsamplingLayer.Builder().kernelSize(2, 2).padding(0, 0).stride(2, 2).build(), //(14-2+0)/2+1=7
    "cnn").addLayer("dense", new DenseLayer.Builder().nOut(10).build(), "pool").addLayer("out", new OutputLayer.Builder().nIn(10).nOut(5).build(), "dense").setOutputs("out").build();
    //Check preprocessors:
    lv1 = (LayerVertex) conf3.getVertices().get("cnn");
    //Shouldn't be adding preprocessor here
    assertNull(lv1.getPreProcessor());
    lv2 = (LayerVertex) conf3.getVertices().get("pool");
    assertNull(lv2.getPreProcessor());
    LayerVertex lv3 = (LayerVertex) conf3.getVertices().get("dense");
    assertTrue(lv3.getPreProcessor() instanceof CnnToFeedForwardPreProcessor);
    CnnToFeedForwardPreProcessor proc = (CnnToFeedForwardPreProcessor) lv3.getPreProcessor();
    assertEquals(3, proc.getNumChannels());
    assertEquals(7, proc.getInputHeight());
    assertEquals(7, proc.getInputWidth());
    LayerVertex lv4 = (LayerVertex) conf3.getVertices().get("out");
    assertNull(lv4.getPreProcessor());
    //Check nIns:
    assertEquals(7 * 7 * 3, ((FeedForwardLayer) lv3.getLayerConf().getLayer()).getNIn());
    //CNN->Dense, RNN->Dense, Dense->RNN
    ComputationGraphConfiguration conf4 = new NeuralNetConfiguration.Builder().graphBuilder().addInputs("inCNN", "inRNN").setInputTypes(InputType.convolutional(28, 28, 1), InputType.recurrent(5)).addLayer("cnn", new ConvolutionLayer.Builder().kernelSize(2, 2).padding(0, 0).stride(2, 2).nOut(3).build(), //(28-2+0)/2+1 = 14
    "inCNN").addLayer("pool", new SubsamplingLayer.Builder().kernelSize(2, 2).padding(0, 0).stride(2, 2).build(), //(14-2+0)/2+1=7
    "cnn").addLayer("dense", new DenseLayer.Builder().nOut(10).build(), "pool").addLayer("dense2", new DenseLayer.Builder().nOut(10).build(), "inRNN").addVertex("merge", new MergeVertex(), "dense", "dense2").addLayer("out", new RnnOutputLayer.Builder().nOut(5).build(), "merge").setOutputs("out").build();
    //Check preprocessors:
    lv1 = (LayerVertex) conf4.getVertices().get("cnn");
    //Expect no preprocessor: cnn data -> cnn layer
    assertNull(lv1.getPreProcessor());
    lv2 = (LayerVertex) conf4.getVertices().get("pool");
    assertNull(lv2.getPreProcessor());
    lv3 = (LayerVertex) conf4.getVertices().get("dense");
    assertTrue(lv3.getPreProcessor() instanceof CnnToFeedForwardPreProcessor);
    proc = (CnnToFeedForwardPreProcessor) lv3.getPreProcessor();
    assertEquals(3, proc.getNumChannels());
    assertEquals(7, proc.getInputHeight());
    assertEquals(7, proc.getInputWidth());
    lv4 = (LayerVertex) conf4.getVertices().get("dense2");
    assertTrue(lv4.getPreProcessor() instanceof RnnToFeedForwardPreProcessor);
    LayerVertex lv5 = (LayerVertex) conf4.getVertices().get("out");
    assertTrue(lv5.getPreProcessor() instanceof FeedForwardToRnnPreProcessor);
    //Check nIns:
    assertEquals(7 * 7 * 3, ((FeedForwardLayer) lv3.getLayerConf().getLayer()).getNIn());
    assertEquals(5, ((FeedForwardLayer) lv4.getLayerConf().getLayer()).getNIn());
    //10+10 out of the merge vertex -> 20 in to output layer vertex
    assertEquals(20, ((FeedForwardLayer) lv5.getLayerConf().getLayer()).getNIn());
    //Input to 2 CNN layers:
    ComputationGraphConfiguration conf5 = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).graphBuilder().addInputs("input").setInputTypes(InputType.convolutional(28, 28, 1)).addLayer("cnn_1", new ConvolutionLayer.Builder(2, 2).stride(2, 2).nIn(1).nOut(3).build(), "input").addLayer("cnn_2", new ConvolutionLayer.Builder(4, 4).stride(2, 2).padding(1, 1).nIn(1).nOut(3).build(), "input").addLayer("max_1", new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2).build(), "cnn_1", "cnn_2").addLayer("output", new OutputLayer.Builder().nOut(10).build(), //.nIn(7 * 7 * 6)
    "max_1").setOutputs("output").pretrain(false).backprop(true).build();
    lv1 = (LayerVertex) conf5.getVertices().get("cnn_1");
    //Expect no preprocessor: cnn data -> cnn layer
    assertNull(lv1.getPreProcessor());
    lv2 = (LayerVertex) conf5.getVertices().get("cnn_2");
    //Expect no preprocessor: cnn data -> cnn layer
    assertNull(lv2.getPreProcessor());
    assertNull(((LayerVertex) conf5.getVertices().get("max_1")).getPreProcessor());
    lv3 = (LayerVertex) conf5.getVertices().get("output");
    assertTrue(lv3.getPreProcessor() instanceof CnnToFeedForwardPreProcessor);
    CnnToFeedForwardPreProcessor cnnff = (CnnToFeedForwardPreProcessor) lv3.getPreProcessor();
    assertEquals(6, cnnff.getNumChannels());
    assertEquals(7, cnnff.getInputHeight());
    assertEquals(7, cnnff.getInputWidth());
}
Also used : LayerVertex(org.deeplearning4j.nn.conf.graph.LayerVertex) CnnToFeedForwardPreProcessor(org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor) MergeVertex(org.deeplearning4j.nn.conf.graph.MergeVertex) RnnToFeedForwardPreProcessor(org.deeplearning4j.nn.conf.preprocessor.RnnToFeedForwardPreProcessor) FeedForwardToRnnPreProcessor(org.deeplearning4j.nn.conf.preprocessor.FeedForwardToRnnPreProcessor) Test(org.junit.Test)

Example 2 with CnnToFeedForwardPreProcessor

use of org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor in project deeplearning4j by deeplearning4j.

the class MultiLayerNeuralNetConfigurationTest method testYaml.

@Test
public void testYaml() throws Exception {
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().list().layer(0, new RBM.Builder().dist(new NormalDistribution(1, 1e-1)).build()).inputPreProcessor(0, new CnnToFeedForwardPreProcessor()).build();
    String json = conf.toYaml();
    MultiLayerConfiguration from = MultiLayerConfiguration.fromYaml(json);
    assertEquals(conf.getConf(0), from.getConf(0));
    Properties props = new Properties();
    props.put("json", json);
    String key = props.getProperty("json");
    assertEquals(json, key);
    File f = new File("props");
    f.deleteOnExit();
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
    props.store(bos, "");
    bos.flush();
    bos.close();
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
    Properties props2 = new Properties();
    props2.load(bis);
    bis.close();
    assertEquals(props2.getProperty("json"), props.getProperty("json"));
    String yaml = props2.getProperty("json");
    MultiLayerConfiguration conf3 = MultiLayerConfiguration.fromYaml(yaml);
    assertEquals(conf.getConf(0), conf3.getConf(0));
}
Also used : CnnToFeedForwardPreProcessor(org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor) Properties(java.util.Properties) NormalDistribution(org.deeplearning4j.nn.conf.distribution.NormalDistribution) Test(org.junit.Test)

Example 3 with CnnToFeedForwardPreProcessor

use of org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor in project deeplearning4j by deeplearning4j.

the class MultiLayerNeuralNetConfigurationTest method testJson.

@Test
public void testJson() throws Exception {
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().list().layer(0, new RBM.Builder().dist(new NormalDistribution(1, 1e-1)).build()).inputPreProcessor(0, new CnnToFeedForwardPreProcessor()).build();
    String json = conf.toJson();
    MultiLayerConfiguration from = MultiLayerConfiguration.fromJson(json);
    assertEquals(conf.getConf(0), from.getConf(0));
    Properties props = new Properties();
    props.put("json", json);
    String key = props.getProperty("json");
    assertEquals(json, key);
    File f = new File("props");
    f.deleteOnExit();
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
    props.store(bos, "");
    bos.flush();
    bos.close();
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
    Properties props2 = new Properties();
    props2.load(bis);
    bis.close();
    assertEquals(props2.getProperty("json"), props.getProperty("json"));
    String json2 = props2.getProperty("json");
    MultiLayerConfiguration conf3 = MultiLayerConfiguration.fromJson(json2);
    assertEquals(conf.getConf(0), conf3.getConf(0));
}
Also used : CnnToFeedForwardPreProcessor(org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor) Properties(java.util.Properties) NormalDistribution(org.deeplearning4j.nn.conf.distribution.NormalDistribution) Test(org.junit.Test)

Example 4 with CnnToFeedForwardPreProcessor

use of org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor in project deeplearning4j by deeplearning4j.

the class GradientCheckTestsComputationGraph method testCnnDepthMerge.

@Test
public void testCnnDepthMerge() {
    Nd4j.getRandom().setSeed(12345);
    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 0.1)).updater(Updater.NONE).learningRate(1.0).graphBuilder().addInputs("input").addLayer("l1", new ConvolutionLayer.Builder().kernelSize(2, 2).stride(1, 1).padding(0, 0).nIn(2).nOut(2).activation(Activation.TANH).build(), "input").addLayer("l2", new ConvolutionLayer.Builder().kernelSize(2, 2).stride(1, 1).padding(0, 0).nIn(2).nOut(2).activation(Activation.TANH).build(), "input").addVertex("merge", new MergeVertex(), "l1", "l2").addLayer("outputLayer", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(5 * 5 * (2 + 2)).nOut(3).build(), "merge").setOutputs("outputLayer").inputPreProcessor("outputLayer", new CnnToFeedForwardPreProcessor(5, 5, 4)).pretrain(false).backprop(true).build();
    ComputationGraph graph = new ComputationGraph(conf);
    graph.init();
    Random r = new Random(12345);
    //Order: examples, channels, height, width
    INDArray input = Nd4j.rand(new int[] { 5, 2, 6, 6 });
    INDArray labels = Nd4j.zeros(5, 3);
    for (int i = 0; i < 5; i++) labels.putScalar(new int[] { i, r.nextInt(3) }, 1.0);
    if (PRINT_RESULTS) {
        System.out.println("testCnnDepthMerge()");
        for (int j = 0; j < graph.getNumLayers(); j++) System.out.println("Layer " + j + " # params: " + graph.getLayer(j).numParams());
    }
    boolean gradOK = GradientCheckUtil.checkGradients(graph, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR, DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, new INDArray[] { input }, new INDArray[] { labels });
    String msg = "testCnnDepthMerge()";
    assertTrue(msg, gradOK);
}
Also used : CnnToFeedForwardPreProcessor(org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) Random(java.util.Random) INDArray(org.nd4j.linalg.api.ndarray.INDArray) NormalDistribution(org.deeplearning4j.nn.conf.distribution.NormalDistribution) ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration) ComputationGraph(org.deeplearning4j.nn.graph.ComputationGraph) Test(org.junit.Test)

Example 5 with CnnToFeedForwardPreProcessor

use of org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor in project deeplearning4j by deeplearning4j.

the class MultiLayerTest method testSummary.

@Test
public void testSummary() {
    int V_WIDTH = 130;
    int V_HEIGHT = 130;
    int V_NFRAMES = 150;
    MultiLayerConfiguration confForArchitecture = //l2 regularization on all layers
    new NeuralNetConfiguration.Builder().seed(12345).regularization(true).l2(0.001).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).learningRate(0.4).list().layer(0, //3 channels: RGB
    new ConvolutionLayer.Builder(10, 10).nIn(3).nOut(30).stride(4, 4).activation(Activation.RELU).weightInit(WeightInit.RELU).updater(Updater.ADAGRAD).build()).layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(3, 3).stride(2, 2).build()).layer(2, new ConvolutionLayer.Builder(3, 3).nIn(30).nOut(10).stride(2, 2).activation(Activation.RELU).weightInit(WeightInit.RELU).updater(Updater.ADAGRAD).build()).layer(3, new DenseLayer.Builder().activation(Activation.RELU).nIn(490).nOut(50).weightInit(WeightInit.RELU).updater(Updater.ADAGRAD).gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue).gradientNormalizationThreshold(10).learningRate(0.5).build()).layer(4, new GravesLSTM.Builder().activation(Activation.SOFTSIGN).nIn(50).nOut(50).weightInit(WeightInit.XAVIER).updater(Updater.ADAGRAD).gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue).gradientNormalizationThreshold(10).learningRate(0.6).build()).layer(5, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(50).nOut(//4 possible shapes: circle, square, arc, line
    4).updater(Updater.ADAGRAD).weightInit(WeightInit.XAVIER).gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue).gradientNormalizationThreshold(10).build()).inputPreProcessor(0, new RnnToCnnPreProcessor(V_HEIGHT, V_WIDTH, 3)).inputPreProcessor(3, new CnnToFeedForwardPreProcessor(7, 7, 10)).inputPreProcessor(4, new FeedForwardToRnnPreProcessor()).pretrain(false).backprop(true).backpropType(BackpropType.TruncatedBPTT).tBPTTForwardLength(V_NFRAMES / 5).tBPTTBackwardLength(V_NFRAMES / 5).build();
    MultiLayerNetwork modelExpectedArch = new MultiLayerNetwork(confForArchitecture);
    modelExpectedArch.init();
    MultiLayerNetwork modelMow = new TransferLearning.Builder(modelExpectedArch).setFeatureExtractor(2).build();
    System.out.println(modelExpectedArch.summary());
    System.out.println(modelMow.summary());
}
Also used : RnnToCnnPreProcessor(org.deeplearning4j.nn.conf.preprocessor.RnnToCnnPreProcessor) CnnToFeedForwardPreProcessor(org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor) FeedForwardToRnnPreProcessor(org.deeplearning4j.nn.conf.preprocessor.FeedForwardToRnnPreProcessor) Test(org.junit.Test)

Aggregations

CnnToFeedForwardPreProcessor (org.deeplearning4j.nn.conf.preprocessor.CnnToFeedForwardPreProcessor)16 Test (org.junit.Test)15 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)6 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)4 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)4 ComputationGraphConfiguration (org.deeplearning4j.nn.conf.ComputationGraphConfiguration)3 NormalDistribution (org.deeplearning4j.nn.conf.distribution.NormalDistribution)3 OutputLayer (org.deeplearning4j.nn.conf.layers.OutputLayer)3 FeedForwardToCnnPreProcessor (org.deeplearning4j.nn.conf.preprocessor.FeedForwardToCnnPreProcessor)3 FeedForwardToRnnPreProcessor (org.deeplearning4j.nn.conf.preprocessor.FeedForwardToRnnPreProcessor)3 File (java.io.File)2 Properties (java.util.Properties)2 ConvolutionLayer (org.deeplearning4j.nn.conf.layers.ConvolutionLayer)2 SubsamplingLayer (org.deeplearning4j.nn.conf.layers.SubsamplingLayer)2 RnnToCnnPreProcessor (org.deeplearning4j.nn.conf.preprocessor.RnnToCnnPreProcessor)2 ComputationGraph (org.deeplearning4j.nn.graph.ComputationGraph)2 ScoreIterationListener (org.deeplearning4j.optimize.listeners.ScoreIterationListener)2 INDArray (org.nd4j.linalg.api.ndarray.INDArray)2 ClassPathResource (org.nd4j.linalg.io.ClassPathResource)2 LossNegativeLogLikelihood (org.nd4j.linalg.lossfunctions.impl.LossNegativeLogLikelihood)2