Class ConvolutionUtils


  • public class ConvolutionUtils
    extends Object
    • Method Detail

      • getIntConfig

        public static int[] getIntConfig​(int[] inputValue,
                                         int defaultValue)
        Return the configuration for a given value for values like stride, dilation, kernel size that require 2 values If the input is already length 2, return that if the length is only 1, return the value specified twice otherwise return the default value duplicated twice
        Parameters:
        inputValue - the input value to return
        defaultValue - the default value if none is present
        Returns:
        the int value as specified above.
      • getDeconvolutionOutputSize

        public static int[] getDeconvolutionOutputSize​(INDArray inputData,
                                                       int[] kernel,
                                                       int[] strides,
                                                       int[] padding,
                                                       ConvolutionMode convolutionMode,
                                                       int[] dilation,
                                                       CNN2DFormat format)
        Get the output size of a deconvolution operation for given input data. In deconvolution, we compute the inverse of the shape computation of a convolution.
        Parameters:
        inputData - Input data
        kernel - Kernel size (height/width)
        strides - Strides (height/width)
        padding - Padding (height/width)
        convolutionMode - Convolution mode (Same, Strict, Truncate)
        dilation - Kernel dilation (height/width)
        Returns:
        Output size: int[2] with output height/width
      • getDeconvolution3DOutputSize

        public static long[] getDeconvolution3DOutputSize​(INDArray inputData,
                                                          int[] kernel,
                                                          int[] strides,
                                                          int[] padding,
                                                          int[] dilation,
                                                          ConvolutionMode convolutionMode,
                                                          Convolution3D.DataFormat dataFormat)
        Get the output size of a deconvolution operation for given input data. In deconvolution, we compute the inverse of the shape computation of a convolution.
        Parameters:
        inputData - Input data
        kernel - Kernel size (height/width)
        strides - Strides (height/width)
        padding - Padding (height/width)
        convolutionMode - Convolution mode (Same, Strict, Truncate)
        dilation - Kernel dilation (height/width)
        Returns:
        Output size: int[2] with output height/width
      • getOutputSize

        public static int[] getOutputSize​(INDArray inputData,
                                          int[] kernel,
                                          int[] strides,
                                          int[] padding,
                                          ConvolutionMode convolutionMode,
                                          int[] dilation,
                                          CNN2DFormat format)
        Get the output size (height/width) for the given input data and CNN configuration
        Parameters:
        inputData - Input data
        kernel - Kernel size (height/width)
        strides - Strides (height/width)
        padding - Padding (height/width)
        convolutionMode - Convolution mode (Same, Strict, Truncate)
        dilation - Kernel dilation (height/width)
        format - Format for input activations
        Returns:
        Output size: int[2] with output height/width
      • validateShapes

        public static void validateShapes​(INDArray inputData,
                                          int[] eKernel,
                                          int[] strides,
                                          int[] padding,
                                          ConvolutionMode convolutionMode,
                                          int[] dilation,
                                          int[] inShape,
                                          boolean atrous)
      • effectiveKernelSize

        public static int[] effectiveKernelSize​(int[] kernel,
                                                int[] dilation)
      • getSameModeTopLeftPadding

        public static int[] getSameModeTopLeftPadding​(int[] outSize,
                                                      int[] inSize,
                                                      int[] kernel,
                                                      int[] strides,
                                                      int[] dilation)
        Get top and left padding for same mode only.
        Parameters:
        outSize - Output size (length 2 array, height dimension first)
        inSize - Input size (length 2 array, height dimension first)
        kernel - Kernel size (length 2 array, height dimension first)
        strides - Strides (length 2 array, height dimension first)
        dilation - Dilation (length 2 array, height dimension first)
        Returns:
        Top left padding (length 2 array, height dimension first)
      • getSameModeBottomRightPadding

        public static int[] getSameModeBottomRightPadding​(int[] outSize,
                                                          int[] inSize,
                                                          int[] kernel,
                                                          int[] strides,
                                                          int[] dilation)
        Get bottom and right padding for same mode only.
        Parameters:
        outSize - Output size (length 2 array, height dimension first)
        inSize - Input size (length 2 array, height dimension first)
        kernel - Kernel size (length 2 array, height dimension first)
        strides - Strides (length 2 array, height dimension first)
        dilation - Dilation (length 2 array, height dimension first)
        Returns:
        Bottom right padding (length 2 array, height dimension first)
      • getHeightAndWidth

        public static int[] getHeightAndWidth​(NeuralNetConfiguration conf)
        Get the height and width from the configuration
        Parameters:
        conf - the configuration to get height and width from
        Returns:
        the configuration to get height and width from
      • numFeatureMap

        public static long numFeatureMap​(NeuralNetConfiguration conf)
        Parameters:
        conf - the configuration to get the number of kernels from
        Returns:
        the number of kernels/filters to apply
      • getHeightAndWidth

        public static int[] getHeightAndWidth​(int[] shape)
        Get the height and width for an image
        Parameters:
        shape - the shape of the image
        Returns:
        the height and width for the image
      • numChannels

        public static int numChannels​(int[] shape)
        Returns the number of feature maps for a given shape (must be at least 3 dimensions
        Parameters:
        shape - the shape to get the number of feature maps for
        Returns:
        the number of feature maps for a particular shape
      • validateConvolutionModePadding

        public static void validateConvolutionModePadding​(ConvolutionMode mode,
                                                          int[] padding)
        Check that the convolution mode is consistent with the padding specification
      • validateCnnKernelStridePadding

        public static void validateCnnKernelStridePadding​(int[] kernelSize,
                                                          int[] stride,
                                                          int[] padding)
        Perform validation on the CNN layer kernel/stride/padding. Expect 2d int[], with values > 0 for kernel size and stride, and values >= 0 for padding.
        Parameters:
        kernelSize - Kernel size array to check
        stride - Stride array to check
        padding - Padding array to check
      • getHWDFromInputType

        public static int[] getHWDFromInputType​(InputType inputType)
        Get heigh/width/channels as length 3 int[] from the InputType
        Parameters:
        inputType - Input type to get
        Returns:
        Length
      • cnn1dMaskReduction

        public static INDArray cnn1dMaskReduction​(INDArray in,
                                                  int kernel,
                                                  int stride,
                                                  int padding,
                                                  int dilation,
                                                  ConvolutionMode cm)
        Given a mask array for a 1D CNN layer of shape [minibatch, sequenceLength], reduce the mask according to the 1D CNN layer configuration. Unlike RNN layers, 1D CNN layers may down-sample the data; consequently, we need to down-sample the mask array in the same way, to maintain the correspondence between the masks and the output activations
        Parameters:
        in - Input size
        kernel - Kernel size
        stride - Stride
        padding - Padding
        dilation - Dilation
        cm - Convolution mode
        Returns:
        Reduced mask
      • cnn2dMaskReduction

        public static INDArray cnn2dMaskReduction​(INDArray inMask,
                                                  int[] kernel,
                                                  int[] stride,
                                                  int[] padding,
                                                  int[] dilation,
                                                  ConvolutionMode convolutionMode)
        Reduce a 2d CNN layer mask array (of 0s and 1s) according to the layer configuration. Note that when a CNN layer changes the shape of the activations (for example, stride > 1) the corresponding mask array needs to change shape also (as there is a correspondence between the two). This method performs the forward pass for the mask.
        Parameters:
        inMask - Input mask array - rank 4, shape [mb,c,h,1] or [mb,c,w,1] or [mb,c,h,w]
        kernel - Kernel configuration for the layer
        stride - Stride
        padding - Padding
        dilation - Dilation
        convolutionMode - Convolution mode
        Returns:
        The mask array corresponding to the network output