Java Code Examples for org.deeplearning4j.nn.conf.ConvolutionMode#Truncate

The following examples show how to use org.deeplearning4j.nn.conf.ConvolutionMode#Truncate . 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: ConvDataFormatTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDepthwiseConv2d() {
    try {
        for (boolean helpers : new boolean[]{false, true}) {
            for (ConvolutionMode cm : new ConvolutionMode[]{ConvolutionMode.Truncate, ConvolutionMode.Same}) {
                Nd4j.getRandom().setSeed(12345);
                Nd4j.getEnvironment().allowHelpers(helpers);
                String msg = helpers ? "With helpers (" + cm + ")" : "No helpers (" + cm + ")";
                System.out.println(" --- " + msg + " ---");

                INDArray inNCHW = Nd4j.rand(this.dataType, 2, 3, 12, 12);
                INDArray labels = TestUtils.randomOneHot(2, 10);

                TestCase tc = TestCase.builder()
                        .msg(msg)
                        .net1(getDepthwiseConv2dNet(CNN2DFormat.NCHW, true, cm))
                        .net2(getDepthwiseConv2dNet(CNN2DFormat.NCHW, false, cm))
                        .net3(getDepthwiseConv2dNet(CNN2DFormat.NHWC, true, cm))
                        .net4(getDepthwiseConv2dNet(CNN2DFormat.NHWC, false, cm))
                        .inNCHW(inNCHW)
                        .labelsNCHW(labels)
                        .labelsNHWC(labels)
                        .testLayerIdx(1)
                        .helpers(helpers)
                        .build();

                testHelper(tc);
            }
        }
    } finally {
        Nd4j.getEnvironment().allowHelpers(true);
    }
}
 
Example 2
Source File: ConvDataFormatTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeconv2d() {
    try {
        for (boolean helpers : new boolean[]{false, true}) {
            for (ConvolutionMode cm : new ConvolutionMode[]{ConvolutionMode.Truncate, ConvolutionMode.Same}) {
                Nd4j.getRandom().setSeed(12345);
                Nd4j.getEnvironment().allowHelpers(helpers);
                String msg = helpers ? "With helpers (" + cm + ")" : "No helpers (" + cm + ")";
                System.out.println(" --- " + msg + " ---");

                INDArray inNCHW = Nd4j.rand(this.dataType, 2, 3, 12, 12);
                INDArray labels = TestUtils.randomOneHot(2, 10);

                TestCase tc = TestCase.builder()
                        .msg(msg)
                        .net1(getDeconv2DNet2dNet(CNN2DFormat.NCHW, true, cm))
                        .net2(getDeconv2DNet2dNet(CNN2DFormat.NCHW, false, cm))
                        .net3(getDeconv2DNet2dNet(CNN2DFormat.NHWC, true, cm))
                        .net4(getDeconv2DNet2dNet(CNN2DFormat.NHWC, false, cm))
                        .inNCHW(inNCHW)
                        .labelsNCHW(labels)
                        .labelsNHWC(labels)
                        .testLayerIdx(1)
                        .helpers(helpers)
                        .build();

                testHelper(tc);
            }
        }
    } finally {
        Nd4j.getEnvironment().allowHelpers(true);
    }
}
 
Example 3
Source File: ConvDataFormatTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testConv2d() {
    try {
        for (boolean helpers : new boolean[]{false, true}) {
            for (ConvolutionMode cm : new ConvolutionMode[]{ConvolutionMode.Truncate, ConvolutionMode.Same}) {
                Nd4j.getRandom().setSeed(12345);
                Nd4j.getEnvironment().allowHelpers(helpers);
                String msg = helpers ? "With helpers (" + cm + ")" : "No helpers (" + cm + ")";
                System.out.println(" --- " + msg + " ---");

                INDArray inNCHW = Nd4j.rand(this.dataType, 2, 3, 12, 12);
                INDArray labels = TestUtils.randomOneHot(2, 10);

                TestCase tc = TestCase.builder()
                        .msg(msg)
                        .net1(getConv2dNet(CNN2DFormat.NCHW, true, cm))
                        .net2(getConv2dNet(CNN2DFormat.NCHW, false, cm))
                        .net3(getConv2dNet(CNN2DFormat.NHWC, true, cm))
                        .net4(getConv2dNet(CNN2DFormat.NHWC, false, cm))
                        .inNCHW(inNCHW)
                        .labelsNCHW(labels)
                        .labelsNHWC(labels)
                        .testLayerIdx(1)
                        .helpers(helpers)
                        .build();

                testHelper(tc);
            }
        }
    } finally {
        Nd4j.getEnvironment().allowHelpers(true);
    }
}
 
Example 4
Source File: TestConvolution.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCudnnDilation(){
    //Sanity check on dilated conv execution
    int[] k = new int[]{2,3,4,5};
    int[] d = new int[]{1,2,3,4};

    for( int[] inputSize : new int[][]{{10,1,28,28}, {3,3,224,224}}) {
        for (int i = 0; i < k.length; i++) {
            for(ConvolutionMode cm : new ConvolutionMode[]{ConvolutionMode.Same, ConvolutionMode.Truncate}) {

                MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                        .convolutionMode(ConvolutionMode.Same)
                        .list()
                        .layer(new ConvolutionLayer.Builder().kernelSize(k[i], k[i]).dilation(d[i], d[i]).nOut(3).build())
                        .layer(new SubsamplingLayer.Builder().kernelSize(k[i], k[i]).dilation(d[i], d[i]).build())
                        .layer(new OutputLayer.Builder().nOut(10).build())
                        .setInputType(InputType.convolutional(inputSize[3], inputSize[2], inputSize[1]))
                        .build();

                MultiLayerNetwork net = new MultiLayerNetwork(conf);
                net.init();

                INDArray in = Nd4j.create(inputSize);
                net.output(in);
            }
        }
    }
}
 
