mindspore.ops.DepthwiseConv2dNative

class mindspore.ops.DepthwiseConv2dNative(channel_multiplier, kernel_size, mode=3, pad_mode='valid', pad=0, stride=1, dilation=1, group=1)[source]

Returns the depth-wise convolution value for the input.

Applies depthwise conv2d for the input, which will generate more channels with channel_multiplier. Given an input tensor of shape \((N, C_{in}, H_{in}, W_{in})\) where \(N\) is the batch size, \(C\) is the channels, \(H\) is height, \(W\) is width and a filter tensor with kernel size \((\text{kernel_size[0]}, \text{kernel_size[1]})\), where \(\text{kernel_size[0]}\) indicates the kernel_size of height, \(\text{kernel_size[1]}\) indicates the kernel_size of width, containing \(C_{in} * \text{channel_multiplier}\) convolutional filters of depth 1; it applies different filters to each input channel (channel_multiplier channels for each input channel has the default value 1), then concatenates the results together. The output has \(C_{in} * \text{channel_multiplier}\) channels.

Parameters
  • channel_multiplier (int) – The multiplier for the original output convolution. Its value must be greater than 0.

  • kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 2 integers. Specifies the height and width of the 2D convolution window. Single int means the value is for both the height and the width of the kernel. A tuple of 2 ints means the first value is for the height and the other is for the width of the kernel.

  • mode (int) – Modes for different convolutions. 0 Math convolution, 1 cross-correlation convolution , 2 deconvolution, 3 depthwise convolution. Default: 3.

  • pad_mode (str) –

    Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “valid”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input x. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side. If this mode is set, pad must be 0.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, pad must be 0.

    • pad: Implicit paddings on both sides of the input x. The number of pad will be padded to the input Tensor borders. pad must be greater than or equal to 0.

  • pad (Union[int, tuple[int]]) – Implicit paddings on both sides of the input x. If pad is one integer, the paddings of top, bottom, left and right are the same, equal to pad. If pad is a tuple with four integers, the paddings of top, bottom, left and right will be equal to pad[0], pad[1], pad[2], and pad[3] accordingly. Default: 0.

  • stride (Union(int, tuple[int])) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.

  • dilation (Union(int, tuple[int])) – The data type is int or a tuple of 2 integers. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the height and width of the input x. Default: 1.

  • group (int) – Splits input into groups. Default: 1.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

  • weight (Tensor) - Set the size of kernel as \((\text{kernel_size[0]}, \text{kernel_size[1]})\), then the shape is \((K, C_{in}, \text{kernel_size[0]}, \text{kernel_size[1]})\), K must be 1.

Outputs:

Tensor of shape \((N, C_{in} * \text{channel_multiplier}, H_{out}, W_{out})\).

Raises
  • TypeError – If kernel_size, stride, pad or dilation is neither an int nor a tuple.

  • TypeError – If channel_multiplier or group is not an int.

  • ValueError – If stride or dilation is less than 1.

  • ValueError – If pad_mode is not one of the following:’same’, ‘valid’ or ‘pad’.

  • ValueError – If pad_mode it not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0).

Supported Platforms:

Ascend

Examples

>>> x = Tensor(np.ones([10, 32, 32, 32]), mindspore.float32)
>>> weight = Tensor(np.ones([1, 32, 3, 3]), mindspore.float32)
>>> depthwise_conv2d = ops.DepthwiseConv2dNative(channel_multiplier=3, kernel_size=(3, 3))
>>> output = depthwise_conv2d(x, weight)
>>> print(output.shape)
(10, 96, 30, 30)