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

The following examples show how to use org.deeplearning4j.nn.conf.inputs.InputType#getType() . 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: InputTypeUtil.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method for determining the appropriate preprocessor for CNN layers, such as {@link ConvolutionLayer} and
 * {@link SubsamplingLayer}
 *
 * @param inputType     Input type to get the preprocessor for
 * @return              Null if no preprocessor is required; otherwise the appropriate preprocessor for the given input type
 */
public static InputPreProcessor getPreProcessorForInputTypeCnn3DLayers(InputType inputType, String layerName) {
    switch (inputType.getType()) {
        case FF:
            log.info("Automatic addition of FF -> CNN3D preprocessors: not yet implemented (layer name: \""
                            + layerName + "\")");
            return null;
        case RNN:
            log.warn("Automatic addition of RNN -> CNN3D preprocessors: not yet implemented (layer name: \""
                            + layerName + "\")");
            return null;
        // TODO: handle CNN to CNN3D
        case CNN3D:
            return null;
        default:
            throw new RuntimeException("Unknown input type: " + inputType);
    }
}
 
Example 2
Source File: Subsampling1DLayer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for Subsampling1D layer (layer name=\"" + getLayerName()
                        + "\"): Expected RNN input, got " + inputType);
    }
    InputType.InputTypeRecurrent r = (InputType.InputTypeRecurrent) inputType;
    long inputTsLength = r.getTimeSeriesLength();
    long outLength;
    if (inputTsLength < 0) {
        //Probably: user did InputType.recurrent(x) without specifying sequence length
        outLength = -1;
    } else {
        outLength = Convolution1DUtils.getOutputSize(inputTsLength, kernelSize[0], stride[0], padding[0],
                        convolutionMode, dilation[0]);
    }
    return InputType.recurrent(r.getSize(), outLength, r.getFormat());
}
 
Example 3
Source File: PrimaryCapsules.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setNIn(InputType inputType, boolean override) {
    if (inputType == null || inputType.getType() != Type.CNN) {
        throw new IllegalStateException("Invalid input for Primary Capsules layer (layer name = \""
                + layerName + "\"): expect CNN input.  Got: " + inputType);
    }

    InputTypeConvolutional ci = (InputTypeConvolutional) inputType;

    this.inputChannels = (int) ci.getChannels();

    if(capsules <= 0 || override) {

        InputTypeConvolutional out = (InputTypeConvolutional) InputTypeUtil
                .getOutputTypeCnnLayers(inputType, kernelSize, stride, padding, dilation, convolutionMode,
                        capsuleDimensions * channels, -1, getLayerName(), PrimaryCapsules.class);

        this.capsules = (int) (out.getChannels() * out.getHeight() * out.getWidth() / capsuleDimensions);
    }
}
 
Example 4
Source File: SelfAttentionLayer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public InputType getOutputType(int layerIndex, InputType inputType) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for Self Attention layer (layer index = " + layerIndex
                + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: "
                + inputType);
    }

    InputType.InputTypeRecurrent itr = (InputType.InputTypeRecurrent) inputType;

    if(projectInput){
        return InputType.recurrent(nOut, itr.getTimeSeriesLength());
    }else{
        return InputType.recurrent(nIn, itr.getTimeSeriesLength());
    }
}
 
Example 5
Source File: Upsampling2D.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setNIn(InputType inputType, boolean override) {
    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);
    }
    this.format = ((InputType.InputTypeConvolutional)inputType).getFormat();
}
 
Example 6
Source File: RecurrentAttentionLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setNIn(InputType inputType, boolean override) {
    if (inputType == null || inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Invalid input for Recurrent Attention layer (layer name = \"" + getLayerName()
                + "\"): expect RNN input type with size > 0. Got: " + inputType);
    }

    if (nIn <= 0 || override) {
        InputType.InputTypeRecurrent r = (InputType.InputTypeRecurrent) inputType;
        this.nIn = r.getSize();
    }
}
 