Example 5
Source File: KerasConvolutionUtils.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get convolution border mode from Keras layer configuration.
 *
 * @param layerConfig dictionary containing Keras layer configuration
 * @return Border mode of convolutional layers
 * @throws InvalidKerasConfigurationException Invalid Keras configuration
 */
public static ConvolutionMode getConvolutionModeFromConfig(Map<String, Object> layerConfig,
                                                           KerasLayerConfiguration conf)
        throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
    Map<String, Object> innerConfig = KerasLayerUtils.getInnerLayerConfigFromConfig(layerConfig, conf);
    if (!innerConfig.containsKey(conf.getLAYER_FIELD_BORDER_MODE()))
        throw new InvalidKerasConfigurationException("Could not determine convolution border mode: no "
                + conf.getLAYER_FIELD_BORDER_MODE() + " field found");
    String borderMode = (String) innerConfig.get(conf.getLAYER_FIELD_BORDER_MODE());
    ConvolutionMode convolutionMode;
    if (borderMode.equals(conf.getLAYER_BORDER_MODE_SAME())) {
        /* Keras relies upon the Theano and TensorFlow border mode definitions and operations:
         * TH: http://deeplearning.net/software/theano/library/tensor/nnet/conv.html#theano.tensor.nnet.conv.conv2d
         * TF: https://www.tensorflow.org/api_docs/python/nn/convolution#conv2d
         */
        convolutionMode = ConvolutionMode.Same;

    } else if (borderMode.equals(conf.getLAYER_BORDER_MODE_VALID()) ||
            borderMode.equals(conf.getLAYER_BORDER_MODE_FULL())) {
        convolutionMode = ConvolutionMode.Truncate;
    } else if(borderMode.equals(conf.getLAYER_BORDER_MODE_CAUSAL())) {
        convolutionMode = ConvolutionMode.Causal;
    } else {
        throw new UnsupportedKerasConfigurationException("Unsupported convolution border mode: " + borderMode);
    }
    return convolutionMode;
}
 
Example 6
Source File: ConvDataFormatTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSeparableConv2d() {
    try {
        for (boolean helpers : new boolean[]{false, true}) {
            for (ConvolutionMode cm : new ConvolutionMode[]{ConvolutionMode.Truncate, ConvolutionMode.Same}) {
                Nd4j.getRandom().setSeed(12345);
                Nd4j.getEnvironment().allowHelpers(helpers);
                String msg = helpers ? "With helpers (" + cm + ")" : "No helpers (" + cm + ")";
                System.out.println(" --- " + msg + " ---");

                INDArray inNCHW = Nd4j.rand(this.dataType, 2, 3, 12, 12);
                INDArray labels = TestUtils.randomOneHot(2, 10);

                TestCase tc = TestCase.builder()
                        .msg(msg)
                        .net1(getSeparableConv2dNet(CNN2DFormat.NCHW, true, cm))
                        .net2(getSeparableConv2dNet(CNN2DFormat.NCHW, false, cm))
                        .net3(getSeparableConv2dNet(CNN2DFormat.NHWC, true, cm))
                        .net4(getSeparableConv2dNet(CNN2DFormat.NHWC, false, cm))
                        .inNCHW(inNCHW)
                        .labelsNCHW(labels)
                        .labelsNHWC(labels)
                        .testLayerIdx(1)
                        .build();

                testHelper(tc);
            }
        }
    } finally {
        Nd4j.getEnvironment().allowHelpers(true);
    }
}
 
Example 7
Source File: PrimaryCapsules.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public Builder(int capsuleDimensions, int channels){
    this(capsuleDimensions, channels, new int[]{9, 9}, new int[]{2, 2}, new int[]{0, 0}, new int[]{1, 1}, ConvolutionMode.Truncate);
}
 
