Package org.deeplearning4j.util
Class ConvolutionUtils
- java.lang.Object
-
- org.deeplearning4j.util.ConvolutionUtils
-
public class ConvolutionUtils extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static String
NCHW_NHWC_ERROR_MSG
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static INDArray
adapt2dMask(INDArray mask, INDArray output, @NonNull CNN2DFormat format, LayerWorkspaceMgr workspaceMgr, ArrayType type)
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.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.static int[]
effectiveKernelSize(int[] kernel, int[] dilation)
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.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.static CNN2DFormat
getFormatForLayer(Layer layer)
Get the format for a given layer.static int[]
getHeightAndWidth(int[] shape)
Get the height and width for an imagestatic int[]
getHeightAndWidth(NeuralNetConfiguration conf)
Get the height and width from the configurationstatic int[]
getHWDFromInputType(InputType inputType)
Get heigh/width/channels as length 3 int[] from the InputTypestatic 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 twicestatic int[]
getOutputSize(INDArray inputData, int[] kernel, int[] strides, int[] padding, ConvolutionMode convolutionMode)
Deprecated.static int[]
getOutputSize(INDArray inputData, int[] kernel, int[] strides, int[] padding, ConvolutionMode convolutionMode, int[] dilation)
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 configurationstatic int[]
getSameModeBottomRightPadding(int[] outSize, int[] inSize, int[] kernel, int[] strides, int[] dilation)
Get bottom and right padding for same mode only.static int[]
getSameModeTopLeftPadding(int[] outSize, int[] inSize, int[] kernel, int[] strides, int[] dilation)
Get top and left padding for same mode only.static boolean
layerHasConvolutionLayout(Layer layer)
Returns true if a layer has aCNN2DFormat
property.static int
numChannels(int[] shape)
Returns the number of feature maps for a given shape (must be at least 3 dimensionsstatic long
numFeatureMap(NeuralNetConfiguration conf)
static PaddingMode
paddingModeForConvolutionMode(ConvolutionMode convolutionMode)
static INDArray
reshape2dTo4d(INDArray in2d, long[] toShape, CNN2DFormat format, LayerWorkspaceMgr workspaceMgr, ArrayType type)
static INDArray
reshape2dTo5d(Convolution3D.DataFormat format, INDArray in2d, long n, long d, long h, long w, long ch, LayerWorkspaceMgr workspaceMgr, ArrayType type)
static INDArray
reshape3dMask(INDArray mask, LayerWorkspaceMgr workspaceMgr, ArrayType type)
static INDArray
reshape4dMask(INDArray mask, LayerWorkspaceMgr workspaceMgr, ArrayType arrayType)
static INDArray
reshape4dTo2d(INDArray in, CNN2DFormat format, LayerWorkspaceMgr workspaceMgr, ArrayType type)
static INDArray
reshape4dTo2d(INDArray in, LayerWorkspaceMgr workspaceMgr, ArrayType type)
static INDArray
reshape5dTo2d(Convolution3D.DataFormat format, INDArray in, LayerWorkspaceMgr workspaceMgr, ArrayType type)
static INDArray
reshapeCnn3dMask(Convolution3D.DataFormat format, INDArray mask, INDArray label, LayerWorkspaceMgr workspaceMgr, ArrayType type)
static INDArray
reshapeMaskIfRequired(INDArray mask, INDArray output, CNN2DFormat format, LayerWorkspaceMgr workspaceMgr, ArrayType type)
static INDArray
reshapeMaskIfRequired(INDArray mask, INDArray output, LayerWorkspaceMgr workspaceMgr, ArrayType type)
static void
validateCnnKernelStridePadding(int[] kernelSize, int[] stride, int[] padding)
Perform validation on the CNN layer kernel/stride/padding.static void
validateConvolutionModePadding(ConvolutionMode mode, int[] padding)
Check that the convolution mode is consistent with the padding specificationstatic void
validateShapes(INDArray inputData, int[] eKernel, int[] strides, int[] padding, ConvolutionMode convolutionMode, int[] dilation, int[] inShape, boolean atrous)
-
-
-
Field Detail
-
NCHW_NHWC_ERROR_MSG
public static final String NCHW_NHWC_ERROR_MSG
- See Also:
- Constant Field Values
-
-
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 returndefaultValue
- the default value if none is present- Returns:
- the int value as specified above.
-
getOutputSize
@Deprecated public static int[] getOutputSize(INDArray inputData, int[] kernel, int[] strides, int[] padding, ConvolutionMode convolutionMode)
Deprecated.
-
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 datakernel
- 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 datakernel
- 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
@Deprecated public static int[] getOutputSize(INDArray inputData, int[] kernel, int[] strides, int[] padding, ConvolutionMode convolutionMode, int[] dilation)
-
layerHasConvolutionLayout
public static boolean layerHasConvolutionLayout(Layer layer)
Returns true if a layer has aCNN2DFormat
property. This is currently in use for:ConvolutionLayer
,SubsamplingLayer
,Upsampling2D
,SpaceToBatchLayer
,SpaceToDepthLayer
,ZeroPaddingLayer
,SeparableConvolution2D
,Cropping2D
,DepthwiseConvolution2D
- Parameters:
layer
- the layer to check- Returns:
- true if the layer is one of the above types, false otherwise
-
getFormatForLayer
public static CNN2DFormat getFormatForLayer(Layer layer)
Get the format for a given layer.layerHasConvolutionLayout(Layer)
should return true on the givenLayer
type or anIllegalArgumentException
will be thrown- Parameters:
layer
- the input layer- Returns:
- the
CNN2DFormat
for the given layer
-
paddingModeForConvolutionMode
public static PaddingMode paddingModeForConvolutionMode(ConvolutionMode convolutionMode)
ConvertConvolutionMode
toPaddingMode
ConvolutionMode.Same
:PaddingMode.SAME
ConvolutionMode.Strict
,ConvolutionMode.Truncate
:PaddingMode.VALID
ConvolutionMode.Causal
:PaddingMode.VALID
- Parameters:
convolutionMode
- the inputConvolutionMode
- Returns:
- the equivalent
PaddingMode
-
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 datakernel
- 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 checkstride
- Stride array to checkpadding
- Padding array to check
-
reshape4dTo2d
public static INDArray reshape4dTo2d(INDArray in, LayerWorkspaceMgr workspaceMgr, ArrayType type)
-
reshape4dTo2d
public static INDArray reshape4dTo2d(INDArray in, CNN2DFormat format, LayerWorkspaceMgr workspaceMgr, ArrayType type)
-
reshape5dTo2d
public static INDArray reshape5dTo2d(@NonNull Convolution3D.DataFormat format, INDArray in, LayerWorkspaceMgr workspaceMgr, ArrayType type)
-
reshapeCnn3dMask
public static INDArray reshapeCnn3dMask(@NonNull Convolution3D.DataFormat format, INDArray mask, INDArray label, LayerWorkspaceMgr workspaceMgr, ArrayType type)
-
reshape2dTo4d
public static INDArray reshape2dTo4d(INDArray in2d, long[] toShape, CNN2DFormat format, LayerWorkspaceMgr workspaceMgr, ArrayType type)
-
reshape2dTo5d
public static INDArray reshape2dTo5d(Convolution3D.DataFormat format, INDArray in2d, long n, long d, long h, long w, long ch, LayerWorkspaceMgr workspaceMgr, ArrayType type)
-
reshapeMaskIfRequired
@Deprecated public static INDArray reshapeMaskIfRequired(INDArray mask, INDArray output, LayerWorkspaceMgr workspaceMgr, ArrayType type)
-
reshapeMaskIfRequired
public static INDArray reshapeMaskIfRequired(INDArray mask, INDArray output, CNN2DFormat format, LayerWorkspaceMgr workspaceMgr, ArrayType type)
-
adapt2dMask
public static INDArray adapt2dMask(INDArray mask, INDArray output, @NonNull @NonNull CNN2DFormat format, LayerWorkspaceMgr workspaceMgr, ArrayType type)
-
reshape3dMask
public static INDArray reshape3dMask(INDArray mask, LayerWorkspaceMgr workspaceMgr, ArrayType type)
-
reshape4dMask
public static INDArray reshape4dMask(INDArray mask, LayerWorkspaceMgr workspaceMgr, ArrayType arrayType)
-
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 sizekernel
- Kernel sizestride
- Stridepadding
- Paddingdilation
- Dilationcm
- 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 layerstride
- Stridepadding
- Paddingdilation
- DilationconvolutionMode
- Convolution mode- Returns:
- The mask array corresponding to the network output
-
-