Java Code Examples for org.deeplearning4j.nn.conf.inputs.InputType#InputTypeConvolutional3D

The following examples show how to use org.deeplearning4j.nn.conf.inputs.InputType#InputTypeConvolutional3D . 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: Upsampling3D.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public LayerMemoryReport getMemoryReport(InputType inputType) {
    InputType.InputTypeConvolutional3D c = (InputType.InputTypeConvolutional3D) inputType;
    InputType.InputTypeConvolutional3D outputType =
                    (InputType.InputTypeConvolutional3D) getOutputType(-1, inputType);

    // During forward pass: im2col array + reduce. Reduce is counted as activations, so only im2col is working mem
    val im2colSizePerEx = c.getChannels() & outputType.getDepth() * outputType.getHeight() * outputType.getWidth()
                    * size[0] * size[1] * size[2];

    // Current implementation does NOT cache im2col etc... which means: it's recalculated on each backward pass
    long trainingWorkingSizePerEx = im2colSizePerEx;
    if (getIDropout() != null) {
        //Dup on the input before dropout, but only for training
        trainingWorkingSizePerEx += inputType.arrayElementsPerExample();
    }

    return new LayerMemoryReport.Builder(layerName, Upsampling3D.class, inputType, outputType).standardMemory(0, 0) //No params
                    .workingMemory(0, im2colSizePerEx, 0, trainingWorkingSizePerEx)
                    .cacheMemory(MemoryReport.CACHE_MODE_ALL_ZEROS, MemoryReport.CACHE_MODE_ALL_ZEROS) //No caching
                    .build();
}
 
Example 2
Source File: Subsampling3DLayer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public LayerMemoryReport getMemoryReport(InputType inputType) {
    InputType.InputTypeConvolutional3D c = (InputType.InputTypeConvolutional3D) inputType;
    InputType.InputTypeConvolutional3D outputType =
                    (InputType.InputTypeConvolutional3D) getOutputType(-1, inputType);
    val actElementsPerEx = outputType.arrayElementsPerExample();

    //During forward pass: im2col array + reduce. Reduce is counted as activations, so only im2col is working mem
    val im2colSizePerEx = c.getChannels() * outputType.getHeight() * outputType.getWidth() * outputType.getDepth()
                    * kernelSize[0] * kernelSize[1];

    //Current implementation does NOT cache im2col etc... which means: it's recalculated on each backward pass
    long trainingWorkingSizePerEx = im2colSizePerEx;
    if (getIDropout() != null) {
        //Dup on the input before dropout, but only for training
        trainingWorkingSizePerEx += inputType.arrayElementsPerExample();
    }

    return new LayerMemoryReport.Builder(layerName, Subsampling3DLayer.class, inputType, outputType)
                    .standardMemory(0, 0) //No params
                    .workingMemory(0, im2colSizePerEx, 0, trainingWorkingSizePerEx)
                    .cacheMemory(MemoryReport.CACHE_MODE_ALL_ZEROS, MemoryReport.CACHE_MODE_ALL_ZEROS) //No caching
                    .build();
}
 
Example 3
Source File: Cnn3DToFeedForwardPreProcessor.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.CNN3D) {
        throw new IllegalStateException("Invalid input type: Expected input of type CNN3D, got " + inputType);
    }

    InputType.InputTypeConvolutional3D c = (InputType.InputTypeConvolutional3D) inputType;
    val outSize = c.getChannels() * c.getDepth() * c.getHeight() * c.getWidth();
    return InputType.feedForward(outSize);
}
 
Example 4
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 5
Source File: Upsampling3D.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 Upsampling 3D layer (layer name=\"" + getLayerName()
                        + "\"): Expected CNN3D input, got " + inputType);
    }
    InputType.InputTypeConvolutional3D i = (InputType.InputTypeConvolutional3D) inputType;

    long inHeight = (int) i.getHeight();
    long inWidth = (int) i.getWidth();
    long inDepth = (int) i.getDepth();
    long inChannels = (int) i.getChannels();

    return InputType.convolutional3D(size[0] * inDepth, size[1] * inHeight, size[2] * inWidth, inChannels);
}
 
Example 6
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 7
Source File: FeedForwardLayer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public InputPreProcessor getPreProcessorForInputType(InputType inputType) {
    if (inputType == null) {
        throw new IllegalStateException(
                        "Invalid input for layer (layer name = \"" + getLayerName() + "\"): input type is null");
    }

    switch (inputType.getType()) {
        case FF:
        case CNNFlat:
            //FF -> FF and CNN (flattened format) -> FF: no preprocessor necessary
            return null;
        case RNN:
            //RNN -> FF
            return new RnnToFeedForwardPreProcessor(((InputType.InputTypeRecurrent)inputType).getFormat());
        case CNN:
            //CNN -> FF
            InputType.InputTypeConvolutional c = (InputType.InputTypeConvolutional) inputType;
            return new CnnToFeedForwardPreProcessor(c.getHeight(), c.getWidth(), c.getChannels(), c.getFormat());
        case CNN3D:
            //CNN3D -> FF
            InputType.InputTypeConvolutional3D c3d = (InputType.InputTypeConvolutional3D) inputType;
            return new Cnn3DToFeedForwardPreProcessor(c3d.getDepth(), c3d.getHeight(), c3d.getWidth(),
                            c3d.getChannels(), c3d.getDataFormat() == Convolution3D.DataFormat.NCDHW);
        default:
            throw new RuntimeException("Unknown input type: " + inputType);
    }
}
 
