Java Code Examples for org.deeplearning4j.nn.conf.inputs.InputType#convolutional()

The following examples show how to use org.deeplearning4j.nn.conf.inputs.InputType#convolutional() . 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: Cropping2D.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    int[] hwd = ConvolutionUtils.getHWDFromInputType(inputType);
    int outH = hwd[0] - cropping[0] - cropping[1];
    int outW = hwd[1] - cropping[2] - cropping[3];

    InputType.InputTypeConvolutional c = (InputType.InputTypeConvolutional)inputType;

    return InputType.convolutional(outH, outW, hwd[2], c.getFormat());
}
 
Example 2
Source File: PrimaryCapsulesTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testInputType(){
    PrimaryCapsules layer = new PrimaryCapsules.Builder(8, 8)
            .kernelSize(7, 7)
            .stride(2, 2)
            .build();
    InputType in1 = InputType.convolutional(7, 7, 16);


    layer.setNIn(in1, true);

    assertEquals(8, layer.getCapsules());
    assertEquals(8, layer.getCapsuleDimensions());
}
 
Example 3
Source File: PrimaryCapsulesTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testOutputType(){
    PrimaryCapsules layer = new PrimaryCapsules.Builder(8, 8)
            .kernelSize(7, 7)
            .stride(2, 2)
            .build();


    InputType in1 = InputType.convolutional(7, 7, 16);
    assertEquals(InputType.recurrent(8, 8), layer.getOutputType(0, in1));

}
 
Example 4
Source File: DropoutLayerTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testInputTypes() {
    DropoutLayer config = new DropoutLayer.Builder(0.5).build();

    InputType in1 = InputType.feedForward(20);
    InputType in2 = InputType.convolutional(28, 28, 1);

    assertEquals(in1, config.getOutputType(0, in1));
    assertEquals(in2, config.getOutputType(0, in2));
    assertNull(config.getPreProcessorForInputType(in1));
    assertNull(config.getPreProcessorForInputType(in2));
}
 
Example 5
Source File: ActivationLayerTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testInputTypes() {
    org.deeplearning4j.nn.conf.layers.ActivationLayer l =
                    new org.deeplearning4j.nn.conf.layers.ActivationLayer.Builder().activation(Activation.RELU)
                                    .build();


    InputType in1 = InputType.feedForward(20);
    InputType in2 = InputType.convolutional(28, 28, 1);

    assertEquals(in1, l.getOutputType(0, in1));
    assertEquals(in2, l.getOutputType(0, in2));
    assertNull(l.getPreProcessorForInputType(in1));
    assertNull(l.getPreProcessorForInputType(in2));
}
 
Example 6
Source File: ReshapeVertex.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException {
    //Infer output shape from specified shape:
    switch (newShape.length) {
        case 2:
            return InputType.feedForward(newShape[1]);
        case 3:
            return InputType.recurrent(newShape[1]);
        case 4:
            return InputType.convolutional(newShape[2], newShape[3], newShape[1]); //[mb,d,h,w] for activations
        default:
            throw new UnsupportedOperationException(
                            "Cannot infer input type for reshape array " + Arrays.toString(newShape));
    }
}
 
Example 7
Source File: SpaceToBatchLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.CNN) {
        throw new IllegalStateException("Invalid input for Subsampling layer (layer name=\"" + getLayerName()
                        + "\"): Expected CNN input, got " + inputType);
    }
    InputType.InputTypeConvolutional i = (InputType.InputTypeConvolutional) inputType;
    return InputType.convolutional((i.getHeight() + padding[0][0] + padding[0][1]) / blocks[0],
                    (i.getWidth() + padding[1][0] + padding[1][1]) / blocks[1], i.getChannels(), i.getFormat());
}
 
Example 8
Source File: Upsampling2D.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.CNN) {
        throw new IllegalStateException("Invalid input for Upsampling 2D layer (layer name=\"" + getLayerName()
                        + "\"): Expected CNN input, got " + inputType);
    }
    InputType.InputTypeConvolutional i = (InputType.InputTypeConvolutional) inputType;
    val inHeight = i.getHeight();
    val inWidth = i.getWidth();
    val inDepth = i.getChannels();

    return InputType.convolutional(size[0] * inHeight, size[1] * inWidth, inDepth, i.getFormat());
}
 
Example 9
Source File: ZeroPaddingLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    int[] hwd = ConvolutionUtils.getHWDFromInputType(inputType);
    int outH = hwd[0] + padding[0] + padding[1];
    int outW = hwd[1] + padding[2] + padding[3];

    InputType.InputTypeConvolutional c = (InputType.InputTypeConvolutional)inputType;

    return InputType.convolutional(outH, outW, hwd[2], c.getFormat());
}
 