Example 8
Source File: CNN3DGradientCheckTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testCnn3DZeroPadding() {
        Nd4j.getRandom().setSeed(42);

        int depth = 4;
        int height = 4;
        int width = 4;


        int[] minibatchSizes = {3};
        int convNIn = 2;
        int convNOut1 = 3;
        int convNOut2 = 4;
        int denseNOut = 5;
        int finalNOut = 42;


        int[] kernel = {2, 2, 2};
        int[] zeroPadding = {1, 1, 2, 2, 3, 3};

        Activation[] activations = {Activation.SIGMOID};

        ConvolutionMode[] modes = {ConvolutionMode.Truncate, ConvolutionMode.Same};

        for (Activation afn : activations) {
            for (int miniBatchSize : minibatchSizes) {
                for (ConvolutionMode mode : modes) {

                    int outDepth = mode == ConvolutionMode.Same ?
                            depth : (depth - kernel[0]) + 1;
                    int outHeight = mode == ConvolutionMode.Same ?
                            height : (height - kernel[1]) + 1;
                    int outWidth = mode == ConvolutionMode.Same ?
                            width : (width - kernel[2]) + 1;

                    outDepth += zeroPadding[0] + zeroPadding[1];
                    outHeight += zeroPadding[2] + zeroPadding[3];
                    outWidth += zeroPadding[4] + zeroPadding[5];

                    INDArray input = Nd4j.rand(new int[]{miniBatchSize, convNIn, depth, height, width});
                    INDArray labels = Nd4j.zeros(miniBatchSize, finalNOut);
                    for (int i = 0; i < miniBatchSize; i++) {
                        labels.putScalar(new int[]{i, i % finalNOut}, 1.0);
                    }

                    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                            .dataType(DataType.DOUBLE)
                            .updater(new NoOp()).weightInit(WeightInit.LECUN_NORMAL)
                            .dist(new NormalDistribution(0, 1))
                            .list()
                            .layer(0, new Convolution3D.Builder().activation(afn).kernelSize(kernel)
                                    .nIn(convNIn).nOut(convNOut1).hasBias(false)
                                    .convolutionMode(mode).dataFormat(Convolution3D.DataFormat.NCDHW)
                                    .build())
                            .layer(1, new Convolution3D.Builder().activation(afn).kernelSize(1, 1, 1)
                                    .nIn(convNOut1).nOut(convNOut2).hasBias(false)
                                    .convolutionMode(mode).dataFormat(Convolution3D.DataFormat.NCDHW)
                                    .build())
                            .layer(2, new ZeroPadding3DLayer.Builder(zeroPadding).build())
                            .layer(3, new DenseLayer.Builder().nOut(denseNOut).build())
                            .layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                                    .activation(Activation.SOFTMAX).nOut(finalNOut).build())
                            .inputPreProcessor(3,
                                    new Cnn3DToFeedForwardPreProcessor(outDepth, outHeight, outWidth,
                                            convNOut2, true))
                            .setInputType(InputType.convolutional3D(depth, height, width, convNIn)).build();

                    String json = conf.toJson();
                    MultiLayerConfiguration c2 = MultiLayerConfiguration.fromJson(json);
                    assertEquals(conf, c2);

                    MultiLayerNetwork net = new MultiLayerNetwork(conf);
                    net.init();

                    String msg = "Minibatch size = " + miniBatchSize + ", activationFn=" + afn
                            + ", kernel = " + Arrays.toString(kernel) + ", mode = " + mode.toString()
                            + ", input depth " + depth + ", input height " + height
                            + ", input width " + width;

                    if (PRINT_RESULTS) {
                        log.info(msg);
//                        for (int j = 0; j < net.getnLayers(); j++) {
//                            log.info("Layer " + j + " # params: " + net.getLayer(j).numParams());
//                        }
                    }

                    boolean gradOK = GradientCheckUtil.checkGradients(new GradientCheckUtil.MLNConfig().net(net).input(input)
                            .labels(labels).subset(true).maxPerParam(512));

                    assertTrue(msg, gradOK);

                    TestUtils.testModelSerialization(net);
                }

            }
        }
    }
 
Example 9
Source File: CNN3DGradientCheckTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testCnn3DPooling() {
    Nd4j.getRandom().setSeed(42);

    int depth = 4;
    int height = 4;
    int width = 4;


    int[] minibatchSizes = {3};
    int convNIn = 2;
    int convNOut = 4;
    int denseNOut = 5;
    int finalNOut = 42;

    int[] kernel = {2, 2, 2};

    Activation[] activations = {Activation.SIGMOID};

    Subsampling3DLayer.PoolingType[] poolModes = {Subsampling3DLayer.PoolingType.AVG};

    ConvolutionMode[] modes = {ConvolutionMode.Truncate};

    for (Activation afn : activations) {
        for (int miniBatchSize : minibatchSizes) {
            for (Subsampling3DLayer.PoolingType pool : poolModes) {
                for (ConvolutionMode mode : modes) {
                    for (Convolution3D.DataFormat df : Convolution3D.DataFormat.values()) {

                        int outDepth = depth / kernel[0];
                        int outHeight = height / kernel[1];
                        int outWidth = width / kernel[2];

                        INDArray input = Nd4j.rand(
                                df == Convolution3D.DataFormat.NCDHW ? new int[]{miniBatchSize, convNIn, depth, height, width}
                                        : new int[]{miniBatchSize, depth, height, width, convNIn});
                        INDArray labels = Nd4j.zeros(miniBatchSize, finalNOut);
                        for (int i = 0; i < miniBatchSize; i++) {
                            labels.putScalar(new int[]{i, i % finalNOut}, 1.0);
                        }

                        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                                .dataType(DataType.DOUBLE)
                                .updater(new NoOp())
                                .weightInit(WeightInit.XAVIER)
                                .dist(new NormalDistribution(0, 1))
                                .list()
                                .layer(0, new Convolution3D.Builder().activation(afn).kernelSize(1, 1, 1)
                                        .nIn(convNIn).nOut(convNOut).hasBias(false)
                                        .convolutionMode(mode).dataFormat(df)
                                        .build())
                                .layer(1, new Subsampling3DLayer.Builder(kernel)
                                        .poolingType(pool).convolutionMode(mode).dataFormat(df).build())
                                .layer(2, new DenseLayer.Builder().nOut(denseNOut).build())
                                .layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                                        .activation(Activation.SOFTMAX).nOut(finalNOut).build())
                                .inputPreProcessor(2,
                                        new Cnn3DToFeedForwardPreProcessor(outDepth, outHeight, outWidth,convNOut, df))
                                .setInputType(InputType.convolutional3D(df, depth, height, width, convNIn)).build();

                        String json = conf.toJson();
                        MultiLayerConfiguration c2 = MultiLayerConfiguration.fromJson(json);
                        assertEquals(conf, c2);

                        MultiLayerNetwork net = new MultiLayerNetwork(conf);
                        net.init();

                        String msg = "Minibatch size = " + miniBatchSize + ", activationFn=" + afn
                                + ", kernel = " + Arrays.toString(kernel) + ", mode = " + mode.toString()
                                + ", input depth " + depth + ", input height " + height
                                + ", input width " + width + ", dataFormat=" + df;

                        if (PRINT_RESULTS) {
                            log.info(msg);
                        }

                        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS,
                                DEFAULT_MAX_REL_ERROR, DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS,
                                RETURN_ON_FIRST_FAILURE, input, labels);

                        assertTrue(msg, gradOK);

                        TestUtils.testModelSerialization(net);
                    }
                }
            }
        }
    }
}
 
