depthwise_conv2d#
- ivy.depthwise_conv2d(x, filters, strides, padding, /, *, data_format='NHWC', dilations=1, out=None)[source]#
Compute a 2-D depthwise convolution given 4-D input
x
and filters arrays.- Parameters:
x (
Union
[Array
,NativeArray
]) – Input image [batch_size,h,w,d_in] or [batch_size,d_in,h,w].filters (
Union
[Array
,NativeArray
]) – Convolution filters [fh,fw,d_in]. (d_in must be the same as d from x)strides (
Union
[int
,Tuple
[int
,int
]]) – The stride of the sliding window for each dimension of input.padding (
Union
[str
,Sequence
[Tuple
[int
,int
]]]) – either the string ‘SAME’ (padding with zeros evenly), the string ‘VALID’ (no padding), or a sequence of n (low, high) integer pairs that give the padding to apply before and after each spatial dimension.data_format (
str
, default:'NHWC'
) – The ordering of the dimensions in the input, one of “NHWC” or “NCHW”. “NHWC” corresponds to inputs with shape (batch_size, height, width, channels), while “NCHW” corresponds to input with shape (batch_size, channels, height, width).dilations (
Union
[int
,Tuple
[int
,int
]], default:1
) – The dilation factor for each dimension of input. (Default value = 1)out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – The result of the convolution operation.
Both the description and the type hints above assumes an array input for simplicity,
but this function is nestable, and therefore also accepts
ivy.Container
instances in place of any of the arguments.
Examples
With
ivy.Array
input:>>> x = ivy.random_normal(mean=0, std=1, shape=[1, 28, 28, 3]) >>> filters = ivy.random_normal(mean=0, std=1, shape=[3, 3, 3]) >>> y = ivy.depthwise_conv2d(x, filters, (1, 1), 'VALID') >>> print(y.shape) ivy.Shape(1, 26, 26, 3)
>>> x = ivy.random_normal(mean=0, std=1, shape=[1, 32, 32, 3]) >>> y = ivy.zeros((1, 16, 16, 3)) >>> filters = ivy.random_normal(mean=0, std=1, shape=[5, 5, 3]) >>> ivy.depthwise_conv2d(x, filters, [2, 2], 'SAME', out=y) >>> print(y.shape) ivy.Shape(1, 16, 16, 3)
>>> x = ivy.random_normal(mean=0, std=1, shape=[1, 64, 64, 32]) >>> y = ivy.zeros((1, 61, 61, 32)) >>> filters = ivy.random_normal(mean=0, std=1, shape=[4, 4, 32]) >>> ivy.depthwise_conv2d(x, filters, [1, 1], 'VALID', out=y) >>> print(x.shape) ivy.Shape(1, 64, 64, 32)
With
ivy.NativeArray
input:>>> x = ivy.native_array(ivy.random_normal(mean=0, std=1, shape=[1, 7, 7, 64])) >>> filters = ivy.native_array(ivy.random_normal(mean=0, std=1, shape=[3, 3, 64])) >>> y = ivy.depthwise_conv2d(x, filters, [1, 1], 'SAME') >>> print(y.shape) ivy.Shape(1, 7, 7, 64)
With a mix of
ivy.Array
andivy.Container
inputs:>>> x = ivy.eye(6, 6).reshape((1, 6, 6, 1)) #NHWC >>> a = ivy.array([[1., 1., 1.], [1., -8., 1.], [1., 1., 1.]]).expand_dims(axis=-1) >>> b = ivy.array([[1., 1., 1.], ... [1., 1., 1.], ... [1., 1., 1.]]).expand_dims(axis=-1) / 9.0 >>> filters = ivy.Container(a = a, b = b) >>> y = ivy.depthwise_conv2d(x, filters, 1, 'VALID', dilations=2) >>> print(y) { a: ivy.array([[[[-6.], [0.]], [[0.], [-6.]]]]), b: ivy.array([[[[0.33333334], [0.]], [[0.], [0.33333334]]]]) }
With a mix of
ivy.Array
, code:ivy.NativeArray andivy.Container
inputs:>>> x = ivy.eye(6, 6).reshape((1, 6, 6, 1)) #NHWC >>> y = ivy.native_array(ivy.eye(6, 6).reshape((1, 6, 6, 1))) >>> inp = ivy.Container(x = x, y = y) >>> filter = ivy.array([[1., 1., 1.], ... [1., -8., 1.], ... [1., 1., 1.]]).expand_dims(axis=-1) >>> y = ivy.depthwise_conv2d(inp, filter, 1, 'VALID', dilations=2) >>> print(y) { x: ivy.array([[[[-6.], [0.]], [[0.], [-6.]]]]), y: ivy.array([[[[-6.], [0.]], [[0.], [-6.]]]]) }
- Array.depthwise_conv2d(self, filters, strides, padding, /, *, data_format='NHWC', dilations=1, out=None)[source]#
ivy.Array instance method variant of ivy.depthwise_conv2d. This method simply wraps the function, and so the docstring for ivy.depthwise_conv2d also applies to this method with minimal changes.
- Parameters:
self (
Array
) – Input image [batch_size,h,w,d].filters (
Union
[Array
,NativeArray
]) – Convolution filters [fh,fw,d_in]. (d_in must be the same as d from self)strides (
Union
[int
,Tuple
[int
],Tuple
[int
,int
]]) – The stride of the sliding window for each dimension of input.padding (
Union
[str
,List
[int
]]) – “SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.data_format (
str
, default:'NHWC'
) – “NHWC” or “NCHW”. Defaults to “NHWC”.dilations (
Union
[int
,Tuple
[int
],Tuple
[int
,int
]], default:1
) – The dilation factor for each dimension of input. (Default value = 1)out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – The result of the convolution operation.
Examples
>>> x = ivy.randint(0, 255, shape=(1, 128, 128, 3)).astype(ivy.float32) / 255.0 >>> filters = ivy.random_normal(mean=0, std=1, shape=[3, 3, 3]) >>> y = x.depthwise_conv2d(filters, 2, 'SAME') >>> print(y.shape) (1, 64, 64, 3)
- Container.depthwise_conv2d(self, filters, strides, padding, /, *, data_format='NHWC', dilations=1, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
ivy.Container instance method variant of ivy.depthwise_conv2d. This method simply wraps the function, and so the docstring for ivy.depthwise_conv2d also applies to this method with minimal changes.
- Parameters:
self (
Container
) – Input image [batch_size,h,w,d].filters (
Union
[Array
,NativeArray
,Container
]) – Convolution filters [fh,fw,d_in]. (d_in must be the same as d from self)strides (
Union
[int
,Tuple
[int
],Tuple
[int
,int
],Container
]) – The stride of the sliding window for each dimension of input.padding (
Union
[str
,List
[int
],Container
]) – “SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.data_format (
Union
[str
,Container
], default:'NHWC'
) – “NHWC” or “NCHW”. Defaults to “NHWC”.dilations (
Union
[int
,Tuple
[int
],Tuple
[int
,int
],Container
], default:1
) – The dilation factor for each dimension of input. (Default value = 1)out (
Optional
[Container
], default:None
) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container
- Returns:
ret – The result of the convolution operation.
Examples
>>> a = ivy.randint(0, 255, shape=(1, 128, 128, 3)).astype(ivy.float32) / 255.0 >>> b = ivy.randint(0, 255, shape=(1, 128, 128, 3)).astype(ivy.float32) / 255.0 >>> inp = ivy.Container(a=a, b=b) >>> filters = ivy.random_normal(mean=0, std=1, shape=[3, 3, 3]) >>> y = inp.depthwise_conv2d(filters, 2, 'SAME') >>> print(y.shape) [1, 64, 64, 3]