Example 10
Source File: SpaceToDepthLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.CNN) {
        throw new IllegalStateException("Invalid input for space to channels layer (layer name=\"" + getLayerName()
                        + "\"): Expected CNN input, got " + inputType);
    }
    InputType.InputTypeConvolutional i = (InputType.InputTypeConvolutional) inputType;
    return InputType.convolutional(i.getHeight() / blockSize, i.getWidth() / blockSize,
                    i.getChannels() * blockSize * blockSize, i.getFormat());
}
 
Example 11
Source File: RnnToCnnPreProcessor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input type: Expected input of type RNN, got " + inputType);
    }

    InputType.InputTypeRecurrent c = (InputType.InputTypeRecurrent) inputType;
    int expSize = inputHeight * inputWidth * numChannels;
    if (c.getSize() != expSize) {
        throw new IllegalStateException("Invalid input: expected RNN input of size " + expSize + " = (d="
                        + numChannels + " * w=" + inputWidth + " * h=" + inputHeight + "), got " + inputType);
    }

    return InputType.convolutional(inputHeight, inputWidth, numChannels);
}
 
Example 12
Source File: FeedForwardToCnnPreProcessor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(InputType inputType) {

    switch (inputType.getType()) {
        case FF:
            InputType.InputTypeFeedForward c = (InputType.InputTypeFeedForward) inputType;
            val expSize = inputHeight * inputWidth * numChannels;
            if (c.getSize() != expSize) {
                throw new IllegalStateException("Invalid input: expected FeedForward input of size " + expSize
                                + " = (d=" + numChannels + " * w=" + inputWidth + " * h=" + inputHeight + "), got "
                                + inputType);
            }
            return InputType.convolutional(inputHeight, inputWidth, numChannels);
        case CNN:
            InputType.InputTypeConvolutional c2 = (InputType.InputTypeConvolutional) inputType;

            if (c2.getChannels() != numChannels || c2.getHeight() != inputHeight || c2.getWidth() != inputWidth) {
                throw new IllegalStateException("Invalid input: Got CNN input type with (d,w,h)=(" + c2.getChannels()
                                + "," + c2.getWidth() + "," + c2.getHeight() + ") but expected (" + numChannels
                                + "," + inputHeight + "," + inputWidth + ")");
            }
            return c2;
        case CNNFlat:
            InputType.InputTypeConvolutionalFlat c3 = (InputType.InputTypeConvolutionalFlat) inputType;
            if (c3.getDepth() != numChannels || c3.getHeight() != inputHeight || c3.getWidth() != inputWidth) {
                throw new IllegalStateException("Invalid input: Got CNN input type with (d,w,h)=(" + c3.getDepth()
                                + "," + c3.getWidth() + "," + c3.getHeight() + ") but expected (" + numChannels
                                + "," + inputHeight + "," + inputWidth + ")");
            }
            return c3.getUnflattenedType();
        default:
            throw new IllegalStateException("Invalid input type: got " + inputType);
    }
}
 
Example 13
Source File: ReshapePreprocessor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputType getOutputType(InputType inputType) throws InvalidInputTypeException {
    long[] shape = getShape(this.targetShape, 0);
    InputType ret;
    switch (shape.length) {
        case 2:
            ret = InputType.feedForward(shape[1]);
            break;
        case 3:
            RNNFormat format = RNNFormat.NCW;
            if(this.format != null && this.format instanceof RNNFormat)
                format = (RNNFormat)this.format;

            ret = InputType.recurrent(shape[2], shape[1], format);
            break;
        case 4:
            if (inputShape.length == 1 || inputType.getType() == InputType.Type.RNN) {
                ret = InputType.convolutional(shape[1], shape[2], shape[3]);
            } else {

                CNN2DFormat cnnFormat = CNN2DFormat.NCHW;
                if (this.format != null && this.format instanceof CNN2DFormat)
                    cnnFormat = (CNN2DFormat) this.format;

                if (cnnFormat == CNN2DFormat.NCHW) {
                    ret = InputType.convolutional(shape[2], shape[3], shape[1], cnnFormat);
                } else {
                    ret = InputType.convolutional(shape[1], shape[2], shape[3], cnnFormat);
                }
            }
            break;
        default:
            throw new UnsupportedOperationException(
                    "Cannot infer input type for reshape array " + Arrays.toString(shape));
    }
    return ret;
}
 
Example 14
Source File: TestComputationGraphNetwork.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void singleInputElemVertex() {
    final InputType inputType = InputType.convolutional(10, 10, 2);
    final ComputationGraph graph = new ComputationGraph(new NeuralNetConfiguration.Builder()
            .graphBuilder()
            .setInputTypes(inputType)
            .addInputs("input")
            .setOutputs("output")
            .addLayer("0", new ConvolutionLayer.Builder().nOut(5).convolutionMode(ConvolutionMode.Same).build(),"input" )
            .addVertex("dummyAdd", new ElementWiseVertex(ElementWiseVertex.Op.Add), "0")
            .addLayer("output", new CnnLossLayer(), "dummyAdd")
            .build());
    graph.init();
    graph.outputSingle(Nd4j.randn(1, 2, 10, 10));
}
 