Example 10
Source File: CNN3DGradientCheckTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testCnn3DUpsampling() {
        Nd4j.getRandom().setSeed(42);

        int depth = 2;
        int height = 2;
        int width = 2;


        int[] minibatchSizes = {3};
        int convNIn = 2;
        int convNOut = 4;
        int denseNOut = 5;
        int finalNOut = 42;


        int[] upsamplingSize = {2, 2, 2};

        Activation[] activations = {Activation.SIGMOID};


        ConvolutionMode[] modes = {ConvolutionMode.Truncate};

        for (Activation afn : activations) {
            for (int miniBatchSize : minibatchSizes) {
                for (ConvolutionMode mode : modes) {
                    for(Convolution3D.DataFormat df : Convolution3D.DataFormat.values()) {

                        int outDepth = depth * upsamplingSize[0];
                        int outHeight = height * upsamplingSize[1];
                        int outWidth = width * upsamplingSize[2];

                        INDArray input = df == Convolution3D.DataFormat.NCDHW ? Nd4j.rand(miniBatchSize, convNIn, depth, height, width) : Nd4j.rand(miniBatchSize, depth, height, width, convNIn);
                        INDArray labels = Nd4j.zeros(miniBatchSize, finalNOut);
                        for (int i = 0; i < miniBatchSize; i++) {
                            labels.putScalar(new int[]{i, i % finalNOut}, 1.0);
                        }

                        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                                .dataType(DataType.DOUBLE)
                                .updater(new NoOp()).weightInit(WeightInit.LECUN_NORMAL)
                                .dist(new NormalDistribution(0, 1))
                                .seed(12345)
                                .list()
                                .layer(0, new Convolution3D.Builder().activation(afn).kernelSize(1, 1, 1)
                                        .nIn(convNIn).nOut(convNOut).hasBias(false)
                                        .convolutionMode(mode).dataFormat(df)
                                        .build())
                                .layer(1, new Upsampling3D.Builder(upsamplingSize[0]).dataFormat(df).build())
                                .layer(2, new DenseLayer.Builder().nOut(denseNOut).build())
                                .layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                                        .activation(Activation.SOFTMAX).nOut(finalNOut).build())
                                .inputPreProcessor(2,
                                        new Cnn3DToFeedForwardPreProcessor(outDepth, outHeight, outWidth,
                                                convNOut, true))
                                .setInputType(InputType.convolutional3D(df, depth, height, width, convNIn)).build();

                        String json = conf.toJson();
                        MultiLayerConfiguration c2 = MultiLayerConfiguration.fromJson(json);
                        assertEquals(conf, c2);

                        MultiLayerNetwork net = new MultiLayerNetwork(conf);
                        net.init();

                        String msg = "Minibatch size = " + miniBatchSize + ", activationFn=" + afn
                                + ", kernel = " + Arrays.toString(upsamplingSize) + ", mode = " + mode.toString()
                                + ", input depth " + depth + ", input height " + height
                                + ", input width " + width;

                        if (PRINT_RESULTS) {
                            log.info(msg);
//                            for (int j = 0; j < net.getnLayers(); j++) {
//                                log.info("Layer " + j + " # params: " + net.getLayer(j).numParams());
//                            }
                        }

                        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS,
                                DEFAULT_MAX_REL_ERROR, DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS,
                                RETURN_ON_FIRST_FAILURE, input, labels);

                        assertTrue(msg, gradOK);

                        TestUtils.testModelSerialization(net);
                    }
                }
            }
        }
    }
 