Example 7
Source File: SeparableConvolution2D.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 Convolution layer (layer name=\"" + getLayerName()
                        + "\"): Expected CNN input, got " + inputType);
    }

    CNN2DFormat format = ((InputType.InputTypeConvolutional)inputType).getFormat();

    return InputTypeUtil.getOutputTypeCnnLayers(inputType, kernelSize, stride, padding, dilation, convolutionMode,
                    nOut, layerIndex, getLayerName(), format, SeparableConvolution2DLayer.class);
}
 
Example 8
Source File: FeedForwardToCnn3DPreProcessor.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;
            int expSize = inputDepth * 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.convolutional3D(inputDepth, 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 (c,w,h)=(" + c2.getChannels()
                        + "," + c2.getWidth() + "," + c2.getHeight() + ") but expected (" + numChannels
                        + "," + inputHeight + "," + inputWidth + ")");
            }
            return InputType.convolutional3D(1, c2.getHeight(), c2.getWidth(), c2.getChannels());
        case CNN3D:
            InputType.InputTypeConvolutional3D c3 = (InputType.InputTypeConvolutional3D) inputType;

            if (c3.getChannels() != numChannels || c3.getDepth() != inputDepth ||
                    c3.getHeight() != inputHeight || c3.getWidth() != inputWidth) {
                throw new IllegalStateException("Invalid input: Got CNN input type with (c, d,w,h)=("
                        + c3.getChannels() + "," + c3.getDepth() + "," + c3.getWidth() + "," + c3.getHeight()
                        + ") but expected (" + numChannels + "," + inputDepth + ","
                        + inputHeight + "," + inputWidth + ")");
            }
            return c3;
        default:
            throw new IllegalStateException("Invalid input type: got " + inputType);
    }
}
 
Example 9
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 10
Source File: Upsampling1D.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.RNN) {
        throw new IllegalStateException("Invalid input for 1D Upsampling layer (layer index = " + layerIndex
                        + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: "
                        + inputType);
    }
    InputType.InputTypeRecurrent recurrent = (InputType.InputTypeRecurrent) inputType;
    long outLength = recurrent.getTimeSeriesLength();
    if (outLength > 0) {
        outLength *= size[0];
    }
    return InputType.recurrent(recurrent.getSize(), outLength);
}
 
Example 11
Source File: TimeDistributed.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setNIn(InputType inputType, boolean override) {
    if (inputType.getType() != InputType.Type.RNN) {
        throw new IllegalStateException("Only RNN input type is supported as input to TimeDistributed layer");
    }

    InputType.InputTypeRecurrent rnn = (InputType.InputTypeRecurrent) inputType;
    InputType ff = InputType.feedForward(rnn.getSize());
    this.rnnDataFormat = rnn.getFormat();
    underlying.setNIn(ff, override);
}
 
Example 12
Source File: Subsampling3DLayer.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.CNN3D) {
        throw new IllegalStateException("Invalid input for Subsampling 3D layer (layer name=\"" + getLayerName()
                        + "\"): Expected CNN input, got " + inputType);
    }

    long inChannels = ((InputType.InputTypeConvolutional3D) inputType).getChannels();
    if (inChannels > Integer.MAX_VALUE)
        throw new ND4JArraySizeException();
    return InputTypeUtil.getOutputTypeCnn3DLayers(inputType, dataFormat, kernelSize, stride, padding, new int[] {1, 1, 1}, // no dilation
                    convolutionMode, (int) inChannels,
                    layerIndex, getLayerName(), Subsampling3DLayer.class);
}
 
Example 13
Source File: RepeatVector.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.FF) {
        throw new IllegalStateException("Invalid input for RepeatVector layer (layer name=\"" + getLayerName()
                        + "\"): Expected FF input, got " + inputType);
    }
    InputType.InputTypeFeedForward ffInput = (InputType.InputTypeFeedForward) inputType;
    return InputType.recurrent(ffInput.getSize(), n, this.dataFormat);
}
 