Example 15
Source File: GlobalPoolingLayer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {

    switch (inputType.getType()) {
        case FF:
            throw new UnsupportedOperationException(
                            "Global max pooling cannot be applied to feed-forward input type. Got input type = "
                                            + inputType);
        case RNN:
            InputType.InputTypeRecurrent recurrent = (InputType.InputTypeRecurrent) inputType;
            if (collapseDimensions) {
                //Return 2d (feed-forward) activations
                return InputType.feedForward(recurrent.getSize());
            } else {
                //Return 3d activations, with shape [minibatch, timeStepSize, 1]
                return recurrent;
            }
        case CNN:
            InputType.InputTypeConvolutional conv = (InputType.InputTypeConvolutional) inputType;
            if (collapseDimensions) {
                return InputType.feedForward(conv.getChannels());
            } else {
                return InputType.convolutional(1, 1, conv.getChannels(), conv.getFormat());
            }
        case CNN3D:
            InputType.InputTypeConvolutional3D conv3d = (InputType.InputTypeConvolutional3D) inputType;
            if (collapseDimensions) {
                return InputType.feedForward(conv3d.getChannels());
            } else {
                return InputType.convolutional3D(1, 1, 1, conv3d.getChannels());
            }
        case CNNFlat:
            InputType.InputTypeConvolutionalFlat convFlat = (InputType.InputTypeConvolutionalFlat) inputType;
            if (collapseDimensions) {
                return InputType.feedForward(convFlat.getDepth());
            } else {
                return InputType.convolutional(1, 1, convFlat.getDepth());
            }
        default:
            throw new UnsupportedOperationException("Unknown or not supported input type: " + inputType);
    }
}
 
Example 16
Source File: CustomBroadcast.java    From wekaDeeplearning4j with GNU General Public License v3.0 4 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    InputType.InputTypeConvolutional convolutional = (InputType.InputTypeConvolutional) inputType;
    long channels = convolutional.getChannels();
    return InputType.convolutional(width, width, channels, CNN2DFormat.NHWC);
}
 
Example 17
Source File: UnstackVertex.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException {
    if (vertexInputs.length == 1)
        return vertexInputs[0];
    InputType first = vertexInputs[0];
    if (first.getType() == InputType.Type.CNNFlat) {
        //TODO
        //Merging flattened CNN format data could be messy?
        throw new InvalidInputTypeException(
                        "Invalid input: UnstackVertex cannot currently merge CNN data in flattened format. Got: "
                                        + vertexInputs);
    } else if (first.getType() != InputType.Type.CNN) {
        //FF or RNN data inputs
        int size = 0;
        InputType.Type type = null;
        for (int i = 0; i < vertexInputs.length; i++) {
            if (vertexInputs[i].getType() != first.getType()) {
                throw new InvalidInputTypeException(
                                "Invalid input: UnstackVertex cannot merge activations of different types:"
                                                + " first type = " + first.getType() + ", input type " + (i + 1)
                                                + " = " + vertexInputs[i].getType());
            }

            long thisSize;
            switch (vertexInputs[i].getType()) {
                case FF:
                    thisSize = ((InputType.InputTypeFeedForward) vertexInputs[i]).getSize();
                    type = InputType.Type.FF;
                    break;
                case RNN:
                    thisSize = ((InputType.InputTypeRecurrent) vertexInputs[i]).getSize();
                    type = InputType.Type.RNN;
                    break;
                default:
                    throw new IllegalStateException("Unknown input type: " + vertexInputs[i]); //Should never happen
            }
            if (thisSize <= 0) {//Size is not defined
                size = -1;
            } else {
                size += thisSize;
            }
        }

        if (size > 0) {
            //Size is specified
            if (type == InputType.Type.FF)
                return InputType.feedForward(size);
            else
                return InputType.recurrent(size);
        } else {
            //size is unknown
            if (type == InputType.Type.FF)
                return InputType.feedForward(-1);
            else
                return InputType.recurrent(-1);
        }
    } else {
        //CNN inputs... also check that the channels, width and heights match:
        InputType.InputTypeConvolutional firstConv = (InputType.InputTypeConvolutional) first;

        val fd = firstConv.getChannels();
        val fw = firstConv.getWidth();
        val fh = firstConv.getHeight();

        long depthSum = fd;

        for (int i = 1; i < vertexInputs.length; i++) {
            if (vertexInputs[i].getType() != InputType.Type.CNN) {
                throw new InvalidInputTypeException(
                                "Invalid input: UnstackVertex cannot process activations of different types:"
                                                + " first type = " + InputType.Type.CNN + ", input type " + (i + 1)
                                                + " = " + vertexInputs[i].getType());
            }

            InputType.InputTypeConvolutional otherConv = (InputType.InputTypeConvolutional) vertexInputs[i];

            val od = otherConv.getChannels();
            val ow = otherConv.getWidth();
            val oh = otherConv.getHeight();

            if (fw != ow || fh != oh) {
                throw new InvalidInputTypeException(
                                "Invalid input: UnstackVertex cannot merge CNN activations of different width/heights:"
                                                + "first [channels,width,height] = [" + fd + "," + fw + "," + fh
                                                + "], input " + i + " = [" + od + "," + ow + "," + oh + "]");
            }

            depthSum += od;
        }

        return InputType.convolutional(fh, fw, depthSum);
    }
}
 