Example 8
Source File: Deconvolution3D.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.CNN3D) {
        throw new IllegalStateException("Invalid input for Deconvolution 3D layer (layer name=\"" + getLayerName() + "\"): Expected CNN3D input, got " + inputType);
    }

    if (nIn <= 0 || override) {
        InputType.InputTypeConvolutional3D c = (InputType.InputTypeConvolutional3D) inputType;
        this.nIn = c.getChannels();
    }
}
 
Example 9
Source File: Convolution3D.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.CNN3D) {
        throw new IllegalStateException("Invalid input for Convolution 3D layer (layer name=\"" + getLayerName()
                        + "\"): Expected CNN3D input, got " + inputType);
    }

    if (nIn <= 0 || override) {
        InputType.InputTypeConvolutional3D c = (InputType.InputTypeConvolutional3D) inputType;
        this.nIn = c.getChannels();
    }
}
 
Example 10
Source File: Cropping3D.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 cropping 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() - cropping[0] - cropping[1],
                    c.getHeight() - cropping[2] - cropping[3], c.getWidth() - cropping[4] - cropping[5],
                    c.getChannels());
}
 
Example 11
Source File: KerasInput.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * Get layer output type.
 *
 * @param inputType Array of InputTypes
 * @return output type as InputType
 * @throws InvalidKerasConfigurationException     Invalid Keras config
 * @throws UnsupportedKerasConfigurationException Unsupported Keras config
 */
@Override
public InputType getOutputType(InputType... inputType)
        throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
    if (inputType.length > 0)
        log.warn("Keras Input layer does not accept inputs (received " + inputType.length + "). Ignoring.");
    InputType myInputType;
    switch (this.inputShape.length) {
        case 1:
            myInputType = new InputType.InputTypeFeedForward(this.inputShape[0], null);
            break;
        case 2:
            if(this.dimOrder != null) {
                switch (this.dimOrder) {
                    case TENSORFLOW:    //NWC == channels_last
                        myInputType = new InputType.InputTypeRecurrent(this.inputShape[1], this.inputShape[0], RNNFormat.NWC);
                        break;
                    case THEANO:        //NCW == channels_first
                        myInputType = new InputType.InputTypeRecurrent(this.inputShape[0], this.inputShape[1], RNNFormat.NCW);
                        break;
                    case NONE:
                        //Assume RNN in [mb, seqLen, size] format
                        myInputType = new InputType.InputTypeRecurrent(this.inputShape[1], this.inputShape[0], RNNFormat.NWC);
                        break;
                    default:
                        throw new IllegalStateException("Unknown/not supported dimension ordering: " + this.dimOrder);
                }
            } else {
                //Assume RNN in [mb, seqLen, size] format
                myInputType = new InputType.InputTypeRecurrent(this.inputShape[1], this.inputShape[0], RNNFormat.NWC);
            }

            break;
        case 3:
            switch (this.dimOrder) {
                case TENSORFLOW:
                    /* TensorFlow convolutional input: # rows, # cols, # channels */
                    myInputType = new InputType.InputTypeConvolutional(this.inputShape[0], this.inputShape[1],
                            this.inputShape[2], CNN2DFormat.NHWC);
                    break;
                case THEANO:
                    /* Theano convolutional input:     # channels, # rows, # cols */
                    myInputType = new InputType.InputTypeConvolutional(this.inputShape[1], this.inputShape[2],
                            this.inputShape[0], CNN2DFormat.NCHW);
                    break;
                default:
                    this.dimOrder = DimOrder.THEANO;
                    myInputType = new InputType.InputTypeConvolutional(this.inputShape[1], this.inputShape[2],
                            this.inputShape[0], CNN2DFormat.NCHW);
                    log.warn("Couldn't determine dim ordering / data format from model file. Older Keras " +
                            "versions may come without specified backend, in which case we assume the model was " +
                            "built with theano." );
            }
            break;
        case 4:
            switch (this.dimOrder) {
                case TENSORFLOW:
                    myInputType = new InputType.InputTypeConvolutional3D(Convolution3D.DataFormat.NDHWC,
                            this.inputShape[0], this.inputShape[1],
                            this.inputShape[2],this.inputShape[3]);
                    break;
                case THEANO:
                    myInputType = new InputType.InputTypeConvolutional3D(Convolution3D.DataFormat.NCDHW,
                            this.inputShape[3], this.inputShape[0],
                            this.inputShape[1],this.inputShape[2]);
                    break;
                default:
                    this.dimOrder = DimOrder.THEANO;
                    myInputType = new InputType.InputTypeConvolutional3D(Convolution3D.DataFormat.NCDHW,
                            this.inputShape[3], this.inputShape[0],
                            this.inputShape[1],this.inputShape[2]);
                    log.warn("Couldn't determine dim ordering / data format from model file. Older Keras " +
                            "versions may come without specified backend, in which case we assume the model was " +
                            "built with theano." );
            }
            break;
        default:
            throw new UnsupportedKerasConfigurationException(
                    "Inputs with " + this.inputShape.length + " dimensions not supported");
    }
    return myInputType;
}
 
Example 12
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);
    }
}