Example 11
Source File: CNN3DGradientCheckTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeconv3d() {
    Nd4j.getRandom().setSeed(12345);
    // Note: we checked this with a variety of parameters, but it takes a lot of time.
    int[] depths = {8, 8, 9};
    int[] heights = {8, 9, 9};
    int[] widths = {8, 8, 9};


    int[][] kernels = {{2, 2, 2}, {3, 3, 3}, {2, 3, 2}};
    int[][] strides = {{1, 1, 1}, {1, 1, 1}, {2, 2, 2}};

    Activation[] activations = {Activation.SIGMOID, Activation.TANH, Activation.IDENTITY};

    ConvolutionMode[] modes = {ConvolutionMode.Truncate, ConvolutionMode.Same, ConvolutionMode.Same};
    int[] mbs = {1, 3, 2};
    Convolution3D.DataFormat[] dataFormats = new Convolution3D.DataFormat[]{Convolution3D.DataFormat.NCDHW, Convolution3D.DataFormat.NDHWC, Convolution3D.DataFormat.NCDHW};

    int convNIn = 2;
    int finalNOut = 2;
    int[] deconvOut = {2, 3, 4};

    for (int i = 0; i < activations.length; i++) {
        Activation afn = activations[i];
        int miniBatchSize = mbs[i];
        int depth = depths[i];
        int height = heights[i];
        int width = widths[i];
        ConvolutionMode mode = modes[i];
        int[] kernel = kernels[i];
        int[] stride = strides[i];
        Convolution3D.DataFormat df = dataFormats[i];
        int dOut = deconvOut[i];

        INDArray input;
        if (df == Convolution3D.DataFormat.NDHWC) {
            input = Nd4j.rand(new int[]{miniBatchSize, depth, height, width, convNIn});
        } else {
            input = Nd4j.rand(new int[]{miniBatchSize, convNIn, depth, height, width});
        }
        INDArray labels = Nd4j.zeros(miniBatchSize, finalNOut);
        for (int j = 0; j < miniBatchSize; j++) {
            labels.putScalar(new int[]{j, j % finalNOut}, 1.0);
        }

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .dataType(DataType.DOUBLE)
                .updater(new NoOp())
                .weightInit(new NormalDistribution(0, 0.1))
                .list()
                .layer(0, new Convolution3D.Builder().activation(afn).kernelSize(kernel)
                        .stride(stride).nIn(convNIn).nOut(dOut).hasBias(false)
                        .convolutionMode(mode).dataFormat(df)
                        .build())
                .layer(1, new Deconvolution3D.Builder().activation(afn).kernelSize(kernel)
                        .stride(stride).nOut(dOut).hasBias(false)
                        .convolutionMode(mode).dataFormat(df)
                        .build())
                .layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                        .activation(Activation.SOFTMAX).nOut(finalNOut).build())
                .setInputType(InputType.convolutional3D(df, depth, height, width, convNIn)).build();

        String json = conf.toJson();
        MultiLayerConfiguration c2 = MultiLayerConfiguration.fromJson(json);
        assertEquals(conf, c2);

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        net.init();

        String msg = "DataFormat = " + df + ", minibatch size = " + miniBatchSize + ", activationFn=" + afn
                + ", kernel = " + Arrays.toString(kernel) + ", stride = "
                + Arrays.toString(stride) + ", mode = " + mode.toString()
                + ", input depth " + depth + ", input height " + height
                + ", input width " + width;

        if (PRINT_RESULTS) {
            log.info(msg);
        }

        boolean gradOK = GradientCheckUtil.checkGradients(new GradientCheckUtil.MLNConfig().net(net).input(input)
                .labels(labels).subset(true).maxPerParam(64));

        assertTrue(msg, gradOK);

        TestUtils.testModelSerialization(net);
    }
}
 
Example 12
Source File: ValidateMKLDNN.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void validateConvSubsampling() throws Exception {
    //Only run test if using nd4j-native backend
    assumeTrue(Nd4j.getBackend().getClass().getName().toLowerCase().contains("native"));
    Nd4j.setDefaultDataTypes(DataType.FLOAT, DataType.FLOAT);
    Nd4j.getRandom().setSeed(12345);

    int[] inputSize = {-1, 3, 16, 16};

    for(int minibatch : new int[]{1,3}) {
        for (ConvolutionMode cm : new ConvolutionMode[]{ConvolutionMode.Same, ConvolutionMode.Truncate}) {
            for (int[] kernel : new int[][]{{2, 2}, {2, 3}}) {
                for (int[] stride : new int[][]{{1, 1}, {2, 2}}) {
                    for (PoolingType pt : new PoolingType[]{PoolingType.MAX, PoolingType.AVG}) {

                        inputSize[0] = minibatch;
                        INDArray f = Nd4j.rand(DataType.FLOAT, inputSize);
                        INDArray l = TestUtils.randomOneHot(minibatch, 10).castTo(DataType.FLOAT);

                        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                                .updater(new Adam(0.01))
                                .convolutionMode(cm)
                                .seed(12345)
                                .list()
                                .layer(new ConvolutionLayer.Builder().activation(Activation.TANH)
                                        .kernelSize(kernel)
                                        .stride(stride)
                                        .padding(0, 0)
                                        .nOut(3)
                                        .build())
                                .layer(new SubsamplingLayer.Builder()
                                        .poolingType(pt)
                                        .kernelSize(kernel)
                                        .stride(stride)
                                        .padding(0, 0)
                                        .build())
                                .layer(new ConvolutionLayer.Builder().activation(Activation.TANH)
                                        .kernelSize(kernel)
                                        .stride(stride)
                                        .padding(0, 0)
                                        .nOut(3)
                                        .build())
                                .layer(new OutputLayer.Builder().nOut(10).activation(Activation.SOFTMAX).lossFunction(LossFunctions.LossFunction.MCXENT).build())
                                .setInputType(InputType.convolutional(inputSize[2], inputSize[3], inputSize[1]))
                                .build();

                        MultiLayerNetwork netWith = new MultiLayerNetwork(conf.clone());
                        netWith.init();

                        MultiLayerNetwork netWithout = new MultiLayerNetwork(conf.clone());
                        netWithout.init();

                        String name = pt + ", mb=" + minibatch + ", cm=" + cm + ", kernel=" + Arrays.toString(kernel) + ", stride=" + Arrays.toString(stride);
                        LayerHelperValidationUtil.TestCase tc = LayerHelperValidationUtil.TestCase.builder()
                                .testName(name)
                                .allowHelpersForClasses(Arrays.<Class<?>>asList(org.deeplearning4j.nn.layers.convolution.subsampling.SubsamplingLayer.class,
                                        org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.class))
                                .testForward(true)
                                .testScore(true)
                                .testBackward(true)
                                .testTraining(true)
                                .features(f)
                                .labels(l)
                                .data(new SingletonDataSetIterator(new DataSet(f, l)))
                                .build();

                        System.out.println("Starting test: " + name);
                        LayerHelperValidationUtil.validateMLN(netWith, tc);
                    }
                }
            }
        }
    }
}
 
