Search in sources :

Example 1 with DL4JException

use of org.deeplearning4j.exception.DL4JException in project deeplearning4j by deeplearning4j.

the class TestConvolutionModes method testConvolutionModeInputTypes.

@Test
public void testConvolutionModeInputTypes() {
    //Test 1: input 3x3, stride 1, kernel 2
    int inH = 3;
    int inW = 3;
    int kH = 2;
    int kW = 2;
    int sH = 1;
    int sW = 1;
    int pH = 0;
    int pW = 0;
    int minibatch = 3;
    int dIn = 5;
    int dOut = 7;
    int[] kernel = { kH, kW };
    int[] stride = { sH, sW };
    int[] padding = { pH, pW };
    INDArray inData = Nd4j.create(minibatch, dIn, inH, inW);
    InputType inputType = InputType.convolutional(inH, inW, dIn);
    //Strict mode: expect 2x2 out -> (inH - kernel + 2*padding)/stride + 1 = (3-2+0)/1+1 = 2
    InputType.InputTypeConvolutional it = (InputType.InputTypeConvolutional) InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, padding, ConvolutionMode.Strict, dOut, -1, "layerName", ConvolutionLayer.class);
    assertEquals(2, it.getHeight());
    assertEquals(2, it.getWidth());
    assertEquals(dOut, it.getDepth());
    int[] outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, padding, ConvolutionMode.Strict);
    assertEquals(2, outSize[0]);
    assertEquals(2, outSize[1]);
    //Truncate: same as strict here
    it = (InputType.InputTypeConvolutional) InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, padding, ConvolutionMode.Truncate, dOut, -1, "layerName", ConvolutionLayer.class);
    assertEquals(2, it.getHeight());
    assertEquals(2, it.getWidth());
    assertEquals(dOut, it.getDepth());
    outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, padding, ConvolutionMode.Truncate);
    assertEquals(2, outSize[0]);
    assertEquals(2, outSize[1]);
    //Same mode: ceil(in / stride) = 3
    it = (InputType.InputTypeConvolutional) InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, null, ConvolutionMode.Same, dOut, -1, "layerName", ConvolutionLayer.class);
    assertEquals(3, it.getHeight());
    assertEquals(3, it.getWidth());
    assertEquals(dOut, it.getDepth());
    outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, null, ConvolutionMode.Same);
    assertEquals(3, outSize[0]);
    assertEquals(3, outSize[1]);
    //Test 2: input 3x4, stride 2, kernel 3
    inH = 3;
    inW = 4;
    kH = 3;
    kW = 3;
    sH = 2;
    sW = 2;
    kernel = new int[] { kH, kW };
    stride = new int[] { sH, sW };
    padding = new int[] { pH, pW };
    inData = Nd4j.create(minibatch, dIn, inH, inW);
    inputType = InputType.convolutional(inH, inW, dIn);
    //Strict mode: (4-3+0)/2+1 is not an integer -> exception
    try {
        InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, padding, ConvolutionMode.Strict, dOut, -1, "layerName", ConvolutionLayer.class);
        fail("Expected exception");
    } catch (DL4JException e) {
        System.out.println(e.getMessage());
    }
    try {
        outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, padding, ConvolutionMode.Strict);
        fail("Exception expected");
    } catch (DL4JException e) {
        System.out.println(e.getMessage());
    }
    //Truncate: (3-3+0)/2+1 = 1 in height dim; (4-3+0)/2+1 = 1 in width dim
    it = (InputType.InputTypeConvolutional) InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, padding, ConvolutionMode.Truncate, dOut, -1, "layerName", ConvolutionLayer.class);
    assertEquals(1, it.getHeight());
    assertEquals(1, it.getWidth());
    assertEquals(dOut, it.getDepth());
    outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, padding, ConvolutionMode.Truncate);
    assertEquals(1, outSize[0]);
    assertEquals(1, outSize[1]);
    //Same mode: ceil(3/2) = 2 in height dim; ceil(4/2) = 2 in width dimension
    it = (InputType.InputTypeConvolutional) InputTypeUtil.getOutputTypeCnnLayers(inputType, kernel, stride, null, ConvolutionMode.Same, dOut, -1, "layerName", ConvolutionLayer.class);
    assertEquals(2, it.getHeight());
    assertEquals(2, it.getWidth());
    assertEquals(dOut, it.getDepth());
    outSize = ConvolutionUtils.getOutputSize(inData, kernel, stride, null, ConvolutionMode.Same);
    assertEquals(2, outSize[0]);
    assertEquals(2, outSize[1]);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) InputType(org.deeplearning4j.nn.conf.inputs.InputType) DL4JException(org.deeplearning4j.exception.DL4JException) ConvolutionLayer(org.deeplearning4j.nn.conf.layers.ConvolutionLayer) Test(org.junit.Test)

Example 2 with DL4JException

use of org.deeplearning4j.exception.DL4JException in project deeplearning4j by deeplearning4j.

the class TestConvolutionModes method testStrictTruncateConvolutionModeOutput.