Example 14
Source File: BatchNormalization.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputPreProcessor getPreProcessorForInputType(InputType inputType) {
    if (inputType.getType() == InputType.Type.CNNFlat) {
        InputType.InputTypeConvolutionalFlat i = (InputType.InputTypeConvolutionalFlat) inputType;
        return new FeedForwardToCnnPreProcessor(i.getHeight(), i.getWidth(), i.getDepth());
    } else if (inputType.getType() == InputType.Type.RNN) {
        return new RnnToFeedForwardPreProcessor();
    }

    return null;
}
 
Example 15
Source File: Cnn3DLossLayer.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.CNN3D
                    && inputType.getType() != InputType.Type.CNNFlat)) {
        throw new IllegalStateException("Invalid input type for CnnLossLayer (layer index = " + layerIndex
                        + ", layer name=\"" + getLayerName() + "\"): Expected CNN3D or CNNFlat input, got "
                        + inputType);
    }
    return inputType;
}
 
Example 16
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 17
Source File: ZeroPadding1DLayer.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.RNN) {
        throw new IllegalStateException("Invalid input for 1D CNN layer (layer index = " + layerIndex
                        + ", layer name = \"" + getLayerName() + "\"): expect RNN input type with size > 0. Got: "
                        + inputType);
    }
    InputType.InputTypeRecurrent recurrent = (InputType.InputTypeRecurrent) inputType;
    return InputType.recurrent(recurrent.getSize(), recurrent.getTimeSeriesLength() + padding[0] + padding[1]);
}
 
Example 18
Source File: ZeroPadding3DLayer.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.CNN3D) {
        throw new IllegalStateException("Invalid input for 3D CNN layer (layer index = " + layerIndex
                        + ", layer name = \"" + getLayerName() + "\"): expect CNN3D input type with size > 0. Got: "
                        + inputType);
    }
    InputType.InputTypeConvolutional3D c = (InputType.InputTypeConvolutional3D) inputType;
    return InputType.convolutional3D(c.getDepth() + padding[0] + padding[1],
                    c.getHeight() + padding[2] + padding[3], c.getWidth() + padding[4] + padding[5],
                    c.getChannels());
}
 
Example 19
Source File: Convolution3D.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.CNN3D) {
        throw new IllegalStateException("Invalid input for Convolution3D layer (layer name=\"" + getLayerName()
                        + "\"): Expected CNN3D input, got " + inputType);
    }
    return InputTypeUtil.getOutputTypeCnn3DLayers(inputType, dataFormat, kernelSize, stride, padding, dilation, convolutionMode,
                    nOut, layerIndex, getLayerName(), Convolution3DLayer.class);
}
 
Example 20
Source File: ElementWiseVertex.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.CNN) {
        //FF, RNN or flat CNN data inputs
        for (int i = 1; i < vertexInputs.length; i++) {
            if (vertexInputs[i].getType() != first.getType()) {
                throw new InvalidInputTypeException(
                        "Invalid input: ElementWise vertex cannot process activations of different types:"
                                + " first type = " + first.getType() + ", input type " + (i + 1)
                                + " = " + vertexInputs[i].getType());
            }
        }
    } 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();

        for (int i = 1; i < vertexInputs.length; i++) {
            if (vertexInputs[i].getType() != InputType.Type.CNN) {
                throw new InvalidInputTypeException(
                        "Invalid input: ElementWise vertex 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 (fd != od || fw != ow || fh != oh) {
                throw new InvalidInputTypeException(
                        "Invalid input: ElementWise vertex cannot process CNN activations of different sizes:"
                                + "first [channels,width,height] = [" + fd + "," + fw + "," + fh
                                + "], input " + i + " = [" + od + "," + ow + "," + oh + "]");
            }
        }
    }
    return first; //Same output shape/size as
}