mindspore.ops.Conv2D

class mindspore.ops.Conv2D(*args, **kwargs)[source]

2D convolution layer.

Applies a 2D convolution over an input tensor which is typically of shape (N,Cin,Hin,Win), where N is batch size and Cin is channel number. For each batch of shape (Cin,Hin,Win), the formula is defined as:

outj=i=0Cin1ccor(Wij,Xi)+bj,

where ccor is the cross correlation operator, Cin is the input channel number, j ranges from 0 to Cout1, Wij corresponds to the i-th channel of the j-th filter and outj corresponds to the j-th channel of the output. Wij is a slice of kernel and it has shape (ks_h,ks_w), where ks_h and ks_w are the height and width of the convolution kernel. The full kernel has shape (Cout,Cin//group,ks_h,ks_w), where group 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 1+Hin+2×paddingks_h(ks_h1)×(dilation1)stride and 1+Win+2×paddingks_w(ks_w1)×(dilation1)stride respectively.

The first introduction can be found in paper Gradient Based Learning Applied to Document Recognition. More detailed introduction can be found here: http://cs231n.github.io/convolutional-networks/.

Parameters
  • out_channel (int) – The dimension of the output.

  • kernel_size (Union[int, tuple[int]]) – The kernel size of the 2D convolution.

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

  • pad_mode (str) – Modes to fill padding. It could be “valid”, “same”, or “pad”. Default: “valid”.

  • pad (Union(int, tuple[int])) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of top, bottom, left and right are the same, equal to pad. If pad is a tuple of four integers, the padding of top, bottom, left and right equal to pad[0], pad[1], pad[2], and pad[3] correspondingly.

  • stride (Union(int, tuple[int])) – The stride to be applied to the convolution filter. Default: 1.

  • dilation (Union(int, tuple[int])) – Specifies the space to use between kernel elements. Default: 1.

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

  • data_format (str) – The optional value for data format, is ‘NHWC’ or ‘NCHW’. Default: “NCHW”.

Inputs:
  • input (Tensor) - Tensor of shape (N,Cin,Hin,Win).

  • weight (Tensor) - Set size of kernel is (K1,K2), then the shape is (Cout,Cin,K1,K2).

Outputs:

Tensor, the value that applied 2D convolution. The shape is (N,Cout,Hout,Wout).

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

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

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

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

  • ValueError – If pad is a tuple whose length is not equal to 4.

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

  • ValueError – If data_format is neither ‘NCHW’ not ‘NHWC’.

Supported Platforms:

Ascend GPU CPU

Examples

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