@Test
public void testStrictTruncateConvolutionModeOutput() {
    //Idea: with convolution mode == Truncate, input size shouldn't matter (within the bounds of truncated edge),
    // and edge data shouldn't affect the output
    //Use: 9x9, kernel 3, stride 3, padding 0
    // Should get same output for 10x10 and 11x11...
    Nd4j.getRandom().setSeed(12345);
    int[] minibatches = { 1, 3 };
    int[] inDepths = { 1, 3 };
    int[] inSizes = { 9, 10, 11 };
    for (boolean isSubsampling : new boolean[] { false, true }) {
        for (int minibatch : minibatches) {
            for (int inDepth : inDepths) {
                INDArray origData = Nd4j.rand(new int[] { minibatch, inDepth, 9, 9 });
                for (int inSize : inSizes) {
                    for (ConvolutionMode cm : new ConvolutionMode[] { ConvolutionMode.Strict, ConvolutionMode.Truncate }) {
                        INDArray inputData = Nd4j.rand(new int[] { minibatch, inDepth, inSize, inSize });
                        inputData.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(0, 9), NDArrayIndex.interval(0, 9)).assign(origData);
                        Layer layer;
                        if (isSubsampling) {
                            layer = new SubsamplingLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).build();
                        } else {
                            layer = new ConvolutionLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0).nOut(3).build();
                        }
                        MultiLayerNetwork net = null;
                        try {
                            MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).convolutionMode(cm).list().layer(0, layer).layer(1, new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).nOut(3).build()).setInputType(InputType.convolutional(inSize, inSize, inDepth)).build();
                            net = new MultiLayerNetwork(conf);
                            net.init();
                            if (inSize > 9 && cm == ConvolutionMode.Strict) {
                                fail("Expected exception");
                            }
                        } catch (DL4JException e) {
                            if (inSize == 9 || cm != ConvolutionMode.Strict) {
                                e.printStackTrace();
                                fail("Unexpected exception");
                            }
                            //Expected exception
                            continue;
                        } catch (Exception e) {
                            e.printStackTrace();
                            fail("Unexpected exception");
                        }
                        INDArray out = net.output(origData);
                        INDArray out2 = net.output(inputData);
                        assertEquals(out, out2);
                    }
                }
            }
        }
    }
}
Also used : DL4JException(org.deeplearning4j.exception.DL4JException) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ConvolutionLayer(org.deeplearning4j.nn.conf.layers.ConvolutionLayer) DL4JException(org.deeplearning4j.exception.DL4JException) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) ConvolutionMode(org.deeplearning4j.nn.conf.ConvolutionMode) Test(org.junit.Test)

Example 3 with DL4JException

use of org.deeplearning4j.exception.DL4JException in project deeplearning4j by deeplearning4j.

the class TestRecordReaders method testClassIndexOutsideOfRangeRRMDSI_MultipleReaders.