Example 13
Source File: ValidateMKLDNN.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void validateBatchNorm() {
    //Only run test if using nd4j-native backend
    assumeTrue(Nd4j.getBackend().getClass().getName().toLowerCase().contains("native"));
    Nd4j.setDefaultDataTypes(DataType.FLOAT, DataType.FLOAT);
    Nd4j.getRandom().setSeed(12345);

    int[] inputSize = {-1, 3, 16, 16};
    int[] stride = {1, 1};
    int[] kernel = {2, 2};
    ConvolutionMode cm = ConvolutionMode.Truncate;

    for (int minibatch : new int[]{1, 3}) {
        for (boolean b : new boolean[]{true, false}) {

            inputSize[0] = minibatch;
            INDArray f = Nd4j.rand(Nd4j.defaultFloatingPointType(), inputSize);
            INDArray l = TestUtils.randomOneHot(minibatch, 10);

            MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                    .dataType(DataType.FLOAT)
                    .updater(new Adam(0.01))
                    .convolutionMode(cm)
                    .seed(12345)
                    .list()
                    .layer(new ConvolutionLayer.Builder().activation(Activation.TANH)
                            .kernelSize(kernel)
                            .stride(stride)
                            .padding(0, 0)
                            .nOut(3)
                            .build())
                    .layer(new BatchNormalization.Builder().useLogStd(b).helperAllowFallback(false)/*.eps(0)*/.build())
                    .layer(new ConvolutionLayer.Builder().activation(Activation.TANH)
                            .kernelSize(kernel)
                            .stride(stride)
                            .padding(0, 0)
                            .nOut(3)
                            .build())
                    .layer(new OutputLayer.Builder().nOut(10).activation(Activation.SOFTMAX).lossFunction(LossFunctions.LossFunction.MCXENT).build())
                    .setInputType(InputType.convolutional(inputSize[2], inputSize[3], inputSize[1]))
                    .build();

            MultiLayerNetwork netWith = new MultiLayerNetwork(conf.clone());
            netWith.init();

            MultiLayerNetwork netWithout = new MultiLayerNetwork(conf.clone());
            netWithout.init();

            LayerHelperValidationUtil.TestCase tc = LayerHelperValidationUtil.TestCase.builder()
                    .allowHelpersForClasses(Collections.<Class<?>>singletonList(org.deeplearning4j.nn.layers.normalization.BatchNormalization.class))
                    .testForward(true)
                    .testScore(true)
                    .testBackward(true)
                    .testTraining(true)
                    .features(f)
                    .labels(l)
                    .data(new SingletonDataSetIterator(new DataSet(f, l)))
                    .maxRelError(1e-4)
                    .build();

            LayerHelperValidationUtil.validateMLN(netWith, tc);
        }
    }
}
 
Example 14
Source File: ValidateMKLDNN.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test @Ignore   //https://github.com/deeplearning4j/deeplearning4j/issues/7272
public void validateLRN() {

    //Only run test if using nd4j-native backend
    assumeTrue(Nd4j.getBackend().getClass().getName().toLowerCase().contains("native"));
    Nd4j.setDefaultDataTypes(DataType.FLOAT, DataType.FLOAT);
    Nd4j.getRandom().setSeed(12345);

    int[] inputSize = {-1, 3, 16, 16};
    int[] stride = {1, 1};
    int[] kernel = {2, 2};
    ConvolutionMode cm = ConvolutionMode.Truncate;

    double[] a = new double[]{1e-4, 1e-4, 1e-3, 1e-3};
    double[] b = new double[]{0.75, 0.9, 0.75, 0.75};
    double[] n = new double[]{5, 3, 3, 4};
    double[] k = new double[]{2, 2.5, 2.75, 2};

    for (int minibatch : new int[]{1, 3}) {
        for( int i=0; i<a.length; i++ ) {
            System.out.println("+++++ MINIBATCH = " + minibatch + ", TEST=" + i + " +++++");


            inputSize[0] = minibatch;
            INDArray f = Nd4j.rand(Nd4j.defaultFloatingPointType(), inputSize);
            INDArray l = TestUtils.randomOneHot(minibatch, 10).castTo(DataType.FLOAT);

            MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                    .updater(new Adam(0.01))
                    .convolutionMode(cm)
                    .weightInit(new NormalDistribution(0,1))
                    .seed(12345)
                    .list()
                    .layer(new ConvolutionLayer.Builder().activation(Activation.TANH)
                            .kernelSize(kernel)
                            .stride(stride)
                            .padding(0, 0)
                            .nOut(3)
                            .build())
                    .layer(new LocalResponseNormalization.Builder()
                            .alpha(a[i])
                            .beta(b[i])
                            .n(n[i])
                            .k(k[i])
                            .cudnnAllowFallback(false).build())
                    .layer(new OutputLayer.Builder().nOut(10).activation(Activation.SOFTMAX).lossFunction(LossFunctions.LossFunction.MCXENT).build())
                    .setInputType(InputType.convolutional(inputSize[2], inputSize[3], inputSize[1]))
                    .build();

            MultiLayerNetwork netWith = new MultiLayerNetwork(conf.clone());
            netWith.init();

            MultiLayerNetwork netWithout = new MultiLayerNetwork(conf.clone());
            netWithout.init();

            LayerHelperValidationUtil.TestCase tc = LayerHelperValidationUtil.TestCase.builder()
                    .allowHelpersForClasses(Collections.<Class<?>>singletonList(org.deeplearning4j.nn.layers.normalization.LocalResponseNormalization.class))
                    .testForward(true)
                    .testScore(true)
                    .testBackward(true)
                    .testTraining(true)
                    .features(f)
                    .labels(l)
                    .data(new SingletonDataSetIterator(new DataSet(f, l)))
                    //Very infrequent minor differences - as far as I can tell, just numerical precision issues...
                    .minAbsError(1e-3)
                    .maxRelError(1e-2)
                    .build();

            LayerHelperValidationUtil.validateMLN(netWith, tc);

            System.out.println("/////////////////////////////////////////////////////////////////////////////");
        }
    }
}
 
