Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

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 tenor is typically of shape (N,Cin,Hin,Win), where N is batch size, C is channel number, H is feature height, W is feature width.

The output is calculated based on formula:

out(Ni,Coutj)=bias(Coutj)+k=0Cin1ccor(weight(Coutj,k),X(Ni,k))

where bias is the output channel bias, ccor is the cross-correlation, , weight is the convolution kernel value and X represents the input feature map.

Here are the indices' meanings:

  • i corresponds to the batch number, the range is [0,N1], where N is the batch size of the input.

  • j corresponds to the output channel, the range is [0,Cout1], where Cout is the number of output channels, which is also equal to the number of kernels.

  • k corresponds to the input channel, the range is [0,Cin1], where Cin is the number of input channels, which is also equal to the number of channels in the convolutional kernels.

Therefore, in the above formula, bias(Coutj) represents the bias of the j-th output channel, weight(Coutj,k) represents the slice of the j-th convolutional kernel in the k-th channel, and X(Ni,k) represents the slice of the k-th input channel in the i-th batch of the input feature map.

The shape of the convolutional kernel is given by (kernel_size[0],kernel_size[1]), where kernel_size[0] and kernel_size[1] are the height and width of the kernel, respectively. If we consider the input and output channels as well as the group parameter, the complete kernel shape will be (Cout,Cin/group,kernel_size[0],kernel_size[1]), where group is the number of groups dividing x's input channel when applying group convolution.

For more details about convolution layer, please refer to Gradient Based Learning Applied to Document Recognition and ConvNets.

Note

On Ascend platform, only group convolution in depthwise convolution scenarios is supported. That is, when groups>1, condition Cin = Cout = groups must be satisfied.

Parameters
  • input (Tensor) – Tensor of shape (N,Cin,Hin,Win).

  • weight (Tensor) – Tensor of shape (N,Cin/groups,kernel_size[0],kernel_size[1]), then the size of kernel is (kernel_size[0],kernel_size[1]).

  • bias (Tensor, optional) – Bias Tensor with shape (Cout). 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], list[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/list with 2 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 k1 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,Cout,Hout,Wout). To see how different pad modes affect the output shape, please refer to mindspore.nn.Conv2d for more details.

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 (Cout) .

  • 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/list 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

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> 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)