@Test
public void testClassIndexOutsideOfRangeRRMDSI_MultipleReaders() {
    Collection<Collection<Collection<Writable>>> c1 = new ArrayList<>();
    Collection<Collection<Writable>> seq1 = new ArrayList<>();
    seq1.add(Arrays.<Writable>asList(new DoubleWritable(0.0)));
    seq1.add(Arrays.<Writable>asList(new DoubleWritable(0.0)));
    c1.add(seq1);
    Collection<Collection<Writable>> seq2 = new ArrayList<>();
    seq2.add(Arrays.<Writable>asList(new DoubleWritable(0.0)));
    seq2.add(Arrays.<Writable>asList(new DoubleWritable(0.0)));
    c1.add(seq2);
    Collection<Collection<Collection<Writable>>> c2 = new ArrayList<>();
    Collection<Collection<Writable>> seq1a = new ArrayList<>();
    seq1a.add(Arrays.<Writable>asList(new IntWritable(0)));
    seq1a.add(Arrays.<Writable>asList(new IntWritable(1)));
    c2.add(seq1a);
    Collection<Collection<Writable>> seq2a = new ArrayList<>();
    seq2a.add(Arrays.<Writable>asList(new IntWritable(0)));
    seq2a.add(Arrays.<Writable>asList(new IntWritable(2)));
    c2.add(seq2a);
    CollectionSequenceRecordReader csrr = new CollectionSequenceRecordReader(c1);
    CollectionSequenceRecordReader csrrLabels = new CollectionSequenceRecordReader(c2);
    DataSetIterator dsi = new SequenceRecordReaderDataSetIterator(csrr, csrrLabels, 2, 2);
    try {
        DataSet ds = dsi.next();
        fail("Expected exception");
    } catch (DL4JException e) {
        System.out.println("testClassIndexOutsideOfRangeRRMDSI_MultipleReaders(): " + e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : SequenceRecordReaderDataSetIterator(org.deeplearning4j.datasets.datavec.SequenceRecordReaderDataSetIterator) DataSet(org.nd4j.linalg.dataset.api.DataSet) ArrayList(java.util.ArrayList) CollectionSequenceRecordReader(org.datavec.api.records.reader.impl.collection.CollectionSequenceRecordReader) IntWritable(org.datavec.api.writable.IntWritable) Writable(org.datavec.api.writable.Writable) DoubleWritable(org.datavec.api.writable.DoubleWritable) DoubleWritable(org.datavec.api.writable.DoubleWritable) DL4JException(org.deeplearning4j.exception.DL4JException) DL4JException(org.deeplearning4j.exception.DL4JException) Collection(java.util.Collection) IntWritable(org.datavec.api.writable.IntWritable) SequenceRecordReaderDataSetIterator(org.deeplearning4j.datasets.datavec.SequenceRecordReaderDataSetIterator) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) RecordReaderDataSetIterator(org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator) Test(org.junit.Test)

Example 4 with DL4JException

use of org.deeplearning4j.exception.DL4JException in project deeplearning4j by deeplearning4j.

the class TestRecordReaders method testClassIndexOutsideOfRangeRRDSI.

@Test
public void testClassIndexOutsideOfRangeRRDSI() {
    Collection<Collection<Writable>> c = new ArrayList<>();
    c.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(0)));
    c.add(Arrays.<Writable>asList(new DoubleWritable(0.0), new IntWritable(2)));
    CollectionRecordReader crr = new CollectionRecordReader(c);
    RecordReaderDataSetIterator iter = new RecordReaderDataSetIterator(crr, 2, 1, 2);
    try {
        DataSet ds = iter.next();
        fail("Expected exception");
    } catch (DL4JException e) {
        System.out.println("testClassIndexOutsideOfRange(): " + e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DataSet(org.nd4j.linalg.dataset.api.DataSet) ArrayList(java.util.ArrayList) Collection(java.util.Collection) CollectionRecordReader(org.datavec.api.records.reader.impl.collection.CollectionRecordReader) DoubleWritable(org.datavec.api.writable.DoubleWritable) SequenceRecordReaderDataSetIterator(org.deeplearning4j.datasets.datavec.SequenceRecordReaderDataSetIterator) RecordReaderDataSetIterator(org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator) DL4JException(org.deeplearning4j.exception.DL4JException) IntWritable(org.datavec.api.writable.IntWritable) DL4JException(org.deeplearning4j.exception.DL4JException) Test(org.junit.Test)

Example 5 with DL4JException

use of org.deeplearning4j.exception.DL4JException in project deeplearning4j by deeplearning4j.

the class TestInvalidConfigurations method testCnnInvalidConfigPaddingStridesWidth.

@Test
public void testCnnInvalidConfigPaddingStridesWidth() {
    //Idea: some combination of padding/strides are invalid.
    int depthIn = 3;
    int hIn = 10;
    int wIn = 10;
    try {
        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().list().layer(0, new ConvolutionLayer.Builder().kernelSize(2, 3).stride(2, 2).padding(0, 0).nOut(5).build()).layer(1, new OutputLayer.Builder().nOut(10).build()).setInputType(InputType.convolutional(hIn, wIn, depthIn)).build();
    } catch (Exception e) {
        fail("Did not expect exception with default (truncate)");
    }
    try {
        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().convolutionMode(ConvolutionMode.Strict).list().layer(0, new ConvolutionLayer.Builder().kernelSize(2, 3).stride(2, 2).padding(0, 0).nOut(5).build()).layer(1, new OutputLayer.Builder().nOut(10).build()).setInputType(InputType.convolutional(hIn, wIn, depthIn)).build();
        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();
        fail("Expected exception");
    } catch (DL4JException e) {
        System.out.println("testCnnInvalidConfigPaddingStridesWidth(): " + e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) DL4JException(org.deeplearning4j.exception.DL4JException) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) DL4JException(org.deeplearning4j.exception.DL4JException) Test(org.junit.Test)

Aggregations

DL4JException (org.deeplearning4j.exception.DL4JException)25 Test (org.junit.Test)25 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)20 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)20 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)8 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 DoubleWritable (org.datavec.api.writable.DoubleWritable)3 IntWritable (org.datavec.api.writable.IntWritable)3 RecordReaderDataSetIterator (org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator)3 SequenceRecordReaderDataSetIterator (org.deeplearning4j.datasets.datavec.SequenceRecordReaderDataSetIterator)3 ConvolutionLayer (org.deeplearning4j.nn.conf.layers.ConvolutionLayer)3 INDArray (org.nd4j.linalg.api.ndarray.INDArray)3 DataSet (org.nd4j.linalg.dataset.api.DataSet)3 CollectionSequenceRecordReader (org.datavec.api.records.reader.impl.collection.CollectionSequenceRecordReader)2 Writable (org.datavec.api.writable.Writable)2 ConvolutionMode (org.deeplearning4j.nn.conf.ConvolutionMode)2 DataSetIterator (org.nd4j.linalg.dataset.api.iterator.DataSetIterator)2 CollectionRecordReader (org.datavec.api.records.reader.impl.collection.CollectionRecordReader)1 ComputationGraphConfiguration (org.deeplearning4j.nn.conf.ComputationGraphConfiguration)1