Example 15
Source File: TestSameDiffConv.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSameDiffConvGradient() {
    int imgH = 8;
    int imgW = 8;
    int nIn = 3;
    int nOut = 4;
    int[] kernel = {2, 2};
    int[] strides = {1, 1};
    int[] dilation = {1, 1};

    int count = 0;

    //Note: to avoid the exporential number of tests here, we'll randomly run every Nth test only.
    //With n=1, m=3 this is 1 out of every 3 tests (on average)
    Random r = new Random(12345);
    int n = 1;
    int m = 5;
    for(boolean workspaces : new boolean[]{false, true}) {
        for (int minibatch : new int[]{5, 1}) {
            for (boolean hasBias : new boolean[]{true, false}) {
                for (ConvolutionMode cm : new ConvolutionMode[]{ConvolutionMode.Truncate, ConvolutionMode.Same}) {
                    int i = r.nextInt(m);
                    if (i >= n) {
                        //Example: n=2, m=3... skip on i=2, run test on i=0, i=1
                        continue;
                    }

                    String msg = "Test " + (count++) + " - minibatch=" + minibatch + ", ConvolutionMode=" + cm + ", hasBias=" + hasBias;

                    int outH = cm == ConvolutionMode.Same ? imgH : (imgH-2);
                    int outW = cm == ConvolutionMode.Same ? imgW : (imgW-2);

                    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                            .dataType(DataType.DOUBLE)
                            .seed(12345)
                            .updater(new NoOp())
                            .trainingWorkspaceMode(workspaces ? WorkspaceMode.ENABLED : WorkspaceMode.NONE)
                            .inferenceWorkspaceMode(workspaces ? WorkspaceMode.ENABLED : WorkspaceMode.NONE)
                            .list()
                            .layer(new SameDiffConv.Builder()
                                    .weightInit(WeightInit.XAVIER)
                                    .nIn(nIn)
                                    .nOut(nOut)
                                    .kernelSize(kernel)
                                    .stride(strides)
                                    .dilation(dilation)
                                    .convolutionMode(cm)
                                    .activation(Activation.TANH)
                                    .hasBias(hasBias)
                                    .build())
                            .layer(new SameDiffConv.Builder()
                                    .weightInit(WeightInit.XAVIER)
                                    .nIn(nOut)
                                    .nOut(nOut)
                                    .kernelSize(kernel)
                                    .stride(strides)
                                    .dilation(dilation)
                                    .convolutionMode(cm)
                                    .activation(Activation.SIGMOID)
                                    .hasBias(hasBias)
                                    .build())
                            .layer(new OutputLayer.Builder().activation(Activation.SOFTMAX)
                                    .lossFunction(LossFunctions.LossFunction.MCXENT)
                                    .nIn(nOut * outH * outW)
                                    .nOut(nOut).build())
                            .inputPreProcessor(2, new CnnToFeedForwardPreProcessor(outH, outW, nOut))
                            .build();

                    MultiLayerNetwork net = new MultiLayerNetwork(conf);
                    net.init();

                    INDArray f = Nd4j.rand(new int[]{minibatch, nIn, imgH, imgW});
                    INDArray l = TestUtils.randomOneHot(minibatch, nOut);

                    log.info("Starting: " + msg);
                    boolean gradOK = GradientCheckUtil.checkGradients(new GradientCheckUtil.MLNConfig().net(net).input(f)
                            .labels(l).subset(true).maxPerParam(50));

                    assertTrue(msg, gradOK);

                    TestUtils.testModelSerialization(net);

                    //Sanity check on different minibatch sizes:
                    INDArray newIn = Nd4j.vstack(f, f);
                    net.output(newIn);
                }
            }
        }
    }
}
 
