mindspore.ops.Conv2D
- class mindspore.ops.Conv2D(out_channel, kernel_size, mode=1, pad_mode='valid', pad=0, stride=1, dilation=1, group=1, data_format='NCHW')[source]
2D convolution layer.
Applies a 2D convolution over an input tensor which is typically of shape \((N, C_{in}, H_{in}, W_{in})\), where \(N\) is batch size, \(C\) is channel number, \(H\) is feature height, \(W\) is feature width.
The output is calculated based on formula:
\[\text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + \sum_{k = 0}^{C_{in} - 1} \text{ccor}({\text{weight}(C_{\text{out}_j}, k), \text{X}(N_i, 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, ranging from 0 to N-1, where N is the batch size of the input.
\(j\) corresponds to the output channel, ranging from 0 to C_{out}-1, where C_{out} is the number of output channels, which is also equal to the number of kernels.
\(k\) corresponds to the input channel, ranging from 0 to C_{in}-1, where C_{in} 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}(C_{out_j})\) represents the bias of the \(j\)-th output channel, \({weight}(C_{out_j}, k)\) represents the slice of the \(j\)-th convolutional kernel in the \(k\)-th channel, and \({X}(N_i, 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 \((C_{out}, C_{in} / \text{group}, \text{kernel_size[0]}, \text{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.
Note
On Ascend platform, only group convolution in depthwise convolution scenarios is supported. That is, when group>1, condition in_channels = out_channels = group must be satisfied.
- Parameters
out_channel (int) – Specifies output channel \(C_{out}\).
kernel_size (Union[int, tuple[int]]) – Specifies the height and width of the 2D convolution kernel. It can be a single int or a tuple of 2 integers. A single int means the value is for both the height and the width. A tuple of 2 ints means the first value is for the height and the other is for the width.
mode (int, optional) – Modes for different convolutions. The value is currently not used. Default:
1
.pad_mode (str, optional) –
Specifies the padding mode with a padding value of 0. It can be set to:
"same"
,"valid"
or"pad"
. Default:"valid"
."same"
: Pad the input around its edges so that the shape of input and output are the same when stride is set to1
. The amount of padding to is calculated by the operator internally, If the amount is even, it is uniformly distributed around the input, if it is odd, the excess amount goes to the right/bottom side. If this mode is set, pad must be 0."valid"
: No padding is applied to the input, and the output returns the maximum possible height and width. Extra pixels that could not complete a full stride will be discarded. If this mode is set, pad must be 0."pad"
: Pad the input with a specified amount. In this mode, the amount of padding in the height and width directions is determined by the pad parameter. If this mode is set, pad must be greater than or equal to 0.
pad (Union(int, tuple[int]), optional) – Specifies the amount of padding to apply on input when pad_mode is set to
"pad"
. It can be a single int or a tuple of 4 ints. 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]), optional) – Specifies the stride of the convolution kernel’s movement. It can be a single int or a tuple of two or four ints. A single int means the stride is the same in both the height and width directions. A tuple of two ints indicates the strides in the height and width directions, respectively. For a tuple of four ints, the two ints correspond to (N, C) dimension are treated as 1, and the two correspond to (H, W) dimensions is the step size in the height and width directions respectively. Default:
1
.dilation (Union(int, tuple[int]), optional) – Specifies the dilation rate to use for dilated convolution. It can be a single int or a tuple of 2 or 4 integers. A single int means the dilation size is the same in both the height and width directions. A tuple of two ints represents the dilation size in the height and width directions, respectively. For a tuple of four ints, the two ints correspond to (N, C) dimension are treated as 1, and the two correspond to (H, W) dimensions is the dilation size in the height and width directions respectively. Assuming \(dilation=(d0, d1)\), the convolutional kernel samples the input with a spacing of \(d0-1\) elements in the height direction and \(d1-1\) elements in the width direction. The values in the height and width dimensions are in the ranges [1, H] and [1, W], respectively. Default:
1
.group (int, optional) – Specifies the number of groups dividing x’s input channel when applying group convolution. Default:
1
.data_format (str, optional) – The optional value for data format, is
'NHWC'
or'NCHW'
. Default:"NCHW"
.
- Inputs:
x (Tensor) - Input tensor of shape \((N, C_{in}, H_{in}, W_{in})\) or \((N, H_{in}, W_{in}, C_{in}, )\) depending on data_format .
weight (Tensor) - The convolutional kernel value, it should has shape \((C_{out}, C_{in} / \text{group}, \text{kernel_size[0]}, \text{kernel_size[1]})\) .
- Outputs:
Tensor, the value that applied 2D convolution. The shape is \((N, C_{out}, H_{out}, W_{out})\) or \((N, H_{out}, W_{out}, C_{out}, )\). To see how different pad modes affect the output shape, please refer to
mindspore.nn.Conv2d
for more details.
- 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'
or'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
'NHWC'
nor'NCHW'
.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor, ops >>> # case 1: All parameters use default values. >>> x = 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(x, weight) >>> print(output.shape) (10, 32, 30, 30) >>> # case 2: pad_mode="pad", other parameters being default. >>> x = 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, pad_mode="pad", pad=(4, 10, 4, 10)) >>> output = conv2d(x, weight) >>> print(output.shape) (10, 32, 44, 44) >>> # case 3: stride=(2, 4), other parameters being default. >>> x = 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, stride=(2, 4)) >>> output = conv2d(x, weight) >>> print(output.shape) (10, 32, 15, 8) >>> # case 4: dilation=2, other parameters being default. >>> x = 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, dilation=2) >>> output = conv2d(x, weight) >>> print(output.shape) (10, 32, 28, 28) >>> # case 5: group=2, other parameters being default. >>> x = Tensor(np.ones([10, 64, 32, 32]), mindspore.float32) >>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32) >>> conv2d = ops.Conv2D(out_channel=32, kernel_size=3, group=2) >>> output = conv2d(x, weight) >>> print(output.shape) (10, 32, 30, 30) >>> # case 6: All parameters are specified. >>> x = Tensor(np.ones([10, 64, 32, 32]), mindspore.float32) >>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32) >>> conv2d = ops.Conv2D(out_channel=32, kernel_size=3, pad_mode="pad", ... pad=(4, 10, 4, 10), stride=(2, 4), dilation=2, group=2) >>> output = conv2d(x, weight) >>> print(output.shape) (10, 32, 21, 11)