mindspore.nn.Conv2dBnFoldQuant

class mindspore.nn.Conv2dBnFoldQuant(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, eps=1e-05, momentum=0.997, has_bias=False, weight_init='normal', bias_init='zeros', beta_init='zeros', gamma_init='ones', mean_init='zeros', var_init='ones', fake=True, quant_config=quant_config_default, quant_dtype=QuantDtype.INT8, freeze_bn=100000)[source]

2D convolution with Batch Normalization operation folded construct.

This part is a more detailed overview of Conv2d operation. For more detials about Quantilization, please refer to mindspore.nn.FakeQuantWithMinMaxObserver.

Parameters
  • in_channels (int) – The number of input channel \(C_{in}\).

  • out_channels (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple]) – Specifies the height and width of the 2D convolution window.

  • stride (int) – Specifies stride for all spatial dimensions with the same value.

  • pad_mode (str) – Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “same”.

  • padding (int) – Implicit paddings on both sides of the input. Default: 0.

  • eps (float) – Parameters for Batch Normalization. Default: 1e-5.

  • momentum (float) – Parameters for Batch Normalization op. Default: 0.997.

  • dilation (int) – Specifies the dilation rate to use for dilated convolution. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. Default: 1.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Default: ‘zeros’.

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta vector. Default: ‘zeros’.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma vector. Default: ‘ones’.

  • mean_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the mean vector. Default: ‘zeros’.

  • var_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the variance vector. Default: ‘ones’.

  • fake (bool) – Whether Conv2dBnFoldQuant Cell adds FakeQuantWithMinMaxObserver. Default: True.

  • quant_config (QuantConfig) – Configures the oberser types and quant settings of weight and activation. Can be generated by compression.quant.create_quant_config method. Default: both set to default FakeQuantWithMinMaxObserver.

  • quant_dtype (QuantDtype) – Specifies the FakeQuant datatype. Default: QuantDtype.INT8.

  • freeze_bn (int) – The quantization freeze Batch Normalization op is according to the global step. Default: 100000.

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

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\).

Raises
  • TypeError – If in_channels, out_channels, stride, padding or dilation is not an int.

  • TypeError – If has_bias is not a bool.

  • ValueError – If in_channels or out_channels stride, padding or dilation is less than 1.

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

Supported Platforms:

Ascend GPU

Examples

>>> qconfig = compression.quant.create_quant_config()
>>> conv2d_bnfold = nn.Conv2dBnFoldQuant(1, 6, kernel_size=(2, 2), stride=(1, 1), pad_mode="valid",
...                                      quant_config=qconfig)
>>> input = Tensor(np.random.randint(-2, 2, (2, 1, 3, 3)), mindspore.float32)
>>> output = conv2d_bnfold(input)
>>> print(output.shape)
(2, 6, 2, 2)