Example 16
Source File: TestConvolutionModes.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testGlobalLocalConfig() {
    for (ConvolutionMode cm : new ConvolutionMode[] {ConvolutionMode.Strict, ConvolutionMode.Truncate}) {
        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER)
                        .convolutionMode(cm).list()
                        .layer(0, new ConvolutionLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0)
                                        .nIn(3).nOut(
                                                        3)
                                        .build())
                        .layer(1, new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Strict)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0)
                                        .nIn(3).nOut(
                                                        3)
                                        .build())
                        .layer(2, new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Truncate)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0)
                                        .nIn(3).nOut(
                                                        3)
                                        .build())
                        .layer(3, new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Same)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build())
                        .layer(4, new SubsamplingLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0)
                                        .build())
                        .layer(5, new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Strict)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0).build())
                        .layer(6, new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Truncate)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0).build())
                        .layer(7, new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Same)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0).build())
                        .layer(8, new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).nOut(3)
                                .activation(Activation.SOFTMAX).build())
                        .build();

        assertEquals(cm, ((ConvolutionLayer) conf.getConf(0).getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Strict, ((ConvolutionLayer) conf.getConf(1).getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Truncate,
                        ((ConvolutionLayer) conf.getConf(2).getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Same, ((ConvolutionLayer) conf.getConf(3).getLayer()).getConvolutionMode());

        assertEquals(cm, ((SubsamplingLayer) conf.getConf(4).getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Strict, ((SubsamplingLayer) conf.getConf(5).getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Truncate,
                        ((SubsamplingLayer) conf.getConf(6).getLayer()).getConvolutionMode());
        assertEquals(ConvolutionMode.Same, ((SubsamplingLayer) conf.getConf(7).getLayer()).getConvolutionMode());
    }
}
 
Example 17
Source File: TestConvolutionModes.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testGlobalLocalConfigCompGraph() {
    for (ConvolutionMode cm : new ConvolutionMode[] {ConvolutionMode.Strict, ConvolutionMode.Truncate,
                    ConvolutionMode.Same}) {
        ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER)
                        .convolutionMode(cm).graphBuilder().addInputs("in")
                        .addLayer("0", new ConvolutionLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0)
                                        .nIn(3).nOut(
                                                        3)
                                        .build(), "in")
                        .addLayer("1", new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Strict)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0)
                                        .nIn(3).nOut(
                                                        3)
                                        .build(), "0")
                        .addLayer("2", new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Truncate)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0)
                                        .nIn(3).nOut(
                                                        3)
                                        .build(), "1")
                        .addLayer("3", new ConvolutionLayer.Builder().convolutionMode(ConvolutionMode.Same)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0).nIn(3).nOut(3).build(), "2")
                        .addLayer("4", new SubsamplingLayer.Builder().kernelSize(3, 3).stride(3, 3).padding(0, 0)
                                        .build(), "3")
                        .addLayer("5", new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Strict)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "4")
                        .addLayer("6", new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Truncate)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "5")
                        .addLayer("7", new SubsamplingLayer.Builder().convolutionMode(ConvolutionMode.Same)
                                        .kernelSize(3, 3).stride(3, 3).padding(0, 0).build(), "6")
                        .addLayer("8", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT)
                                .activation(Activation.SOFTMAX).nOut(3).build(), "7")
                        .setOutputs("8").build();

        assertEquals(cm, ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("0")).getLayerConf().getLayer())
                        .getConvolutionMode());
        assertEquals(ConvolutionMode.Strict,
                        ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("1")).getLayerConf().getLayer())
                                        .getConvolutionMode());
        assertEquals(ConvolutionMode.Truncate,
                        ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("2")).getLayerConf().getLayer())
                                        .getConvolutionMode());
        assertEquals(ConvolutionMode.Same,
                        ((ConvolutionLayer) ((LayerVertex) conf.getVertices().get("3")).getLayerConf().getLayer())
                                        .getConvolutionMode());

        assertEquals(cm, ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("4")).getLayerConf().getLayer())
                        .getConvolutionMode());
        assertEquals(ConvolutionMode.Strict,
                        ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("5")).getLayerConf().getLayer())
                                        .getConvolutionMode());
        assertEquals(ConvolutionMode.Truncate,
                        ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("6")).getLayerConf().getLayer())
                                        .getConvolutionMode());
        assertEquals(ConvolutionMode.Same,
                        ((SubsamplingLayer) ((LayerVertex) conf.getVertices().get("7")).getLayerConf().getLayer())
                                        .getConvolutionMode());
    }
}
 
Example 18
Source File: PrimaryCapsules.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public Builder(int capsuleDimensions, int channels,
        int[] kernelSize, int[] stride){
    this(capsuleDimensions, channels, kernelSize, stride, new int[]{0, 0}, new int[]{1, 1}, ConvolutionMode.Truncate);
}
 
Example 19
Source File: PrimaryCapsules.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public Builder(int capsuleDimensions, int channels,
        int[] kernelSize, int[] stride, int[] padding){
    this(capsuleDimensions, channels, kernelSize, stride, padding, new int[]{1, 1}, ConvolutionMode.Truncate);
}
 
Example 20
Source File: PrimaryCapsules.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public Builder(int capsuleDimensions, int channels,
        int[] kernelSize, int[] stride, int[] padding, int[] dilation){
    this(capsuleDimensions, channels, kernelSize, stride, padding, dilation, ConvolutionMode.Truncate);
}