mindspore.ops.conv2d

mindspore.ops.conv2d(input, weight, bias=None, stride=1, pad_mode='valid', padding=0, dilation=1, groups=1)[source]

Applies a 2D convolution over an input tensor. The input tensor is typically of shape \((N, C_{in}, H_{in}, W_{in})\), where \(N\) is batch size, \(C\) is channel number, \(H\) is height, \(W\) is width, \(X_i\) is the \(i^{th}\) input value and \(b_i\) indicates the deviation value of the \(i^{th}\) input value. For each batch of shape \((C_{in}, H_{in}, W_{in})\), the formula is defined as:

\[out_j = \sum_{i=0}^{C_{in} - 1} ccor(W_{ij}, X_i) + b_j,\]

where \(ccor\) is the cross-correlation operator, \(C_{in}\) is the input channel number, \(j\) ranges from \(0\) to \(C_{out} - 1\), \(W_{ij}\) corresponds to the \(i\)-th channel of the \(j\)-th filter and \(out_{j}\) corresponds to the \(j\)-th channel of the output. \(W_{ij}\) is a slice of kernel, and it has shape \((\text{kernel_size[0]}, \text{kernel_size[1]})\), where \(\text{ kernel_size[0]}\) and \(\text{kernel_size[1]}\) are the height and width of the convolution kernel. The full kernel has shape \((C_{out}, C_{in} / \text{groups}, \text{kernel_size[0]}, \text{kernel_size[1]})\), where groups is the group number to split the input in the channel dimension.

If the pad_mode is set to be “valid”, the output height and width will be \(\left \lfloor{ 1 + \frac{H_{in} + \text{padding[0]} + \text{padding[1]} - \text{kernel_size[0]} - (\text{kernel_size[0]} - 1) \times(\text{dilation[0]} - 1)} {\text { stride[0] }}} \right \rfloor\) and

\(\left \lfloor{1 + \frac{W_{in} + \text{padding[2]} + \text{padding[3]} - \text{kernel_size[1]} - (\text{kernel_size[1]} - 1) \times(\text{dilation[1]} - 1)} {\text { stride[1] }}} \right \rfloor\) respectively.

where \(dilation\) is spacing between kernel elements, \(stride\) is The step length of each step, \(padding\) is zero-padding added to both sides of the input. For output height and width on other pad_mode, please refer to formula on mindspore.nn.Conv2d.

The first introduction can be found in paper Gradient Based Learning Applied to Document Recognition. More detailed introduction can be found here: ConvNets .

Note

On Ascend platform, only group convolution in depthwise convolution scenarios is supported. That is, when groups>1, condition C_{in} = C_{out} = groups must be satisfied.

Parameters
  • input (Tensor) – Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

  • weight (Tensor) – Tensor of shape \((N, C_{in} / \text{groups}, \text{kernel_size[0]}, \text{kernel_size[1]})\), then the size of kernel is \((\text{kernel_size[0]}, \text{kernel_size[1]})\).

  • bias (Tensor) – Bias Tensor with shape \((C_{out})\). When bias is None, zeros will be used. Default: None.

  • stride (Union(int, tuple[int]), optional) – 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.

  • pad_mode (str, optional) –

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

    • same: Adopts the way of completion. The height and width of the output will be equal to the input x divided by stride. The padding will be evenly calculated in top and bottom, left and right possiblily. Otherwise, the last extra padding will be calculated from the bottom and the right side. If this mode is set, padding 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, padding must be 0.

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

  • padding (Union(int, tuple[int]), optional) – Implicit paddings on both sides of the input x. If padding is one integer, the paddings of top, bottom, left and right are the same, equal to padding. If padding is a tuple with two integers, the padding of top adn bottom is padding[0], and the padding of left and right is padding[1]. Default: 0.

  • dilation (Union(int, tuple[int]), optional) – Gaps between kernel elements.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 than or equal to 1 and bounded by the height and width of the input x. Default: 1.

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

Returns

Tensor, the value that applied 2D convolution. The shape is \((N, C_{out}, H_{out}, W_{out})\).

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

  • TypeErrorgroups is not an int.

  • TypeError – If bias is not a Tensor.

  • ValueError – If the shape of bias is not \(C_{out}\) .

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

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

  • ValueError – If padding is a tuple whose length is not equal to 2.

  • ValueError – If pad_mode is not equal to ‘pad’ and padding is greater than 0.

Supported Platforms:

Ascend GPU CPU

Examples

>>> x = Tensor(np.ones([10, 32, 32, 32]), mindspore.float32)
>>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32)
>>> output = ops.conv2d(x, weight)
>>> print(output.shape)
(10, 32, 30, 30)