Example 18
Source File: PoolHelperVertex.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException {
    if (vertexInputs.length == 1)
        return vertexInputs[0];
    InputType first = vertexInputs[0];
    if (first.getType() == InputType.Type.CNNFlat) {
        //TODO
        //Merging flattened CNN format data could be messy?
        throw new InvalidInputTypeException(
                        "Invalid input: MergeVertex cannot currently merge CNN data in flattened format. Got: "
                                        + vertexInputs);
    } else if (first.getType() != InputType.Type.CNN) {
        //FF or RNN data inputs
        int size = 0;
        InputType.Type type = null;
        for (int i = 0; i < vertexInputs.length; i++) {
            if (vertexInputs[i].getType() != first.getType()) {
                throw new InvalidInputTypeException(
                                "Invalid input: MergeVertex cannot merge activations of different types:"
                                                + " first type = " + first.getType() + ", input type " + (i + 1)
                                                + " = " + vertexInputs[i].getType());
            }

            long thisSize;
            switch (vertexInputs[i].getType()) {
                case FF:
                    thisSize = ((InputType.InputTypeFeedForward) vertexInputs[i]).getSize();
                    type = InputType.Type.FF;
                    break;
                case RNN:
                    thisSize = ((InputType.InputTypeRecurrent) vertexInputs[i]).getSize();
                    type = InputType.Type.RNN;
                    break;
                default:
                    throw new IllegalStateException("Unknown input type: " + vertexInputs[i]); //Should never happen
            }
            if (thisSize <= 0) {//Size is not defined
                size = -1;
            } else {
                size += thisSize;
            }
        }

        if (size > 0) {
            //Size is specified
            if (type == InputType.Type.FF)
                return InputType.feedForward(size);
            else
                return InputType.recurrent(size);
        } else {
            //size is unknown
            if (type == InputType.Type.FF)
                return InputType.feedForward(-1);
            else
                return InputType.recurrent(-1);
        }
    } else {
        //CNN inputs... also check that the channels, width and heights match:
        InputType.InputTypeConvolutional firstConv = (InputType.InputTypeConvolutional) first;

        val fd = firstConv.getChannels();
        val fw = firstConv.getWidth();
        val fh = firstConv.getHeight();

        long depthSum = fd;

        for (int i = 1; i < vertexInputs.length; i++) {
            if (vertexInputs[i].getType() != InputType.Type.CNN) {
                throw new InvalidInputTypeException(
                                "Invalid input: MergeVertex cannot process activations of different types:"
                                                + " first type = " + InputType.Type.CNN + ", input type " + (i + 1)
                                                + " = " + vertexInputs[i].getType());
            }

            InputType.InputTypeConvolutional otherConv = (InputType.InputTypeConvolutional) vertexInputs[i];

            long od = otherConv.getChannels();
            long ow = otherConv.getWidth();
            long oh = otherConv.getHeight();

            if (fw != ow || fh != oh) {
                throw new InvalidInputTypeException(
                                "Invalid input: MergeVertex cannot merge CNN activations of different width/heights:"
                                                + "first [channels,width,height] = [" + fd + "," + fw + "," + fh
                                                + "], input " + i + " = [" + od + "," + ow + "," + oh + "]");
            }

            depthSum += od;
        }

        return InputType.convolutional(fh, fw, depthSum);
    }
}
 
Example 19
Source File: ConvDataFormatTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public InputType getOutputType(InputType inputType) {
    InputType.InputTypeConvolutional c = (InputType.InputTypeConvolutional) inputType;
    return InputType.convolutional(c.getHeight(), c.getWidth(), c.getChannels(), CNN2DFormat.NCHW);
}
 
Example 20
Source File: ConvDataFormatTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public InputType getOutputType(InputType inputType) {
    InputType.InputTypeConvolutional c = (InputType.InputTypeConvolutional) inputType;
    return InputType.convolutional(c.getHeight(), c.getWidth(), c.getChannels(), CNN2DFormat.NCHW);
}